{"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":"%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport os\nimport sklearn\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.pipeline import FeatureUnion, make_pipeline\nfrom sklearn.compose import make_column_transformer\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import cross_val_score,GridSearchCV\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:30.178948Z","iopub.execute_input":"2022-07-31T05:44:30.179444Z","iopub.status.idle":"2022-07-31T05:44:32.249083Z","shell.execute_reply.started":"2022-07-31T05:44:30.179347Z","shell.execute_reply":"2022-07-31T05:44:32.247854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_csv(_filename, _dir):\n    #get and read test csv\n    csv_path = os.path.join(_dir, _filename)\n    return pd.read_csv(csv_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.252147Z","iopub.execute_input":"2022-07-31T05:44:32.253098Z","iopub.status.idle":"2022-07-31T05:44:32.257807Z","shell.execute_reply.started":"2022-07-31T05:44:32.253060Z","shell.execute_reply":"2022-07-31T05:44:32.256853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = get_csv(\"train.csv\", \"/kaggle/input/titanic\")\ntrain_set.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.259035Z","iopub.execute_input":"2022-07-31T05:44:32.259554Z","iopub.status.idle":"2022-07-31T05:44:32.306151Z","shell.execute_reply.started":"2022-07-31T05:44:32.259521Z","shell.execute_reply":"2022-07-31T05:44:32.305359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = get_csv(\"test.csv\", \"/kaggle/input/titanic\")\ntest_set.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.308233Z","iopub.execute_input":"2022-07-31T05:44:32.308749Z","iopub.status.idle":"2022-07-31T05:44:32.332131Z","shell.execute_reply.started":"2022-07-31T05:44:32.308718Z","shell.execute_reply":"2022-07-31T05:44:32.331361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.333406Z","iopub.execute_input":"2022-07-31T05:44:32.333864Z","iopub.status.idle":"2022-07-31T05:44:32.357561Z","shell.execute_reply.started":"2022-07-31T05:44:32.333835Z","shell.execute_reply":"2022-07-31T05:44:32.356703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.describe().style.background_gradient(cmap = \"Blues\")","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.358785Z","iopub.execute_input":"2022-07-31T05:44:32.359252Z","iopub.status.idle":"2022-07-31T05:44:32.469133Z","shell.execute_reply.started":"2022-07-31T05:44:32.359215Z","shell.execute_reply":"2022-07-31T05:44:32.468053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_matrix = train_set.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.471559Z","iopub.execute_input":"2022-07-31T05:44:32.472245Z","iopub.status.idle":"2022-07-31T05:44:32.479408Z","shell.execute_reply.started":"2022-07-31T05:44:32.472203Z","shell.execute_reply":"2022-07-31T05:44:32.478289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_matrix[\"Survived\"].sort_values(ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.480627Z","iopub.execute_input":"2022-07-31T05:44:32.481199Z","iopub.status.idle":"2022-07-31T05:44:32.492213Z","shell.execute_reply.started":"2022-07-31T05:44:32.481157Z","shell.execute_reply":"2022-07-31T05:44:32.491177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_set.columns:\n    sns.barplot(train_set[i].value_counts().index, train_set[i].value_counts()).set_title(i)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:44:32.493579Z","iopub.execute_input":"2022-07-31T05:44:32.494351Z","iopub.status.idle":"2022-07-31T05:45:14.615642Z","shell.execute_reply.started":"2022-07-31T05:44:32.494290Z","shell.execute_reply":"2022-07-31T05:45:14.614619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.hist(bins=50 , figsize= (20, 15))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:14.619718Z","iopub.execute_input":"2022-07-31T05:45:14.620028Z","iopub.status.idle":"2022-07-31T05:45:16.408905Z","shell.execute_reply.started":"2022-07-31T05:45:14.620000Z","shell.execute_reply":"2022-07-31T05:45:16.408050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(train_set[\"PassengerId\"], train_set[\"Survived\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:16.410184Z","iopub.execute_input":"2022-07-31T05:45:16.410691Z","iopub.status.idle":"2022-07-31T05:45:26.290448Z","shell.execute_reply.started":"2022-07-31T05:45:16.410658Z","shell.execute_reply":"2022-07-31T05:45:26.289589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[\"Embarked\"].dropna(inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.291927Z","iopub.execute_input":"2022-07-31T05:45:26.292608Z","iopub.status.idle":"2022-07-31T05:45:26.298789Z","shell.execute_reply.started":"2022-07-31T05:45:26.292564Z","shell.execute_reply":"2022-07-31T05:45:26.297877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[\"Age\"].fillna(train_set[\"Age\"].median(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.300388Z","iopub.execute_input":"2022-07-31T05:45:26.300783Z","iopub.status.idle":"2022-07-31T05:45:26.312259Z","shell.execute_reply.started":"2022-07-31T05:45:26.300743Z","shell.execute_reply":"2022-07-31T05:45:26.311143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.info()\n# don't know what to do with cabi9n, usefull , yet too little values here so maybe put a median value there.","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.314084Z","iopub.execute_input":"2022-07-31T05:45:26.314527Z","iopub.status.idle":"2022-07-31T05:45:26.331521Z","shell.execute_reply.started":"2022-07-31T05:45:26.314487Z","shell.execute_reply":"2022-07-31T05:45:26.330379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_set = train_set.loc[:, [\"Survived\", \"Sex\", \"Age\", \"SibSp\", \"Parch\", \"Fare\"]]\nnew_train_set.info()\ntest_set.dropna()\n\ntest_set.drop(\"Cabin\", axis=1, inplace=True)\n#cut out the data that is not needed","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.333528Z","iopub.execute_input":"2022-07-31T05:45:26.334330Z","iopub.status.idle":"2022-07-31T05:45:26.351753Z","shell.execute_reply.started":"2022-07-31T05:45:26.334256Z","shell.execute_reply":"2022-07-31T05:45:26.351011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reg = DecisionTreeClassifier()\nx = new_train_set.drop(\"Survived\", axis = \"columns\")\ny = new_train_set.Survived\nx.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.353246Z","iopub.execute_input":"2022-07-31T05:45:26.353639Z","iopub.status.idle":"2022-07-31T05:45:26.367002Z","shell.execute_reply.started":"2022-07-31T05:45:26.353607Z","shell.execute_reply":"2022-07-31T05:45:26.365806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(reg.get_params().keys())","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:45:26.368825Z","iopub.execute_input":"2022-07-31T05:45:26.369690Z","iopub.status.idle":"2022-07-31T05:45:26.378716Z","shell.execute_reply.started":"2022-07-31T05:45:26.369642Z","shell.execute_reply":"2022-07-31T05:45:26.377284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"column_trans = make_column_transformer((OneHotEncoder(), [\"Sex\"]),(SimpleImputer(), [\"Age\", \"Fare\"]) ,remainder= \"passthrough\")\ncolumn_trans.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:21.146629Z","iopub.execute_input":"2022-07-31T05:46:21.147027Z","iopub.status.idle":"2022-07-31T05:46:21.164383Z","shell.execute_reply.started":"2022-07-31T05:46:21.146997Z","shell.execute_reply":"2022-07-31T05:46:21.163566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = [{'max_depth' : [3, 10 ,30] ,'max_features': [6,8,10,12]}]\ngrid_search = GridSearchCV(reg, param_grid,cv=5, scoring=\"neg_mean_squared_error\")\ngrid_search_pipeline = make_pipeline(column_trans, grid_search)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:23.129138Z","iopub.execute_input":"2022-07-31T05:46:23.129545Z","iopub.status.idle":"2022-07-31T05:46:23.135754Z","shell.execute_reply.started":"2022-07-31T05:46:23.129513Z","shell.execute_reply":"2022-07-31T05:46:23.134542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search_pipeline.fit(x,y)\ngrid_search_pipeline.predict(x)\nprint(grid_search.best_estimator_)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:25.232481Z","iopub.execute_input":"2022-07-31T05:46:25.232850Z","iopub.status.idle":"2022-07-31T05:46:25.354929Z","shell.execute_reply.started":"2022-07-31T05:46:25.232820Z","shell.execute_reply":"2022-07-31T05:46:25.353775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = make_pipeline(column_trans, grid_search.best_estimator_)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:28.063426Z","iopub.execute_input":"2022-07-31T05:46:28.063836Z","iopub.status.idle":"2022-07-31T05:46:28.068613Z","shell.execute_reply.started":"2022-07-31T05:46:28.063804Z","shell.execute_reply":"2022-07-31T05:46:28.067645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"percentage = cross_val_score(pipe, x , y, cv=5, scoring=\"accuracy\").mean() * 100\nprint(percentage, \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:29.551885Z","iopub.execute_input":"2022-07-31T05:46:29.552577Z","iopub.status.idle":"2022-07-31T05:46:29.629883Z","shell.execute_reply.started":"2022-07-31T05:46:29.552525Z","shell.execute_reply":"2022-07-31T05:46:29.628758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe.fit(x,y)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:31.760962Z","iopub.execute_input":"2022-07-31T05:46:31.761386Z","iopub.status.idle":"2022-07-31T05:46:31.786076Z","shell.execute_reply.started":"2022-07-31T05:46:31.761350Z","shell.execute_reply":"2022-07-31T05:46:31.785349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pipe.predict(test_set)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:33.929456Z","iopub.execute_input":"2022-07-31T05:46:33.930425Z","iopub.status.idle":"2022-07-31T05:46:33.945469Z","shell.execute_reply.started":"2022-07-31T05:46:33.930372Z","shell.execute_reply":"2022-07-31T05:46:33.944319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = {'PassengerId': test_set.PassengerId, 'Survived': predictions}\nbasic_sub = pd.DataFrame(data=sub)\nbasic_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T05:46:37.125797Z","iopub.execute_input":"2022-07-31T05:46:37.126482Z","iopub.status.idle":"2022-07-31T05:46:37.136918Z","shell.execute_reply.started":"2022-07-31T05:46:37.126445Z","shell.execute_reply":"2022-07-31T05:46:37.135719Z"},"trusted":true},"execution_count":null,"outputs":[]}]}