{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T08:24:49.334211Z","iopub.execute_input":"2022-07-14T08:24:49.335201Z","iopub.status.idle":"2022-07-14T08:24:49.368865Z","shell.execute_reply.started":"2022-07-14T08:24:49.335070Z","shell.execute_reply":"2022-07-14T08:24:49.367409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import all the libraries \nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:49.372243Z","iopub.execute_input":"2022-07-14T08:24:49.373105Z","iopub.status.idle":"2022-07-14T08:24:51.118285Z","shell.execute_reply.started":"2022-07-14T08:24:49.373040Z","shell.execute_reply":"2022-07-14T08:24:51.117114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/titanic/train.csv\", index_col=0)\ntest_data = pd.read_csv(\"../input/titanic/test.csv\", index_col=0)\n\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.120440Z","iopub.execute_input":"2022-07-14T08:24:51.121522Z","iopub.status.idle":"2022-07-14T08:24:51.198270Z","shell.execute_reply.started":"2022-07-14T08:24:51.121470Z","shell.execute_reply":"2022-07-14T08:24:51.196843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.200094Z","iopub.execute_input":"2022-07-14T08:24:51.200456Z","iopub.status.idle":"2022-07-14T08:24:51.225502Z","shell.execute_reply.started":"2022-07-14T08:24:51.200422Z","shell.execute_reply":"2022-07-14T08:24:51.223427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.229058Z","iopub.execute_input":"2022-07-14T08:24:51.230125Z","iopub.status.idle":"2022-07-14T08:24:51.249205Z","shell.execute_reply.started":"2022-07-14T08:24:51.230054Z","shell.execute_reply":"2022-07-14T08:24:51.248055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= train_data.drop(columns=['Name', 'Ticket','Cabin'])\ntest= test_data.drop(columns=['Name', 'Ticket','Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.250653Z","iopub.execute_input":"2022-07-14T08:24:51.251046Z","iopub.status.idle":"2022-07-14T08:24:51.264714Z","shell.execute_reply.started":"2022-07-14T08:24:51.251009Z","shell.execute_reply":"2022-07-14T08:24:51.263215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.648559Z","iopub.execute_input":"2022-07-14T08:24:51.649403Z","iopub.status.idle":"2022-07-14T08:24:51.667372Z","shell.execute_reply.started":"2022-07-14T08:24:51.649350Z","shell.execute_reply":"2022-07-14T08:24:51.666004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Sex'].replace(['male', 'female'], [0, 1], inplace=True)\ntrain['Embarked'].replace(['C','Q','S'], [0, 1,2], inplace=True)\ntest['Sex'].replace(['male', 'female'], [0, 1], inplace=True)\ntest['Embarked'].replace(['C','Q','S'], [0, 1,2], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.670409Z","iopub.execute_input":"2022-07-14T08:24:51.671544Z","iopub.status.idle":"2022-07-14T08:24:51.686907Z","shell.execute_reply.started":"2022-07-14T08:24:51.671482Z","shell.execute_reply":"2022-07-14T08:24:51.685675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.688933Z","iopub.execute_input":"2022-07-14T08:24:51.689449Z","iopub.status.idle":"2022-07-14T08:24:51.720995Z","shell.execute_reply.started":"2022-07-14T08:24:51.689408Z","shell.execute_reply":"2022-07-14T08:24:51.719519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['Age', 'Fare','Embarked']] = train[['Age', 'Fare','Embarked']].fillna(train[['Age', 'Fare','Embarked']].median())\ntest[['Age', 'Fare','Embarked']] = test[['Age', 'Fare','Embarked']].fillna(test[['Age', 'Fare','Embarked']].median())","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.724234Z","iopub.execute_input":"2022-07-14T08:24:51.724782Z","iopub.status.idle":"2022-07-14T08:24:51.758996Z","shell.execute_reply.started":"2022-07-14T08:24:51.724728Z","shell.execute_reply":"2022-07-14T08:24:51.757636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:25:29.118940Z","iopub.execute_input":"2022-07-14T08:25:29.119543Z","iopub.status.idle":"2022-07-14T08:25:29.144006Z","shell.execute_reply.started":"2022-07-14T08:25:29.119480Z","shell.execute_reply":"2022-07-14T08:25:29.143064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = train['Survived']\nX = train.drop(labels='Survived', axis = 1)\nx_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.766339Z","iopub.execute_input":"2022-07-14T08:24:51.766912Z","iopub.status.idle":"2022-07-14T08:24:51.781222Z","shell.execute_reply.started":"2022-07-14T08:24:51.766859Z","shell.execute_reply":"2022-07-14T08:24:51.779643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train using random forest trees\nrandom = RandomForestClassifier()\nrandom.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:51.783227Z","iopub.execute_input":"2022-07-14T08:24:51.784318Z","iopub.status.idle":"2022-07-14T08:24:52.085856Z","shell.execute_reply.started":"2022-07-14T08:24:51.784255Z","shell.execute_reply":"2022-07-14T08:24:52.084344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy using random forest \", random.score(x_test, y_test)*100)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:52.087766Z","iopub.execute_input":"2022-07-14T08:24:52.088284Z","iopub.status.idle":"2022-07-14T08:24:52.121133Z","shell.execute_reply.started":"2022-07-14T08:24:52.088227Z","shell.execute_reply":"2022-07-14T08:24:52.119627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survivor = random.predict(x_test)\nsurvivor","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:52.124590Z","iopub.execute_input":"2022-07-14T08:24:52.125449Z","iopub.status.idle":"2022-07-14T08:24:52.156232Z","shell.execute_reply.started":"2022-07-14T08:24:52.125394Z","shell.execute_reply":"2022-07-14T08:24:52.155354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(survivor,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:52.157462Z","iopub.execute_input":"2022-07-14T08:24:52.158420Z","iopub.status.idle":"2022-07-14T08:24:52.166621Z","shell.execute_reply.started":"2022-07-14T08:24:52.158380Z","shell.execute_reply":"2022-07-14T08:24:52.165259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission = pd.DataFrame({'PassengerId': x_test.index,  'Survived': survivor})\n#submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:24:52.168328Z","iopub.execute_input":"2022-07-14T08:24:52.169014Z","iopub.status.idle":"2022-07-14T08:24:52.180537Z","shell.execute_reply.started":"2022-07-14T08:24:52.168971Z","shell.execute_reply":"2022-07-14T08:24:52.179041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = random.predict(test)\n\ntest['Survived'] = predictions\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:27:02.229391Z","iopub.execute_input":"2022-07-14T08:27:02.230065Z","iopub.status.idle":"2022-07-14T08:27:02.275145Z","shell.execute_reply.started":"2022-07-14T08:27:02.230001Z","shell.execute_reply":"2022-07-14T08:27:02.273928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test['Survived']\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:27:26.990169Z","iopub.execute_input":"2022-07-14T08:27:26.990662Z","iopub.status.idle":"2022-07-14T08:27:26.999868Z","shell.execute_reply.started":"2022-07-14T08:27:26.990617Z","shell.execute_reply":"2022-07-14T08:27:26.998661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:29:32.399028Z","iopub.execute_input":"2022-07-14T08:29:32.399534Z","iopub.status.idle":"2022-07-14T08:29:32.408892Z","shell.execute_reply.started":"2022-07-14T08:29:32.399489Z","shell.execute_reply":"2022-07-14T08:29:32.407605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv',header=True,index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T08:29:53.299658Z","iopub.execute_input":"2022-07-14T08:29:53.301043Z","iopub.status.idle":"2022-07-14T08:29:53.311393Z","shell.execute_reply.started":"2022-07-14T08:29:53.300967Z","shell.execute_reply":"2022-07-14T08:29:53.310033Z"},"trusted":true},"execution_count":null,"outputs":[]}]}