{"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":"#importing required libraries\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier\nimport sklearn.metrics as metrics\nfrom sklearn.metrics import classification_report\nfrom sklearn.ensemble import RandomForestClassifier\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-13T08:22:56.641920Z","iopub.execute_input":"2022-08-13T08:22:56.642319Z","iopub.status.idle":"2022-08-13T08:22:56.649136Z","shell.execute_reply.started":"2022-08-13T08:22:56.642286Z","shell.execute_reply":"2022-08-13T08:22:56.647818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading training data\ntrain = pd.read_csv(\"/kaggle/input/titanic/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:22:59.934395Z","iopub.execute_input":"2022-08-13T08:22:59.934812Z","iopub.status.idle":"2022-08-13T08:22:59.955574Z","shell.execute_reply.started":"2022-08-13T08:22:59.934780Z","shell.execute_reply":"2022-08-13T08:22:59.954560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first 5 rows\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:23:03.823261Z","iopub.execute_input":"2022-08-13T08:23:03.824204Z","iopub.status.idle":"2022-08-13T08:23:03.852279Z","shell.execute_reply.started":"2022-08-13T08:23:03.824161Z","shell.execute_reply":"2022-08-13T08:23:03.851319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#number of nulls in each column\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:23:04.863151Z","iopub.execute_input":"2022-08-13T08:23:04.863570Z","iopub.status.idle":"2022-08-13T08:23:04.873936Z","shell.execute_reply.started":"2022-08-13T08:23:04.863536Z","shell.execute_reply":"2022-08-13T08:23:04.872731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking data type \ntrain.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:23:06.210421Z","iopub.execute_input":"2022-08-13T08:23:06.210848Z","iopub.status.idle":"2022-08-13T08:23:06.219813Z","shell.execute_reply.started":"2022-08-13T08:23:06.210813Z","shell.execute_reply":"2022-08-13T08:23:06.218984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{}},{"cell_type":"code","source":"sns.histplot(data = train, x=\"Survived\",color='r')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:24:30.586189Z","iopub.execute_input":"2022-08-13T08:24:30.587474Z","iopub.status.idle":"2022-08-13T08:24:30.836867Z","shell.execute_reply.started":"2022-08-13T08:24:30.587418Z","shell.execute_reply":"2022-08-13T08:24:30.835495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Target variables quantitatives\nsns.barplot(data = train, x=\"Survived\", y=\"Age\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:25:18.199298Z","iopub.execute_input":"2022-08-13T08:25:18.200119Z","iopub.status.idle":"2022-08-13T08:25:18.405912Z","shell.execute_reply.started":"2022-08-13T08:25:18.200076Z","shell.execute_reply":"2022-08-13T08:25:18.404688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data = train, y=\"Age\", x=\"Survived\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:25:39.404822Z","iopub.execute_input":"2022-08-13T08:25:39.405267Z","iopub.status.idle":"2022-08-13T08:25:39.589028Z","shell.execute_reply.started":"2022-08-13T08:25:39.405231Z","shell.execute_reply":"2022-08-13T08:25:39.587698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(data = train, x=\"Survived\", y=\"Fare\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:25:51.363695Z","iopub.execute_input":"2022-08-13T08:25:51.364743Z","iopub.status.idle":"2022-08-13T08:25:51.571712Z","shell.execute_reply.started":"2022-08-13T08:25:51.364699Z","shell.execute_reply":"2022-08-13T08:25:51.570477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data = train, y=\"Fare\", x=\"Survived\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:26:04.412040Z","iopub.execute_input":"2022-08-13T08:26:04.412448Z","iopub.status.idle":"2022-08-13T08:26:04.582291Z","shell.execute_reply.started":"2022-08-13T08:26:04.412416Z","shell.execute_reply":"2022-08-13T08:26:04.581030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#select columns to be used in training\ncolunas = ['Pclass','SibSp','Parch','Fare']\nX = train[colunas]\ny = train.Survived","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:23:07.273304Z","iopub.execute_input":"2022-08-13T08:23:07.273986Z","iopub.status.idle":"2022-08-13T08:23:07.290481Z","shell.execute_reply.started":"2022-08-13T08:23:07.273948Z","shell.execute_reply":"2022-08-13T08:23:07.289377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#there is a need for pre-processing in the other fields\n#split dataset into trainig and testing data from training data\ntrain_X, val_X, train_y, val_y = train_test_split(X,y,random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:42:29.694618Z","iopub.execute_input":"2022-08-12T10:42:29.695478Z","iopub.status.idle":"2022-08-12T10:42:29.704000Z","shell.execute_reply.started":"2022-08-12T10:42:29.695433Z","shell.execute_reply":"2022-08-12T10:42:29.703080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DecisionTreeClassifier(random_state=1)\nmodel.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:42:50.782622Z","iopub.execute_input":"2022-08-12T10:42:50.783068Z","iopub.status.idle":"2022-08-12T10:42:50.798083Z","shell.execute_reply.started":"2022-08-12T10:42:50.783028Z","shell.execute_reply":"2022-08-12T10:42:50.796731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#testing on test data\nprediction = model.predict(val_X)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:43:47.798347Z","iopub.execute_input":"2022-08-12T10:43:47.799513Z","iopub.status.idle":"2022-08-12T10:43:47.807228Z","shell.execute_reply.started":"2022-08-12T10:43:47.799451Z","shell.execute_reply":"2022-08-12T10:43:47.806284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:44:06.224123Z","iopub.execute_input":"2022-08-12T10:44:06.224552Z","iopub.status.idle":"2022-08-12T10:44:06.233517Z","shell.execute_reply.started":"2022-08-12T10:44:06.224515Z","shell.execute_reply":"2022-08-12T10:44:06.232238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics.accuracy_score(val_y, prediction)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:44:25.405381Z","iopub.execute_input":"2022-08-12T10:44:25.405806Z","iopub.status.idle":"2022-08-12T10:44:25.414969Z","shell.execute_reply.started":"2022-08-12T10:44:25.405769Z","shell.execute_reply":"2022-08-12T10:44:25.413504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(val_y,prediction))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:44:51.971921Z","iopub.execute_input":"2022-08-12T10:44:51.972342Z","iopub.status.idle":"2022-08-12T10:44:51.985019Z","shell.execute_reply.started":"2022-08-12T10:44:51.972288Z","shell.execute_reply":"2022-08-12T10:44:51.983699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Predict the test suite and send it to the scoreboard**","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:50:34.822314Z","iopub.execute_input":"2022-08-12T10:50:34.823653Z","iopub.status.idle":"2022-08-12T10:50:34.846466Z","shell.execute_reply.started":"2022-08-12T10:50:34.823603Z","shell.execute_reply":"2022-08-12T10:50:34.845650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_data[colunas]\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:50:44.467842Z","iopub.execute_input":"2022-08-12T10:50:44.468296Z","iopub.status.idle":"2022-08-12T10:50:44.486239Z","shell.execute_reply.started":"2022-08-12T10:50:44.468257Z","shell.execute_reply":"2022-08-12T10:50:44.484855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:50:47.658775Z","iopub.execute_input":"2022-08-12T10:50:47.659228Z","iopub.status.idle":"2022-08-12T10:50:47.669460Z","shell.execute_reply.started":"2022-08-12T10:50:47.659191Z","shell.execute_reply":"2022-08-12T10:50:47.668457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill the null values with the median\nmid = test.Fare.median()\ntest.fillna(mid,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:51:19.532058Z","iopub.execute_input":"2022-08-12T10:51:19.532551Z","iopub.status.idle":"2022-08-12T10:51:19.540757Z","shell.execute_reply.started":"2022-08-12T10:51:19.532510Z","shell.execute_reply":"2022-08-12T10:51:19.539478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:51:22.594320Z","iopub.execute_input":"2022-08-12T10:51:22.594836Z","iopub.status.idle":"2022-08-12T10:51:22.605323Z","shell.execute_reply.started":"2022-08-12T10:51:22.594791Z","shell.execute_reply":"2022-08-12T10:51:22.604487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idade=train.loc[train.Age>=60][\"Survived\"]\nrate_idade=sum(idade)/len(idade)\nprint(\"survivors over 60 years:\", rate_idade)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:51:25.474775Z","iopub.execute_input":"2022-08-12T10:51:25.475767Z","iopub.status.idle":"2022-08-12T10:51:25.483257Z","shell.execute_reply.started":"2022-08-12T10:51:25.475729Z","shell.execute_reply":"2022-08-12T10:51:25.481865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:51:27.030091Z","iopub.execute_input":"2022-08-12T10:51:27.030700Z","iopub.status.idle":"2022-08-12T10:51:27.038637Z","shell.execute_reply.started":"2022-08-12T10:51:27.030662Z","shell.execute_reply":"2022-08-12T10:51:27.037649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#saving output in submission.csv\noutput = pd.DataFrame({'PassengerId': test_data.PassengerId,\n                       'Survived': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:52:14.602346Z","iopub.execute_input":"2022-08-12T10:52:14.602786Z","iopub.status.idle":"2022-08-12T10:52:14.611679Z","shell.execute_reply.started":"2022-08-12T10:52:14.602750Z","shell.execute_reply":"2022-08-12T10:52:14.610431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"age=train.loc[train.Age>=60][\"Survived\"]\nrate_age=sum(age)/len(age)\nprint(\"survivors over 60 years:\", rate_age)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:53:35.532525Z","iopub.execute_input":"2022-08-12T10:53:35.532980Z","iopub.status.idle":"2022-08-12T10:53:35.542075Z","shell.execute_reply.started":"2022-08-12T10:53:35.532947Z","shell.execute_reply":"2022-08-12T10:53:35.540690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[\"Survived\"]\n\nfeatures = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\"]\nX = pd.get_dummies(train[features])\nX_test = pd.get_dummies(test_data[features])\n\nmodel = RandomForestClassifier(n_estimators=100, max_depth=6, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:59:30.768399Z","iopub.execute_input":"2022-08-12T10:59:30.769739Z","iopub.status.idle":"2022-08-12T10:59:30.871002Z","shell.execute_reply.started":"2022-08-12T10:59:30.769535Z","shell.execute_reply":"2022-08-12T10:59:30.869033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y)\nprediction = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:54:38.322473Z","iopub.execute_input":"2022-08-12T10:54:38.322911Z","iopub.status.idle":"2022-08-12T10:54:38.335266Z","shell.execute_reply.started":"2022-08-12T10:54:38.322878Z","shell.execute_reply":"2022-08-12T10:54:38.333776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': prediction})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:55:07.338242Z","iopub.execute_input":"2022-08-12T10:55:07.338682Z","iopub.status.idle":"2022-08-12T10:55:07.347214Z","shell.execute_reply.started":"2022-08-12T10:55:07.338649Z","shell.execute_reply":"2022-08-12T10:55:07.345882Z"},"trusted":true},"execution_count":null,"outputs":[]}]}