{"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":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.impute import SimpleImputer\nfrom sklearn import preprocessing\nfrom sklearn.preprocessing import StandardScaler\nfrom scipy import stats\nfrom scipy.stats import norm\nfrom sklearn.preprocessing import LabelEncoder\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\n%matplotlib inline\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-08T00:50:04.555038Z","iopub.execute_input":"2022-07-08T00:50:04.556154Z","iopub.status.idle":"2022-07-08T00:50:04.566188Z","shell.execute_reply.started":"2022-07-08T00:50:04.556100Z","shell.execute_reply":"2022-07-08T00:50:04.565159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Reading Train Data***","metadata":{}},{"cell_type":"code","source":"Data = pd.read_csv(r'/kaggle/input/titanic/train.csv')\nData.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:04.579740Z","iopub.execute_input":"2022-07-08T00:50:04.580425Z","iopub.status.idle":"2022-07-08T00:50:04.604719Z","shell.execute_reply.started":"2022-07-08T00:50:04.580396Z","shell.execute_reply":"2022-07-08T00:50:04.603450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:04.607017Z","iopub.execute_input":"2022-07-08T00:50:04.608169Z","iopub.status.idle":"2022-07-08T00:50:04.622941Z","shell.execute_reply.started":"2022-07-08T00:50:04.608127Z","shell.execute_reply":"2022-07-08T00:50:04.621765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.describe()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:04.627500Z","iopub.execute_input":"2022-07-08T00:50:04.628402Z","iopub.status.idle":"2022-07-08T00:50:04.664702Z","shell.execute_reply.started":"2022-07-08T00:50:04.628368Z","shell.execute_reply":"2022-07-08T00:50:04.663413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Get Describtion About Object(String) Columns***","metadata":{}},{"cell_type":"code","source":"Data.describe(include=['O'])","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:04.666490Z","iopub.execute_input":"2022-07-08T00:50:04.667027Z","iopub.status.idle":"2022-07-08T00:50:04.695284Z","shell.execute_reply.started":"2022-07-08T00:50:04.666963Z","shell.execute_reply":"2022-07-08T00:50:04.694268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Correlation Between Columns","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10, 10))\nsns.heatmap(Data.corr(), annot = True,cmap= 'Blues')#, fmt='.6g'","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:53.877351Z","iopub.execute_input":"2022-07-08T00:50:53.877918Z","iopub.status.idle":"2022-07-08T00:50:54.630078Z","shell.execute_reply.started":"2022-07-08T00:50:53.877880Z","shell.execute_reply":"2022-07-08T00:50:54.629135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in Data.select_dtypes(exclude ='object').columns:\n    print(i,' relation = ',Data[i].corr(Data['Survived']))","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:05.191081Z","iopub.execute_input":"2022-07-08T00:50:05.191711Z","iopub.status.idle":"2022-07-08T00:50:05.204094Z","shell.execute_reply.started":"2022-07-08T00:50:05.191669Z","shell.execute_reply":"2022-07-08T00:50:05.202892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"####  Here Graph Show That 0.54 of Passenger in Group 1 Had Survived & 0.47 of Passenger in Group 2 Had Survived ...etc","metadata":{}},{"cell_type":"code","source":"sns.barplot(x ='SibSp', y ='Survived', data = Data,\n            palette ='plasma')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:05.207266Z","iopub.execute_input":"2022-07-08T00:50:05.207623Z","iopub.status.idle":"2022-07-08T00:50:05.657708Z","shell.execute_reply.started":"2022-07-08T00:50:05.207593Z","shell.execute_reply":"2022-07-08T00:50:05.655717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Here We Get The exact percentage of those rescued ","metadata":{}},{"cell_type":"code","source":"Data[['SibSp','Survived']].groupby(['SibSp'],as_index=False).mean().sort_values(by='Survived',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:05.663547Z","iopub.execute_input":"2022-07-08T00:50:05.664247Z","iopub.status.idle":"2022-07-08T00:50:05.694493Z","shell.execute_reply.started":"2022-07-08T00:50:05.664184Z","shell.execute_reply":"2022-07-08T00:50:05.693291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"####  Here Graph Show That 0.6 of Passenger in Group 3 Had Survived & 0.5 of Passenger in Group 1 Had Survived ...etc","metadata":{}},{"cell_type":"code","source":"sns.barplot(x ='Parch', y ='Survived', data = Data,\n            palette ='plasma')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:05.696296Z","iopub.execute_input":"2022-07-08T00:50:05.697126Z","iopub.status.idle":"2022-07-08T00:50:06.275138Z","shell.execute_reply.started":"2022-07-08T00:50:05.697085Z","shell.execute_reply":"2022-07-08T00:50:06.268896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data[['Parch','Survived']].groupby(['Parch'],as_index=False).mean().sort_values(by='Survived',ascending=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:06.277193Z","iopub.execute_input":"2022-07-08T00:50:06.277708Z","iopub.status.idle":"2022-07-08T00:50:06.297100Z","shell.execute_reply.started":"2022-07-08T00:50:06.277571Z","shell.execute_reply":"2022-07-08T00:50:06.295804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Graph showing that most of the deaths were men and most of the women were rescued","metadata":{}},{"cell_type":"code","source":"sns.violinplot(x ='Sex', y ='Survived', data = Data)\nsns.swarmplot(x ='Sex', y ='Survived', data = Data, color ='black')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:06.299198Z","iopub.execute_input":"2022-07-08T00:50:06.299664Z","iopub.status.idle":"2022-07-08T00:50:18.254317Z","shell.execute_reply.started":"2022-07-08T00:50:06.299624Z","shell.execute_reply":"2022-07-08T00:50:18.253237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncondition_women = Data['Survived'][(Data['Survived'] == 1) & (Data['Sex'] == 'female')]\ncondition_men = Data['Survived'][(Data['Survived'] == 1) & (Data['Sex'] == 'male')]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.259470Z","iopub.execute_input":"2022-07-08T00:50:18.260274Z","iopub.status.idle":"2022-07-08T00:50:18.268361Z","shell.execute_reply.started":"2022-07-08T00:50:18.260235Z","shell.execute_reply":"2022-07-08T00:50:18.267249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(condition.count()/len(Data['Sex'][Data['Sex']=='female']))\nprint('percentage of men That had Survived = {:.2f}%'.format( condition_men.count()/list(Data['Sex']).count('male')*100 )) \nprint('percentage of Women That had Survived = {:.2f}%'.format( condition_women.count()/list(Data['Sex']).count('female')*100 )) \n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.271023Z","iopub.execute_input":"2022-07-08T00:50:18.271459Z","iopub.status.idle":"2022-07-08T00:50:18.281032Z","shell.execute_reply.started":"2022-07-08T00:50:18.271419Z","shell.execute_reply":"2022-07-08T00:50:18.279895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.select_dtypes(exclude='object').columns\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.283002Z","iopub.execute_input":"2022-07-08T00:50:18.283519Z","iopub.status.idle":"2022-07-08T00:50:18.295418Z","shell.execute_reply.started":"2022-07-08T00:50:18.283474Z","shell.execute_reply":"2022-07-08T00:50:18.294491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent That The Maximun number of Passengers isn't Survived\n### Second Graph represent that and specific The number of Unique Values in this Column is 2( 0 ~ not Survived , 1 ~ Survived )","metadata":{}},{"cell_type":"code","source":"sns.countplot(Data['Survived'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.296521Z","iopub.execute_input":"2022-07-08T00:50:18.297309Z","iopub.status.idle":"2022-07-08T00:50:18.437926Z","shell.execute_reply.started":"2022-07-08T00:50:18.297195Z","shell.execute_reply":"2022-07-08T00:50:18.436776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(Data['Survived'], fit=norm);\nfig = plt.figure()\nres = stats.probplot(Data['Survived'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.443387Z","iopub.execute_input":"2022-07-08T00:50:18.446343Z","iopub.status.idle":"2022-07-08T00:50:18.836455Z","shell.execute_reply.started":"2022-07-08T00:50:18.446294Z","shell.execute_reply":"2022-07-08T00:50:18.835271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent That The Maximun number of Passengers  is Followed 3 Group\n### Second Graph represent that and specific The number of Unique Values in this Column is 3 ( 1,2,3 )","metadata":{}},{"cell_type":"code","source":"sns.distplot(Data['Pclass'], fit=norm)\nfig = plt.figure()\nres = stats.probplot(Data['Pclass'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:18.838315Z","iopub.execute_input":"2022-07-08T00:50:18.839028Z","iopub.status.idle":"2022-07-08T00:50:19.230104Z","shell.execute_reply.started":"2022-07-08T00:50:18.838965Z","shell.execute_reply":"2022-07-08T00:50:19.229151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The Graph Show That The Maximum Number Of Deaths Was in Group 3 \n### And The Maximum Number Of Survived was in Group 1","metadata":{}},{"cell_type":"code","source":"sns.violinplot(x ='Pclass', y ='Survived', data = Data)\nsns.swarmplot(x ='Pclass', y ='Survived', data = Data, color ='black')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:19.232217Z","iopub.execute_input":"2022-07-08T00:50:19.232752Z","iopub.status.idle":"2022-07-08T00:50:26.475357Z","shell.execute_reply.started":"2022-07-08T00:50:19.232714Z","shell.execute_reply":"2022-07-08T00:50:26.474210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent The Maximun number of Passengers is between 20 to 40 and Many BaBies \n#### Second Graph represent that and  Show That it has Different Ages","metadata":{}},{"cell_type":"code","source":"sns.distplot(Data['Age'], fit=norm);\nfig = plt.figure()\nres = stats.probplot(Data['Age'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:26.477666Z","iopub.execute_input":"2022-07-08T00:50:26.478426Z","iopub.status.idle":"2022-07-08T00:50:27.024791Z","shell.execute_reply.started":"2022-07-08T00:50:26.478385Z","shell.execute_reply":"2022-07-08T00:50:27.023862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent The Maximun number of Passengers is Followed 0 Group\n### Second Graph represent that and specific The number of Unique Values in this Column is 7","metadata":{}},{"cell_type":"code","source":"sns.distplot(Data['SibSp'], fit=norm)\nfig = plt.figure()\nres = stats.probplot(Data['SibSp'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:27.029293Z","iopub.execute_input":"2022-07-08T00:50:27.031692Z","iopub.status.idle":"2022-07-08T00:50:27.807972Z","shell.execute_reply.started":"2022-07-08T00:50:27.031652Z","shell.execute_reply":"2022-07-08T00:50:27.806139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent The Maximun number of Passengers is Followed 0 Group\n### Second Graph represent that and specific The number of Unique Values in this Column is 7","metadata":{}},{"cell_type":"code","source":"sns.distplot(Data['Parch'], fit=norm);\nfig = plt.figure()\nres = stats.probplot(Data['Parch'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:27.809463Z","iopub.execute_input":"2022-07-08T00:50:27.809899Z","iopub.status.idle":"2022-07-08T00:50:28.775228Z","shell.execute_reply.started":"2022-07-08T00:50:27.809845Z","shell.execute_reply":"2022-07-08T00:50:28.774189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Graph Represent The Maximun number of Passengers is Followed 0 Group\n#### Second Graph represent that and Show That it has Different Values","metadata":{}},{"cell_type":"code","source":"sns.distplot(Data['Fare'], fit=norm)\nfig = plt.figure()\nres = stats.probplot(Data['Fare'], plot=plt)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:28.776834Z","iopub.execute_input":"2022-07-08T00:50:28.777474Z","iopub.status.idle":"2022-07-08T00:50:29.275077Z","shell.execute_reply.started":"2022-07-08T00:50:28.777434Z","shell.execute_reply":"2022-07-08T00:50:29.274141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.select_dtypes(exclude='object').columns","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:29.277129Z","iopub.execute_input":"2022-07-08T00:50:29.277952Z","iopub.status.idle":"2022-07-08T00:50:29.287760Z","shell.execute_reply.started":"2022-07-08T00:50:29.277907Z","shell.execute_reply":"2022-07-08T00:50:29.286584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### This Graph Show That The relation and Rate Of Four Columns (Sex,Survived,Pclass,Age)\n##### for Example First Graph(upper left) the Second Bar show that Men Had 30 years old in PClass 2 died \n##### and the Second Graph (Upper right) the Second Bar Show That Men Had 0:15 years old in PClass 2 Survived ...etc","metadata":{}},{"cell_type":"code","source":"grid = sns.FacetGrid(Data, row='Sex', col='Survived', size=2.2, aspect=1.6)\ngrid.map(sns.barplot, 'Pclass','Age', alpha=.9).add_legend()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:29.289583Z","iopub.execute_input":"2022-07-08T00:50:29.290072Z","iopub.status.idle":"2022-07-08T00:50:30.306251Z","shell.execute_reply.started":"2022-07-08T00:50:29.290038Z","shell.execute_reply":"2022-07-08T00:50:30.305187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The Graph Show That The Maximum Number Of Deaths Was in Group 0 and 4\n#### And The Maximum Number Of Survived was in Group 1 ","metadata":{}},{"cell_type":"code","source":"sns.violinplot(x ='SibSp', y ='Survived', data = Data)\nsns.swarmplot(x ='SibSp', y ='Survived', data = Data, color ='black')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:30.308045Z","iopub.execute_input":"2022-07-08T00:50:30.309087Z","iopub.status.idle":"2022-07-08T00:50:39.571325Z","shell.execute_reply.started":"2022-07-08T00:50:30.309046Z","shell.execute_reply":"2022-07-08T00:50:39.570183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data['Age'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.572836Z","iopub.execute_input":"2022-07-08T00:50:39.573233Z","iopub.status.idle":"2022-07-08T00:50:39.586423Z","shell.execute_reply.started":"2022-07-08T00:50:39.573179Z","shell.execute_reply":"2022-07-08T00:50:39.585291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split Column Ticket to Type And Number As That They Are Types Of Tickets Make all Passenger Survived Who Have These Types like Type (SC ,SO/C ..) \n#### Type and Number Have Correlation with Output Column (Survived) It  Will Effect on Output","metadata":{}},{"cell_type":"code","source":"n=[]\ns=[]\nfor i in Data['Ticket']:\n    if (i.__contains__(' ')):\n        s.append(i.split(\" \")[0])\n        n.append(i.split(\" \")[1])\n        \n    else:     \n        n.append(i)\n        s.append(np.nan)\nData['Ticket_Type'] = s   \nData['Ticket_Number'] = n\n\nData[['Ticket_Type','Survived']].groupby(['Ticket_Type'],as_index=False).mean().sort_values(by='Survived',ascending=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.592778Z","iopub.execute_input":"2022-07-08T00:50:39.593089Z","iopub.status.idle":"2022-07-08T00:50:39.618627Z","shell.execute_reply.started":"2022-07-08T00:50:39.593064Z","shell.execute_reply":"2022-07-08T00:50:39.617586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### We Have A Null Values in Column Age We Convert it By The Describe Of Age 25% of Data less Than  20.125 And 50% of Data less Than 38 ... etc And Convert Null Values to 4 ","metadata":{}},{"cell_type":"code","source":"Ag=[]\nfor i in Data['Age']:\n    if i <=20.125 :\n        Ag.append(0)\n    elif i <= 28.0:\n        Ag.append(1)\n    elif i <= 38.0:\n        Ag.append(2)\n    elif i <= 80.0:\n        Ag.append(3)    \n    else : \n        Ag.append(4)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.620052Z","iopub.execute_input":"2022-07-08T00:50:39.620905Z","iopub.status.idle":"2022-07-08T00:50:39.628732Z","shell.execute_reply.started":"2022-07-08T00:50:39.620864Z","shell.execute_reply":"2022-07-08T00:50:39.627681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Get From Column Name the NicK_Name As That They Are Nick_Name Make all Passenger Survived Who Have These Nick_Name like Type (Mlle ,MS ..) ...It Have Correlation with Output Column (Survived) It  Will Effect on Output","metadata":{}},{"cell_type":"code","source":"N=[]\nfor i in Data['Name']:\n    i=i.split(' ')[1]\n    N.append(i.split('.')[0])\n    \nData['Nick_Name'] =N ","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.630175Z","iopub.execute_input":"2022-07-08T00:50:39.630928Z","iopub.status.idle":"2022-07-08T00:50:39.641591Z","shell.execute_reply.started":"2022-07-08T00:50:39.630876Z","shell.execute_reply":"2022-07-08T00:50:39.640637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data[['Nick_Name','Survived']].groupby(['Nick_Name'],as_index=False).mean().sort_values(by='Survived',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.643262Z","iopub.execute_input":"2022-07-08T00:50:39.644319Z","iopub.status.idle":"2022-07-08T00:50:39.666000Z","shell.execute_reply.started":"2022-07-08T00:50:39.644280Z","shell.execute_reply":"2022-07-08T00:50:39.664927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Remove All Columns That has Non relation With Output","metadata":{}},{"cell_type":"code","source":"Data.drop(columns=['PassengerId','Name', 'Ticket','Age'], inplace=True)\nData['Ag']=Ag","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.667502Z","iopub.execute_input":"2022-07-08T00:50:39.668402Z","iopub.status.idle":"2022-07-08T00:50:39.676364Z","shell.execute_reply.started":"2022-07-08T00:50:39.668363Z","shell.execute_reply":"2022-07-08T00:50:39.675166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Doing LabelEncoder To Solve String Columns  problem And Convert Them To Int Columns","metadata":{}},{"cell_type":"code","source":"def label_encode_columns(df, columns):\n    encoders = {}\n    for col in columns:\n        le = LabelEncoder().fit(df[col])\n        df[col] = le.transform(df[col])\n        encoders[col] = le\n    return df, encoders","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.677893Z","iopub.execute_input":"2022-07-08T00:50:39.678567Z","iopub.status.idle":"2022-07-08T00:50:39.687226Z","shell.execute_reply.started":"2022-07-08T00:50:39.678428Z","shell.execute_reply":"2022-07-08T00:50:39.686178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encode_columns = list(Data.select_dtypes(['object']).columns)\nNew_Data, encoders = label_encode_columns(df=Data, columns=encode_columns)\n\n\nprint('Updates dataframe is : \\n' ,New_Data.info() )","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.688735Z","iopub.execute_input":"2022-07-08T00:50:39.689826Z","iopub.status.idle":"2022-07-08T00:50:39.719188Z","shell.execute_reply.started":"2022-07-08T00:50:39.689786Z","shell.execute_reply":"2022-07-08T00:50:39.717930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"New_Data","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.721081Z","iopub.execute_input":"2022-07-08T00:50:39.721797Z","iopub.status.idle":"2022-07-08T00:50:39.745115Z","shell.execute_reply.started":"2022-07-08T00:50:39.721754Z","shell.execute_reply":"2022-07-08T00:50:39.743800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=New_Data['Survived']\nNew_Data.drop(columns=['Survived'], inplace=True)\nX=New_Data","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.747412Z","iopub.execute_input":"2022-07-08T00:50:39.748147Z","iopub.status.idle":"2022-07-08T00:50:39.755989Z","shell.execute_reply.started":"2022-07-08T00:50:39.748103Z","shell.execute_reply":"2022-07-08T00:50:39.754413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing All Previous Data Preprocessing On Test Data","metadata":{}},{"cell_type":"markdown","source":"***Reading Test Data***","metadata":{}},{"cell_type":"code","source":"Test_Data = pd.read_csv(r'/kaggle/input/titanic/test.csv')\nTest_Data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.757716Z","iopub.execute_input":"2022-07-08T00:50:39.758616Z","iopub.status.idle":"2022-07-08T00:50:39.785906Z","shell.execute_reply.started":"2022-07-08T00:50:39.758566Z","shell.execute_reply":"2022-07-08T00:50:39.784976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n=[]\ns=[]\nfor i in Test_Data['Ticket']:\n    if (i.__contains__(' ')):\n        s.append(i.split(\" \")[0])\n        n.append(i.split(\" \")[1])\n        \n    else:     \n        n.append(i)\n        s.append(np.nan)\nTest_Data['Ticket_Type'] = s   \nTest_Data['Ticket_Number'] = n\n\nN=[]\nfor i in Test_Data['Name']:\n    i=i.split(' ')[1]\n    N.append(i.split('.')[0])\n    \nAg=[]\nfor i in Test_Data['Age']:\n    if i <=20.125 :\n        Ag.append(0)\n    elif i <= 28.0:\n        Ag.append(1)\n    elif i <= 38.0:\n        Ag.append(2) \n    elif i <= 80.0:\n        Ag.append(3)    \n    else : \n        Ag.append(4)\n        \nTest_Data['Ag'] =Ag    \nTest_Data['Nick_Name'] =N   \nTest_Data.drop(columns=['PassengerId','Name', 'Ticket','Age'], inplace=True)\n\n\nencode_columns = list(Test_Data.select_dtypes(['object']).columns)\nTest_Data, encoders = label_encode_columns(df=Test_Data, columns=encode_columns)\n\nimp = SimpleImputer(missing_values=np.NAN, strategy='mean')\nimp = imp.fit(Test_Data)\n\n\nTest_Data = imp.transform(Test_Data)\nTest_Data= np.reshape(Test_Data,(len(Test_Data),len(Test_Data[0])))\n\nX_Test = pd.DataFrame(Test_Data)\n\nprint('Updates dataframe is : \\n' ,X_Test.info() )\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.787447Z","iopub.execute_input":"2022-07-08T00:50:39.788211Z","iopub.status.idle":"2022-07-08T00:50:39.825102Z","shell.execute_reply.started":"2022-07-08T00:50:39.788171Z","shell.execute_reply":"2022-07-08T00:50:39.823978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Reading gender_submission Data***","metadata":{}},{"cell_type":"code","source":"VD = pd.read_csv(r'/kaggle/input/titanic/gender_submission.csv')\nVD.head(5)\nVD=VD['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.826575Z","iopub.execute_input":"2022-07-08T00:50:39.828778Z","iopub.status.idle":"2022-07-08T00:50:39.837364Z","shell.execute_reply.started":"2022-07-08T00:50:39.828712Z","shell.execute_reply":"2022-07-08T00:50:39.836265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X\nY_train=y\nX_test=X_Test","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.840101Z","iopub.execute_input":"2022-07-08T00:50:39.840872Z","iopub.status.idle":"2022-07-08T00:50:39.847241Z","shell.execute_reply.started":"2022-07-08T00:50:39.840831Z","shell.execute_reply":"2022-07-08T00:50:39.845660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing Machine Learning Models\n# Score = 95.5 %","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import  LinearSVC\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.849882Z","iopub.execute_input":"2022-07-08T00:50:39.850706Z","iopub.status.idle":"2022-07-08T00:50:39.858244Z","shell.execute_reply.started":"2022-07-08T00:50:39.850649Z","shell.execute_reply":"2022-07-08T00:50:39.857274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg = LogisticRegression(random_state=False,penalty='l2', solver= 'newton-cg')\nlogreg.fit(X_train, Y_train)\nY_pred = logreg.predict(X_test)\nacc_log = round(logreg.score(X_train, Y_train) * 100, 2)\nacc_log\nprint('Best Score is :',acc_log)\nVdD_predict=logreg.predict(X_Test)\ncm = confusion_matrix(VD, VdD_predict)\nprint('confusion matrix = \\n',cm)\nsns.heatmap(cm, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:39.860045Z","iopub.execute_input":"2022-07-08T00:50:39.860480Z","iopub.status.idle":"2022-07-08T00:50:40.234204Z","shell.execute_reply.started":"2022-07-08T00:50:39.860445Z","shell.execute_reply":"2022-07-08T00:50:40.233188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest = RandomForestClassifier(n_estimators=50,random_state=False,max_depth=10)\nrandom_forest.fit(X_train, Y_train)\nY_pred = random_forest.predict(X_test)\nrandom_forest.score(X_train, Y_train)\nacc_random_forest = round(random_forest.score(X_train, Y_train) * 100, 2)\nacc_random_forest\nprint('Best Score is :',acc_random_forest)\nVD_predict=random_forest.predict(X_Test)\ncm = confusion_matrix(VD, VD_predict)\nprint('confusion matrix = \\n',cm)\nsns.heatmap(cm, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:40.235763Z","iopub.execute_input":"2022-07-08T00:50:40.236803Z","iopub.status.idle":"2022-07-08T00:50:40.590142Z","shell.execute_reply.started":"2022-07-08T00:50:40.236765Z","shell.execute_reply":"2022-07-08T00:50:40.589110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndecision_tree = DecisionTreeClassifier(random_state=False)\ndecision_tree.fit(X_train, Y_train)\nY_pred = decision_tree.predict(X_test)\nacc_decision_tree = round(decision_tree.score(X_train, Y_train) * 100, 2)\nacc_decision_tree\nprint('Best Score is :',acc_decision_tree)\nVD_predict=decision_tree.predict(X_Test)\ncm = confusion_matrix(VD, VD_predict)\nprint('confusion matrix = \\n',cm)\nsns.heatmap(cm, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:40.591544Z","iopub.execute_input":"2022-07-08T00:50:40.592011Z","iopub.status.idle":"2022-07-08T00:50:40.820195Z","shell.execute_reply.started":"2022-07-08T00:50:40.591971Z","shell.execute_reply":"2022-07-08T00:50:40.819106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 3)\nknn.fit(X_train, Y_train)\nY_pred = knn.predict(X_test)\nacc_knn = round(knn.score(X_train, Y_train) * 100, 2)\nacc_knn\nprint('Best Score is :',acc_knn)\nVD_predict=knn.predict(X_Test)\ncm = confusion_matrix(VD, VD_predict)\nprint('confusion matrix = \\n',cm)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:40.821662Z","iopub.execute_input":"2022-07-08T00:50:40.822473Z","iopub.status.idle":"2022-07-08T00:50:40.903825Z","shell.execute_reply.started":"2022-07-08T00:50:40.822429Z","shell.execute_reply":"2022-07-08T00:50:40.902594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear_svc = LinearSVC(random_state=False)\nlinear_svc.fit(X_train, Y_train)\nY_pred = linear_svc.predict(X_test)\nacc_linear_svc = round(linear_svc.score(X_train, Y_train) * 100, 2)\nacc_linear_svc\nprint('Best Score is :',acc_linear_svc)\nVD_predict=linear_svc.predict(X_Test)\ncm = confusion_matrix(VD, VD_predict)\nprint('confusion matrix = \\n',cm)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:40.905430Z","iopub.execute_input":"2022-07-08T00:50:40.905973Z","iopub.status.idle":"2022-07-08T00:50:40.970668Z","shell.execute_reply.started":"2022-07-08T00:50:40.905935Z","shell.execute_reply":"2022-07-08T00:50:40.969480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGDClassifier()\nsgd.fit(X_train, Y_train)\nY_pred = sgd.predict(X_test)\nacc_sgd = round(sgd.score(X_train, Y_train) * 100, 2)\nacc_sgd\nprint('Best Score is :',acc_sgd)\nVD_predict=sgd.predict(X_Test)\ncm = confusion_matrix(VD, VD_predict)\nprint('confusion matrix = \\n',cm)\nsns.heatmap(cm, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:50:40.972319Z","iopub.execute_input":"2022-07-08T00:50:40.973060Z","iopub.status.idle":"2022-07-08T00:50:41.228360Z","shell.execute_reply.started":"2022-07-08T00:50:40.973012Z","shell.execute_reply":"2022-07-08T00:50:41.227266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import tree\n\nfn=New_Data.columns\nfig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (20,18), dpi=800)\ntree.plot_tree(random_forest.estimators_[0],\n               feature_names = fn, \n               class_names='Survived',\n               filled = True, fontsize=7.5);","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:57:08.122652Z","iopub.execute_input":"2022-07-08T00:57:08.123028Z","iopub.status.idle":"2022-07-08T00:57:30.775409Z","shell.execute_reply.started":"2022-07-08T00:57:08.122998Z","shell.execute_reply":"2022-07-08T00:57:30.774159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# You Can Use GridSearchCV IF You Want And The Code below Run Without Any Problem Just Remove Comment From Code 😊 ","metadata":{}},{"cell_type":"code","source":"'''\nparams=[{'solver':('lbfgs','newton-cg','saga'),'penalty':('l2','l1','elasticnet')},\n        {'penalty':('l2','l1')},\n        {'max_depth':[3,5,10,15]},\n        {'algorithm':('auto', 'ball_tree', 'kd_tree', 'brute')},\n        {'alpha': [0.1,0.0001,1],'penalty':('l2','l1','elasticnet')},\n        {'max_depth':[3,5,10,15]}]\n\n\nLR = LogisticRegression(n_jobs=-1)\nLS = LinearSVC(random_state=False)\nRFR = RandomForestClassifier(n_estimators=100,random_state=False)\nKC = KNeighborsClassifier(n_neighbors = 5,weights= 'uniform')\nSC = SGDClassifier(shuffle=True,n_jobs=-1,random_state=False)\nDTC = DecisionTreeClassifier(random_state=False)\n\nmodels=[LR,LS,RFR,KC,SC,DTC]\n\n\nfor model,param in zip(models,params):\n    print('Model is ',model)\n    for i in range (3,11):\n        GridSearchModel = GridSearchCV(model,param, cv = i,return_train_score=True, n_jobs=-1)\n        GridSearchModel.fit(X, y)\n        VD_predict=GridSearchModel.predict(X_Test)\n        cm = confusion_matrix(VD, VD_predict)\n        sorted(GridSearchModel.cv_results_.keys())\n        print('CV = ',i)\n        print('Best Score is :', GridSearchModel.best_score_)\n        print('Best Parameters are :', GridSearchModel.best_params_)\n        print('confusion matrix = ')\n        print(cm)\n        print('-------------------------------')\n    print('\\t\\t*****************************************************************')\n\n'''","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:26:52.286403Z","iopub.execute_input":"2022-07-08T00:26:52.287135Z","iopub.status.idle":"2022-07-08T00:26:52.29596Z","shell.execute_reply.started":"2022-07-08T00:26:52.287081Z","shell.execute_reply":"2022-07-08T00:26:52.294841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-08T00:38:43.35077Z","iopub.execute_input":"2022-07-08T00:38:43.351287Z","iopub.status.idle":"2022-07-08T00:38:43.361881Z","shell.execute_reply.started":"2022-07-08T00:38:43.351193Z","shell.execute_reply":"2022-07-08T00:38:43.360826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/titanic/gender_submission.csv')\nsub['Survived'] = list(map(int, VdD_predict))\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T01:20:48.51853Z","iopub.execute_input":"2022-07-03T01:20:48.519133Z","iopub.status.idle":"2022-07-03T01:20:48.530081Z","shell.execute_reply.started":"2022-07-03T01:20:48.519096Z","shell.execute_reply":"2022-07-03T01:20:48.528988Z"},"trusted":true},"execution_count":null,"outputs":[]}]}