{"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":"markdown","source":"#  **_Titanic Data_**\n### [1-Data Understanding](#2)\n### [2-Data Preparation](#3)\n### [3-Model Comparing](#4)\n### [4-Predicting 'test.csv'](#5)\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np \nfrom matplotlib import pyplot as plt \nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:50.178165Z","iopub.execute_input":"2022-07-19T17:47:50.179195Z","iopub.status.idle":"2022-07-19T17:47:50.902183Z","shell.execute_reply.started":"2022-07-19T17:47:50.179103Z","shell.execute_reply":"2022-07-19T17:47:50.899676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d = pd.read_csv('../input/titanic/train.csv')\ntest_d = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:50.904500Z","iopub.execute_input":"2022-07-19T17:47:50.904884Z","iopub.status.idle":"2022-07-19T17:47:50.932089Z","shell.execute_reply.started":"2022-07-19T17:47:50.904852Z","shell.execute_reply":"2022-07-19T17:47:50.930801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"2\"></a>\n### Data Understanding","metadata":{}},{"cell_type":"code","source":"train_d.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:50.933634Z","iopub.execute_input":"2022-07-19T17:47:50.934070Z","iopub.status.idle":"2022-07-19T17:47:50.962109Z","shell.execute_reply.started":"2022-07-19T17:47:50.934026Z","shell.execute_reply":"2022-07-19T17:47:50.961206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:50.964185Z","iopub.execute_input":"2022-07-19T17:47:50.965195Z","iopub.status.idle":"2022-07-19T17:47:50.990724Z","shell.execute_reply.started":"2022-07-19T17:47:50.965152Z","shell.execute_reply":"2022-07-19T17:47:50.989442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:50.992198Z","iopub.execute_input":"2022-07-19T17:47:50.992576Z","iopub.status.idle":"2022-07-19T17:47:51.003729Z","shell.execute_reply.started":"2022-07-19T17:47:50.992545Z","shell.execute_reply":"2022-07-19T17:47:51.002171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train_d[\"Embarked\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.005511Z","iopub.execute_input":"2022-07-19T17:47:51.006932Z","iopub.status.idle":"2022-07-19T17:47:51.213708Z","shell.execute_reply.started":"2022-07-19T17:47:51.006898Z","shell.execute_reply":"2022-07-19T17:47:51.212712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"3\"></a>\n### Data Preparation","metadata":{}},{"cell_type":"code","source":"train_d[train_d.Embarked.isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.215842Z","iopub.execute_input":"2022-07-19T17:47:51.216585Z","iopub.status.idle":"2022-07-19T17:47:51.237000Z","shell.execute_reply.started":"2022-07-19T17:47:51.216536Z","shell.execute_reply":"2022-07-19T17:47:51.235799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train_d[(train_d.Cabin.str[0] == 'B') & ((train_d['Fare'] > 75.0)&(train_d['Fare'] < 85.0))]['Embarked'])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.238370Z","iopub.execute_input":"2022-07-19T17:47:51.239189Z","iopub.status.idle":"2022-07-19T17:47:51.414634Z","shell.execute_reply.started":"2022-07-19T17:47:51.239071Z","shell.execute_reply":"2022-07-19T17:47:51.413687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.Embarked.fillna('C',inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.415883Z","iopub.execute_input":"2022-07-19T17:47:51.416361Z","iopub.status.idle":"2022-07-19T17:47:51.421502Z","shell.execute_reply.started":"2022-07-19T17:47:51.416331Z","shell.execute_reply":"2022-07-19T17:47:51.420314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d[\"Title\"] = train_d['Name'].str.extract(' ([A-Za-z]+)\\.', expand=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.425662Z","iopub.execute_input":"2022-07-19T17:47:51.425980Z","iopub.status.idle":"2022-07-19T17:47:51.435382Z","shell.execute_reply.started":"2022-07-19T17:47:51.425951Z","shell.execute_reply":"2022-07-19T17:47:51.434429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.Title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.436588Z","iopub.execute_input":"2022-07-19T17:47:51.436888Z","iopub.status.idle":"2022-07-19T17:47:51.450690Z","shell.execute_reply.started":"2022-07-19T17:47:51.436860Z","shell.execute_reply":"2022-07-19T17:47:51.449728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.Title = train_d.Title.replace(['Mlle','Lady','Ms','Countess','Dona'],['Miss','Miss','Miss','Miss','Miss'])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.452112Z","iopub.execute_input":"2022-07-19T17:47:51.452714Z","iopub.status.idle":"2022-07-19T17:47:51.459301Z","shell.execute_reply.started":"2022-07-19T17:47:51.452682Z","shell.execute_reply":"2022-07-19T17:47:51.458177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titles1 = [i for i in train_d.Title.unique() if  (i != 'Master') & (i != 'Miss') & (i != 'Mrs')]\ntitles2 = ['Mr' for i in range(len(titles1))]\n\ntrain_d.Title = train_d.Title.replace(titles1,titles2)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-19T17:47:51.461026Z","iopub.execute_input":"2022-07-19T17:47:51.461841Z","iopub.status.idle":"2022-07-19T17:47:51.473104Z","shell.execute_reply.started":"2022-07-19T17:47:51.461794Z","shell.execute_reply":"2022-07-19T17:47:51.472041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.Title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.474385Z","iopub.execute_input":"2022-07-19T17:47:51.475036Z","iopub.status.idle":"2022-07-19T17:47:51.484501Z","shell.execute_reply.started":"2022-07-19T17:47:51.475002Z","shell.execute_reply":"2022-07-19T17:47:51.483060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d['Age'].fillna(train_d.groupby('Title')['Age'].transform('median'),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.486412Z","iopub.execute_input":"2022-07-19T17:47:51.487147Z","iopub.status.idle":"2022-07-19T17:47:51.493977Z","shell.execute_reply.started":"2022-07-19T17:47:51.487114Z","shell.execute_reply":"2022-07-19T17:47:51.492976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.drop(['Ticket','Cabin'],axis = 1 , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.495476Z","iopub.execute_input":"2022-07-19T17:47:51.496334Z","iopub.status.idle":"2022-07-19T17:47:51.504230Z","shell.execute_reply.started":"2022-07-19T17:47:51.496302Z","shell.execute_reply":"2022-07-19T17:47:51.503311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d[\"FamilySize\"] = train_d[\"SibSp\"] + train_d[\"Parch\"] + 1 ","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.506767Z","iopub.execute_input":"2022-07-19T17:47:51.507923Z","iopub.status.idle":"2022-07-19T17:47:51.515825Z","shell.execute_reply.started":"2022-07-19T17:47:51.507887Z","shell.execute_reply":"2022-07-19T17:47:51.514574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### For determinig people's assets i am using kmeans","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans\n\nk = KMeans(n_clusters=2)\ngrp = train_d[['Pclass','Fare']]\nk.fit(grp)\nclust = k.predict(grp)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.517060Z","iopub.execute_input":"2022-07-19T17:47:51.517942Z","iopub.status.idle":"2022-07-19T17:47:51.962878Z","shell.execute_reply.started":"2022-07-19T17:47:51.517911Z","shell.execute_reply":"2022-07-19T17:47:51.960859Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### As you can see below figure and [this figure](#1) , surviving is related to assets (cluster which we created above)","metadata":{}},{"cell_type":"code","source":"# As you can see,\nfigure = plt.figure(figsize = (15,6))\nclt = figure.add_subplot(121)\nclt.scatter(x = grp['Pclass'],y=grp['Fare'], c = clust)\nsur = figure.add_subplot(122)\nsur.scatter(x = train_d['Pclass'],y=train_d['Fare'], c = train_d['Survived'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:51.965627Z","iopub.execute_input":"2022-07-19T17:47:51.966638Z","iopub.status.idle":"2022-07-19T17:47:52.421269Z","shell.execute_reply.started":"2022-07-19T17:47:51.966577Z","shell.execute_reply":"2022-07-19T17:47:52.420050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d['Asset'] = clust","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:52.422862Z","iopub.execute_input":"2022-07-19T17:47:52.423159Z","iopub.status.idle":"2022-07-19T17:47:52.428706Z","shell.execute_reply.started":"2022-07-19T17:47:52.423129Z","shell.execute_reply":"2022-07-19T17:47:52.427842Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def heat(variable):\n    f,ax  = plt.subplots(figsize = (12,7))\n    sns.heatmap(variable,linewidths= .1, fmt= \".3f\", ax= ax, annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:52.429712Z","iopub.execute_input":"2022-07-19T17:47:52.430500Z","iopub.status.idle":"2022-07-19T17:47:52.442141Z","shell.execute_reply.started":"2022-07-19T17:47:52.430469Z","shell.execute_reply":"2022-07-19T17:47:52.440750Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heat(train_d[[\"Survived\",\"Pclass\"]].groupby(\"Pclass\").mean().sort_values(by =\"Survived\",ascending = False))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:52.443926Z","iopub.execute_input":"2022-07-19T17:47:52.445241Z","iopub.status.idle":"2022-07-19T17:47:52.727907Z","shell.execute_reply.started":"2022-07-19T17:47:52.445192Z","shell.execute_reply":"2022-07-19T17:47:52.726468Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heat(train_d[[\"Survived\",\"Sex\"]].groupby(\"Sex\").mean().sort_values(by= \"Survived\", ascending = True))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:52.730097Z","iopub.execute_input":"2022-07-19T17:47:52.730594Z","iopub.status.idle":"2022-07-19T17:47:52.987694Z","shell.execute_reply.started":"2022-07-19T17:47:52.730548Z","shell.execute_reply":"2022-07-19T17:47:52.986207Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heat(train_d[[\"Survived\",\"FamilySize\"]].groupby(\"FamilySize\").mean().sort_values(ascending = True,by = \"FamilySize\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:52.989169Z","iopub.execute_input":"2022-07-19T17:47:52.989503Z","iopub.status.idle":"2022-07-19T17:47:53.316361Z","shell.execute_reply.started":"2022-07-19T17:47:52.989473Z","shell.execute_reply":"2022-07-19T17:47:53.315451Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"1\"></a>","metadata":{}},{"cell_type":"code","source":"heat(train_d[[\"Asset\",\"Survived\"]].groupby(\"Asset\").mean().sort_values(by=\"Asset\", ascending= True))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.317681Z","iopub.execute_input":"2022-07-19T17:47:53.318162Z","iopub.status.idle":"2022-07-19T17:47:53.575128Z","shell.execute_reply.started":"2022-07-19T17:47:53.318132Z","shell.execute_reply":"2022-07-19T17:47:53.574273Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### There are some outlier datas so we need to find them","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.576456Z","iopub.execute_input":"2022-07-19T17:47:53.576936Z","iopub.status.idle":"2022-07-19T17:47:53.581839Z","shell.execute_reply.started":"2022-07-19T17:47:53.576907Z","shell.execute_reply":"2022-07-19T17:47:53.580441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\ndef outlier_detector(df,features):\n    outlier_list = []\n    for i in features:\n        q1 = df[i].quantile(0.25)\n        q3 = df[i].quantile(0.75)\n        IQR = q3 - q1\n        outlier=  IQR * 1.5\n        outlier_list_column = df[df[i] < q1 - outlier].index\n        outlier_list_column1 = df[ df[i]> outlier + q3].index\n        outlier_list.extend(outlier_list_column)\n        outlier_list.extend(outlier_list_column1)\n        \n    outlier_list = Counter(outlier_list)\n        \n    multiple_indices = [i for i,v in outlier_list.items() if v>2]\n    return multiple_indices","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.583288Z","iopub.execute_input":"2022-07-19T17:47:53.583662Z","iopub.status.idle":"2022-07-19T17:47:53.593295Z","shell.execute_reply.started":"2022-07-19T17:47:53.583616Z","shell.execute_reply":"2022-07-19T17:47:53.592091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping outlier\ntrain_d = train_d.drop(outlier_detector(train_d,['Age','Fare','FamilySize','SibSp','Parch']),axis = 0).reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.602156Z","iopub.execute_input":"2022-07-19T17:47:53.603015Z","iopub.status.idle":"2022-07-19T17:47:53.629817Z","shell.execute_reply.started":"2022-07-19T17:47:53.602983Z","shell.execute_reply":"2022-07-19T17:47:53.628824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import scale \ntrain_d['Fare'] = scale(train_d['Fare'])\ntrain_d['FamilySize'] = scale(train_d['FamilySize'])\ntrain_d['Age'] = scale(train_d['Age'])\ntrain_d['SibSp'] = scale(train_d['SibSp'])\ntrain_d['Parch'] = scale(train_d['Parch'])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.631284Z","iopub.execute_input":"2022-07-19T17:47:53.631865Z","iopub.status.idle":"2022-07-19T17:47:53.645014Z","shell.execute_reply.started":"2022-07-19T17:47:53.631822Z","shell.execute_reply":"2022-07-19T17:47:53.643851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.646801Z","iopub.execute_input":"2022-07-19T17:47:53.648023Z","iopub.status.idle":"2022-07-19T17:47:53.665802Z","shell.execute_reply.started":"2022-07-19T17:47:53.647978Z","shell.execute_reply":"2022-07-19T17:47:53.664775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,10))\nsns.heatmap(train_d.corr(),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:53.667300Z","iopub.execute_input":"2022-07-19T17:47:53.668007Z","iopub.status.idle":"2022-07-19T17:47:54.389324Z","shell.execute_reply.started":"2022-07-19T17:47:53.667975Z","shell.execute_reply":"2022-07-19T17:47:54.387910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train1 = train_d['Survived']\nx_train = train_d.drop(['Name','Survived',\"PassengerId\"],axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:54.391322Z","iopub.execute_input":"2022-07-19T17:47:54.391724Z","iopub.status.idle":"2022-07-19T17:47:54.399778Z","shell.execute_reply.started":"2022-07-19T17:47:54.391684Z","shell.execute_reply":"2022-07-19T17:47:54.398370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#trasnforming our category variables to number\nx_train1 = pd.get_dummies(x_train,drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:54.401099Z","iopub.execute_input":"2022-07-19T17:47:54.401530Z","iopub.status.idle":"2022-07-19T17:47:54.418927Z","shell.execute_reply.started":"2022-07-19T17:47:54.401499Z","shell.execute_reply":"2022-07-19T17:47:54.417802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train1","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:54.420782Z","iopub.execute_input":"2022-07-19T17:47:54.421127Z","iopub.status.idle":"2022-07-19T17:47:54.447470Z","shell.execute_reply.started":"2022-07-19T17:47:54.421097Z","shell.execute_reply":"2022-07-19T17:47:54.446086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"4\"></a>\n### Model Compare","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import GradientBoostingClassifier,RandomForestClassifier\nfrom sklearn.metrics import mean_absolute_error,mean_squared_error,accuracy_score\nx_train, x_test, y_train , y_test = train_test_split(x_train1,y_train1,test_size= 0.2,random_state = 77)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:54.449017Z","iopub.execute_input":"2022-07-19T17:47:54.449374Z","iopub.status.idle":"2022-07-19T17:47:54.673036Z","shell.execute_reply.started":"2022-07-19T17:47:54.449344Z","shell.execute_reply":"2022-07-19T17:47:54.671723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gbc = GradientBoostingClassifier(subsample= 0.8,n_estimators=300,learning_rate=0.08)\nrfc = RandomForestClassifier(bootstrap =True,\n max_depth= 110,\n max_features= 2,\n min_samples_leaf= 3,\n min_samples_split= 8,\n n_estimators= 100)\n\ngbc.fit(x_train,y_train)\ngbc_predict = gbc.predict(x_test)\nrfc.fit(x_train,y_train)\nrfc_predict = rfc.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:54.674628Z","iopub.execute_input":"2022-07-19T17:47:54.675693Z","iopub.status.idle":"2022-07-19T17:47:55.283166Z","shell.execute_reply.started":"2022-07-19T17:47:54.675657Z","shell.execute_reply":"2022-07-19T17:47:55.281992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mae = []\nmse = []\naccuracy =[]\n\n\nmse.append((mean_squared_error(gbc_predict,y_test) ** 0.5))\nmse.append((mean_squared_error(rfc_predict,y_test) ** 0.5))\nmae.append((mean_absolute_error(gbc_predict,y_test)))\nmae.append((mean_absolute_error(rfc_predict,y_test)))\naccuracy.append((accuracy_score(gbc_predict,y_test)))\naccuracy.append((accuracy_score(rfc_predict,y_test)))\n\ndf = pd.DataFrame({\n    'Models': [\"Gradient Boosting\",\"Random Forest\"],\n    'Mean Squared Error' : mse,\n    'Mean Absolute Error': mae,\n    'Accuracy' : accuracy   \n},columns = ['Models',\"Mean Squared Error\",\"Mean Absolute Error\",\"Accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.285113Z","iopub.execute_input":"2022-07-19T17:47:55.285581Z","iopub.status.idle":"2022-07-19T17:47:55.300677Z","shell.execute_reply.started":"2022-07-19T17:47:55.285533Z","shell.execute_reply":"2022-07-19T17:47:55.299559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.302247Z","iopub.execute_input":"2022-07-19T17:47:55.303114Z","iopub.status.idle":"2022-07-19T17:47:55.316325Z","shell.execute_reply.started":"2022-07-19T17:47:55.303065Z","shell.execute_reply":"2022-07-19T17:47:55.314948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"5\"></a>\n### Predicting 'test.csv'","metadata":{}},{"cell_type":"code","source":"#this funtion changing test.csv to a form which we use it on our model\ndef designer_for_test(df):\n    data = df.copy()\n    data.Embarked.fillna('C',inplace= True)\n    data[\"Title\"] = data['Name'].str.extract(' ([A-Za-z]+)\\.', expand=False)\n    data.Title = data.Title.replace(['Mlle','Lady','Ms','Countess'],['Miss','Miss','Miss','Miss'])\n    titles1 = [i for i in data.Title.unique() if  (i != 'Master') & (i != 'Miss') & (i != 'Mrs')]\n    titles2 = ['Mr' for i in range(len(titles1))]\n    data.Title = data.Title.replace(titles1,titles2)\n    data['Age'].fillna(data.groupby('Title')['Age'].transform('median'),inplace=True)\n    data.drop(['Ticket','Cabin'],axis = 1 , inplace=True)\n    data[\"FamilySize\"] = data[\"SibSp\"] + data[\"Parch\"] + 1 \n    data['Fare'].fillna(float(data['Fare'].median()),inplace =True)\n    l = KMeans(n_clusters=2)\n    grp = data[['Pclass','Fare']]\n    l.fit(grp)\n    clust =l.predict(grp)\n    data['Asset'] = clust\n    data = data.drop(['Name',\"PassengerId\"],axis =1)\n    data['Fare'] = scale(data['Fare'])\n    data['FamilySize'] = scale(data['FamilySize'])\n    data['Age'] = scale(data['Age'])\n    data['SibSp'] = scale(data['SibSp'])\n    data['Parch'] = scale(data['Parch'])\n    x_ = pd.get_dummies(data,drop_first=True)\n    survived = gbc.predict(x_)\n    df['Survived'] = survived\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.318480Z","iopub.execute_input":"2022-07-19T17:47:55.319158Z","iopub.status.idle":"2022-07-19T17:47:55.334926Z","shell.execute_reply.started":"2022-07-19T17:47:55.319111Z","shell.execute_reply":"2022-07-19T17:47:55.333804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_d.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.336386Z","iopub.execute_input":"2022-07-19T17:47:55.337346Z","iopub.status.idle":"2022-07-19T17:47:55.353777Z","shell.execute_reply.started":"2022-07-19T17:47:55.337296Z","shell.execute_reply":"2022-07-19T17:47:55.352688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = designer_for_test(test_d)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.355837Z","iopub.execute_input":"2022-07-19T17:47:55.356496Z","iopub.status.idle":"2022-07-19T17:47:55.415289Z","shell.execute_reply.started":"2022-07-19T17:47:55.356449Z","shell.execute_reply":"2022-07-19T17:47:55.413970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.sample(20)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T17:47:55.416842Z","iopub.execute_input":"2022-07-19T17:47:55.417741Z","iopub.status.idle":"2022-07-19T17:47:55.443117Z","shell.execute_reply.started":"2022-07-19T17:47:55.417707Z","shell.execute_reply":"2022-07-19T17:47:55.441829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b></b> End of project. CONGRATULATIONS!!\n</div>","metadata":{}}]}