{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = train_df = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"8bf9661d2dbf2abebe937a2e59d5993150536142"},"cell_type":"code","source":"train_y = train.iloc[:,1]\ntrain_x = train[[\"Pclass\", \"Sex\", \"Age\", \"SibSp\"]]\ntest_x = test[[\"Pclass\", \"Sex\", \"Age\", \"SibSp\"]]\ntrain_x.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f6a95038f4a7d63dbe5f5b23654637d49b59389"},"cell_type":"code","source":"#train_x['Sex'] = pd.Categorical(train_x.Sex)\n\nfrom sklearn.preprocessing import LabelEncoder\n\nlabel_encoder = LabelEncoder()\ntrain_x['Sex'] = label_encoder.fit_transform(train_x['Sex'])\ntest_x['Sex'] = label_encoder.fit_transform(test_x['Sex'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c42b4ac5d74a6bdd7abb89dfd47d7e60a818307a"},"cell_type":"code","source":"train_x[\"Sex\"].replace(np.NaN, 0)\ntest_x[\"Sex\"].replace(np.NaN, 0)\ntrain_x.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2556c375a43e7addf1582b15da7243c2729bc739"},"cell_type":"code","source":"train_x[\"Age\"] = train_x[\"Age\"].fillna(0)\ntrain_x[\"Age\"] = train_x[\"Age\"].astype(np.float32)\ntest_x[\"Age\"] = test_x[\"Age\"].fillna(0)\ntest_x[\"Age\"] = test_x[\"Age\"].astype(np.float32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3242086d91077ad334820c4353f876f134553f8d"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nrf_model = RandomForestRegressor(n_estimators = 1000, random_state = 42)\nrf_model.fit(train_x, train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"627fc8236deaa99b6e61cabb1d1de4e2d3340220"},"cell_type":"code","source":"#rf_model.predict(train_x)\nrf_model.score(train_x, train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0ef8fe090389519c14da8b3f6b45c6b2a756939"},"cell_type":"code","source":"pred_y = rf_model.predict(test_x).round().astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3bfd2ce5c2fcc9e3270ff44e6e511591263c20ec"},"cell_type":"code","source":"out = pd.DataFrame(pd.read_csv(\"../input/test.csv\")['PassengerId'])\nout['Survived'] = pred_y\nout.to_csv(\"../working/submission.csv\", index = False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"37aa707c5111ae8bc5ae76c306c3a2954844b426"},"cell_type":"raw","source":""}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}