{"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 numpy as np\nimport seaborn as sns\nfrom sklearn.impute import KNNImputer\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.naive_bayes import BernoulliNB","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:53.518729Z","iopub.execute_input":"2022-08-06T16:21:53.519613Z","iopub.status.idle":"2022-08-06T16:21:55.110513Z","shell.execute_reply.started":"2022-08-06T16:21:53.519509Z","shell.execute_reply":"2022-08-06T16:21:55.109274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's read the training set","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/titanic/train.csv')\ntrain.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:55.112649Z","iopub.execute_input":"2022-08-06T16:21:55.113072Z","iopub.status.idle":"2022-08-06T16:21:55.193336Z","shell.execute_reply.started":"2022-08-06T16:21:55.113031Z","shell.execute_reply":"2022-08-06T16:21:55.192367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Women and children first..","metadata":{}},{"cell_type":"code","source":"maleProb=train.Survived.loc[train.Sex=='male' ].mean()\nprint('male surviving probability:',maleProb)\n\nfemaleProb=train.Survived.loc[train.Sex=='female' ].mean()\nprint('female surviving probability:',femaleProb)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:55.195365Z","iopub.execute_input":"2022-08-06T16:21:55.196145Z","iopub.status.idle":"2022-08-06T16:21:55.208279Z","shell.execute_reply.started":"2022-08-06T16:21:55.196103Z","shell.execute_reply":"2022-08-06T16:21:55.206543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x=\"Sex\", y=\"Age\", hue=\"Survived\", kind=\"swarm\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:55.212982Z","iopub.execute_input":"2022-08-06T16:21:55.213951Z","iopub.status.idle":"2022-08-06T16:21:56.299467Z","shell.execute_reply.started":"2022-08-06T16:21:55.213896Z","shell.execute_reply":"2022-08-06T16:21:56.298317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Life is pay to win, poor folx could not make it","metadata":{}},{"cell_type":"code","source":"sns.catplot(x=\"Pclass\", y=\"Survived\", kind=\"bar\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:56.301463Z","iopub.execute_input":"2022-08-06T16:21:56.302222Z","iopub.status.idle":"2022-08-06T16:21:56.681942Z","shell.execute_reply.started":"2022-08-06T16:21:56.302176Z","shell.execute_reply":"2022-08-06T16:21:56.680816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you are a man and you are rich, you almost have the same chance of surviving with a poor female.","metadata":{}},{"cell_type":"code","source":"sns.catplot(x=\"Sex\", y=\"Survived\", hue='Pclass', kind=\"bar\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:56.684180Z","iopub.execute_input":"2022-08-06T16:21:56.684580Z","iopub.status.idle":"2022-08-06T16:21:57.227816Z","shell.execute_reply.started":"2022-08-06T16:21:56.684539Z","shell.execute_reply":"2022-08-06T16:21:57.226729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you have a parent or a child, you have a better chance to live because most children survived. \nFurther, if you have a lover or a sibling, you have something to live for or you are just saved by your lover while they are food to the sharx. \nBeing a loner in a ship is the worst. Not if you have fat stax of money, tho.","metadata":{}},{"cell_type":"code","source":"Families=train.Survived.loc[(train.Parch>0)].mean()\nprint('Parents&Childs:',Families)\nLovers=train.Survived.loc[(train.Parch==0)&(train.SibSp==1)].mean()\nprint('Lovers:',Lovers)\nLoners=train.Survived.loc[(train.Parch==0)].mean()\nprint('Loners:',Loners)\ntotalLoners=train.Survived.loc[(train.Parch==0)&(train.SibSp==0)].mean()\nprint('Total Loners:',totalLoners)\nrichLoners=train.Survived.loc[(train.Parch==0)&(train.SibSp==0)&(train.Pclass==1)].mean()\nprint('Rich Loners:',richLoners)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.232250Z","iopub.execute_input":"2022-08-06T16:21:57.232740Z","iopub.status.idle":"2022-08-06T16:21:57.254691Z","shell.execute_reply.started":"2022-08-06T16:21:57.232702Z","shell.execute_reply":"2022-08-06T16:21:57.253366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x=train[['Age','Sex','Parch','Pclass','SibSp']]\nprint(train_x.dtypes)\ntrain_y=train[['Survived']]\n\n#sex is object type, turned into integer\ntrain_x.Sex.loc[train_x.Sex=='male']=0\ntrain_x.Sex.loc[train_x.Sex=='female']=1\ntrain_x.Sex=train_x['Sex'].astype('str').astype(int)\n\nprint(train_x.dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.256733Z","iopub.execute_input":"2022-08-06T16:21:57.257609Z","iopub.status.idle":"2022-08-06T16:21:57.283780Z","shell.execute_reply.started":"2022-08-06T16:21:57.257564Z","shell.execute_reply":"2022-08-06T16:21:57.282782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"See if there is any missing value","metadata":{}},{"cell_type":"code","source":"missingColumns_x = [col for col in train_x.columns\n                     if train_x[col].isnull().any()]\nprint(missingColumns_x)\nmissingColumns_y = train_y.isnull().any()\nprint(missingColumns_y)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.285193Z","iopub.execute_input":"2022-08-06T16:21:57.285543Z","iopub.status.idle":"2022-08-06T16:21:57.296303Z","shell.execute_reply.started":"2022-08-06T16:21:57.285511Z","shell.execute_reply":"2022-08-06T16:21:57.295080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I am going to use age column in my model, so I will impute the missing values using KNN imputer. This imputer treats nearest neighboring values to make a prediction of the missing value. Simple imputers generally use central distribution methods such as mean, mode and median.","metadata":{}},{"cell_type":"code","source":"knn_imputer = KNNImputer(n_neighbors=4, weights=\"uniform\")\ntrain_xi=pd.DataFrame(knn_imputer.fit_transform(train_x))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.300514Z","iopub.execute_input":"2022-08-06T16:21:57.301469Z","iopub.status.idle":"2022-08-06T16:21:57.367561Z","shell.execute_reply.started":"2022-08-06T16:21:57.301424Z","shell.execute_reply":"2022-08-06T16:21:57.365639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Before I start with my model I want to inspect test data as well.","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/titanic/test.csv')\ntest.describe()\n\ntest_x=test[['Age','Sex','Parch','Pclass','SibSp']]\n#test_y=test[['Survived']]\n\n#sex is object type turned into integer\ntest_x.Sex.loc[test_x.Sex=='male']=0\ntest_x.Sex.loc[test_x.Sex=='female']=1\ntest_x.Sex=test_x['Sex'].astype('str').astype(int)\n\nprint(train_x.dtypes)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.369946Z","iopub.execute_input":"2022-08-06T16:21:57.370578Z","iopub.status.idle":"2022-08-06T16:21:57.462786Z","shell.execute_reply.started":"2022-08-06T16:21:57.370521Z","shell.execute_reply":"2022-08-06T16:21:57.458752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missingColumns_x = [col for col in test_x.columns\n                     if test_x[col].isnull().any()]\nprint(missingColumns_x)\n\nknn_imputer = KNNImputer(n_neighbors=4, weights=\"uniform\")\ntest_xi=pd.DataFrame(knn_imputer.fit_transform(test_x))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.464378Z","iopub.execute_input":"2022-08-06T16:21:57.465528Z","iopub.status.idle":"2022-08-06T16:21:57.482807Z","shell.execute_reply.started":"2022-08-06T16:21:57.465481Z","shell.execute_reply":"2022-08-06T16:21:57.481858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I can start my model with a random forest","metadata":{}},{"cell_type":"code","source":"rfmodel=RandomForestClassifier(random_state=1)\nrfmodel.fit(train_xi,train_y.values.ravel())\npredictions=rfmodel.predict(test_xi)\n\npredictions=pd.DataFrame({'PassengerId':test.PassengerId,'Survived':predictions})\n\npredictions.to_csv('predictions.csv',index=False)\npredictions\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:21:57.484514Z","iopub.execute_input":"2022-08-06T16:21:57.485398Z","iopub.status.idle":"2022-08-06T16:21:57.723513Z","shell.execute_reply.started":"2022-08-06T16:21:57.485353Z","shell.execute_reply":"2022-08-06T16:21:57.721493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets try Naive Bayes today","metadata":{}},{"cell_type":"code","source":"berno = BernoulliNB(binarize=0.7)\npredictions = berno.fit(train_xi,train_y.values.ravel()).predict(test_xi)\npredictions=pd.DataFrame({'PassengerId':test.PassengerId,'Survived':predictions})\n\npredictions.to_csv('predictions.csv',index=False)\npredictions\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:23:36.008235Z","iopub.execute_input":"2022-08-06T16:23:36.009605Z","iopub.status.idle":"2022-08-06T16:23:36.030694Z","shell.execute_reply.started":"2022-08-06T16:23:36.009549Z","shell.execute_reply":"2022-08-06T16:23:36.029497Z"},"trusted":true},"execution_count":null,"outputs":[]}]}