{"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":"# getting the Imports required for this project\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sklearn","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:40.888192Z","iopub.execute_input":"2022-08-10T13:10:40.888597Z","iopub.status.idle":"2022-08-10T13:10:40.893642Z","shell.execute_reply.started":"2022-08-10T13:10:40.888564Z","shell.execute_reply":"2022-08-10T13:10:40.892290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_csv(\"../input/titanic/train.csv\")\ntest_data=pd.read_csv(\"../input/titanic/test.csv\")\nval_data = pd.read_csv(\"../input/titanic/gender_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:40.895658Z","iopub.execute_input":"2022-08-10T13:10:40.896405Z","iopub.status.idle":"2022-08-10T13:10:40.927248Z","shell.execute_reply.started":"2022-08-10T13:10:40.896365Z","shell.execute_reply":"2022-08-10T13:10:40.926067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:40.929654Z","iopub.execute_input":"2022-08-10T13:10:40.930621Z","iopub.status.idle":"2022-08-10T13:10:40.960176Z","shell.execute_reply.started":"2022-08-10T13:10:40.930570Z","shell.execute_reply":"2022-08-10T13:10:40.959337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## how submissions should look like\n\npd.read_csv(\"../titanic/gender_submission.csv\")\n\nPassengerId \tSurvived\n0 \t892 \t0\n1 \t893 \t1\n2 \t894 \t0\n3 \t895 \t0\n4 \t896 \t1\n... \t... \t...\n413 \t1305 \t0\n414 \t1306 \t1\n415 \t1307 \t0\n416 \t1308 \t0\n417 \t1309 \t0\n\n418 rows × 2 columns\n\n\n","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:40.961881Z","iopub.execute_input":"2022-08-10T13:10:40.962638Z","iopub.status.idle":"2022-08-10T13:10:40.990903Z","shell.execute_reply.started":"2022-08-10T13:10:40.962591Z","shell.execute_reply":"2022-08-10T13:10:40.989594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lets clean the training data first and then the test data","metadata":{}},{"cell_type":"code","source":"train_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:40.994282Z","iopub.execute_input":"2022-08-10T13:10:40.995069Z","iopub.status.idle":"2022-08-10T13:10:41.005831Z","shell.execute_reply.started":"2022-08-10T13:10:40.995017Z","shell.execute_reply":"2022-08-10T13:10:41.004612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dropping cabin as it has too many null/nans","metadata":{}},{"cell_type":"code","source":"df_train = train_data.drop(labels=['Survived', 'Cabin', 'Name', 'Ticket'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.007339Z","iopub.execute_input":"2022-08-10T13:10:41.008462Z","iopub.status.idle":"2022-08-10T13:10:41.020805Z","shell.execute_reply.started":"2022-08-10T13:10:41.008414Z","shell.execute_reply":"2022-08-10T13:10:41.019577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked = pd.get_dummies(df_train[\"Embarked\"], drop_first=True)\ndf_train=pd.concat([df_train.drop('Embarked', axis=1), embarked], axis=1)\ndf_train['Sex'] = df_train['Sex'].map({'male':0, 'female':1})","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.022122Z","iopub.execute_input":"2022-08-10T13:10:41.022930Z","iopub.status.idle":"2022-08-10T13:10:41.037200Z","shell.execute_reply.started":"2022-08-10T13:10:41.022889Z","shell.execute_reply":"2022-08-10T13:10:41.035880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.038666Z","iopub.execute_input":"2022-08-10T13:10:41.039103Z","iopub.status.idle":"2022-08-10T13:10:41.055836Z","shell.execute_reply.started":"2022-08-10T13:10:41.039067Z","shell.execute_reply":"2022-08-10T13:10:41.054599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age'] = df_train['Age'].fillna(df_train['Age'].mean())\ndf_train['Age'] = df_train['Age'].round(0).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.057491Z","iopub.execute_input":"2022-08-10T13:10:41.058496Z","iopub.status.idle":"2022-08-10T13:10:41.068674Z","shell.execute_reply.started":"2022-08-10T13:10:41.058446Z","shell.execute_reply":"2022-08-10T13:10:41.066960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.070067Z","iopub.execute_input":"2022-08-10T13:10:41.071217Z","iopub.status.idle":"2022-08-10T13:10:41.082381Z","shell.execute_reply.started":"2022-08-10T13:10:41.071163Z","shell.execute_reply":"2022-08-10T13:10:41.081324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.086557Z","iopub.execute_input":"2022-08-10T13:10:41.087336Z","iopub.status.idle":"2022-08-10T13:10:41.105539Z","shell.execute_reply.started":"2022-08-10T13:10:41.087287Z","shell.execute_reply":"2022-08-10T13:10:41.104131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data['Survived'].values","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.107041Z","iopub.execute_input":"2022-08-10T13:10:41.107905Z","iopub.status.idle":"2022-08-10T13:10:41.114573Z","shell.execute_reply.started":"2022-08-10T13:10:41.107854Z","shell.execute_reply":"2022-08-10T13:10:41.113738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = test_data.drop(['Cabin', 'Name', 'Ticket'], axis=1)\ndf_test['Survived'] = val_data['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.115718Z","iopub.execute_input":"2022-08-10T13:10:41.116400Z","iopub.status.idle":"2022-08-10T13:10:41.127636Z","shell.execute_reply.started":"2022-08-10T13:10:41.116367Z","shell.execute_reply":"2022-08-10T13:10:41.126478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.129004Z","iopub.execute_input":"2022-08-10T13:10:41.129366Z","iopub.status.idle":"2022-08-10T13:10:41.146631Z","shell.execute_reply.started":"2022-08-10T13:10:41.129327Z","shell.execute_reply":"2022-08-10T13:10:41.145477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Sex']=df_test['Sex'].map({'male':0, 'female':1})\nembarked_test = pd.get_dummies(df_test['Embarked'], drop_first=True)\ndf_test = pd.concat([df_test.drop('Embarked', axis=1), embarked_test], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.148505Z","iopub.execute_input":"2022-08-10T13:10:41.148964Z","iopub.status.idle":"2022-08-10T13:10:41.161222Z","shell.execute_reply.started":"2022-08-10T13:10:41.148911Z","shell.execute_reply":"2022-08-10T13:10:41.159847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.162974Z","iopub.execute_input":"2022-08-10T13:10:41.164086Z","iopub.status.idle":"2022-08-10T13:10:41.173832Z","shell.execute_reply.started":"2022-08-10T13:10:41.164044Z","shell.execute_reply":"2022-08-10T13:10:41.172621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Age']= df_test['Age'].fillna(df_test['Age'].mean())\ndf_test['Fare']= df_test['Fare'].fillna(df_test['Fare'].mean())\ndf_test['Age'] = df_test['Age'].round(0).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.175090Z","iopub.execute_input":"2022-08-10T13:10:41.175484Z","iopub.status.idle":"2022-08-10T13:10:41.186444Z","shell.execute_reply.started":"2022-08-10T13:10:41.175450Z","shell.execute_reply":"2022-08-10T13:10:41.185322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.187980Z","iopub.execute_input":"2022-08-10T13:10:41.188348Z","iopub.status.idle":"2022-08-10T13:10:41.208780Z","shell.execute_reply.started":"2022-08-10T13:10:41.188313Z","shell.execute_reply":"2022-08-10T13:10:41.207731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = df_test['Survived'].values","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.210692Z","iopub.execute_input":"2022-08-10T13:10:41.211405Z","iopub.status.idle":"2022-08-10T13:10:41.217130Z","shell.execute_reply.started":"2022-08-10T13:10:41.211344Z","shell.execute_reply":"2022-08-10T13:10:41.216045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = df_test.drop('Survived', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.218600Z","iopub.execute_input":"2022-08-10T13:10:41.219723Z","iopub.status.idle":"2022-08-10T13:10:41.229748Z","shell.execute_reply.started":"2022-08-10T13:10:41.219665Z","shell.execute_reply":"2022-08-10T13:10:41.228580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.231028Z","iopub.execute_input":"2022-08-10T13:10:41.231696Z","iopub.status.idle":"2022-08-10T13:10:41.243821Z","shell.execute_reply.started":"2022-08-10T13:10:41.231651Z","shell.execute_reply":"2022-08-10T13:10:41.242698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df_train\ny=y_train","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.245792Z","iopub.execute_input":"2022-08-10T13:10:41.246613Z","iopub.status.idle":"2022-08-10T13:10:41.256811Z","shell.execute_reply.started":"2022-08-10T13:10:41.246565Z","shell.execute_reply":"2022-08-10T13:10:41.255789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import LogisticRegression\n\nmode_lr = LogisticRegression(max_iter=4000)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.258500Z","iopub.execute_input":"2022-08-10T13:10:41.259614Z","iopub.status.idle":"2022-08-10T13:10:41.583155Z","shell.execute_reply.started":"2022-08-10T13:10:41.259566Z","shell.execute_reply":"2022-08-10T13:10:41.582111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mode_lr.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.584379Z","iopub.execute_input":"2022-08-10T13:10:41.585104Z","iopub.status.idle":"2022-08-10T13:10:41.680374Z","shell.execute_reply.started":"2022-08-10T13:10:41.585065Z","shell.execute_reply":"2022-08-10T13:10:41.679359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = mode_lr.predict(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.682106Z","iopub.execute_input":"2022-08-10T13:10:41.682888Z","iopub.status.idle":"2022-08-10T13:10:41.691500Z","shell.execute_reply.started":"2022-08-10T13:10:41.682838Z","shell.execute_reply":"2022-08-10T13:10:41.690256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test, pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.693159Z","iopub.execute_input":"2022-08-10T13:10:41.693864Z","iopub.status.idle":"2022-08-10T13:10:41.702180Z","shell.execute_reply.started":"2022-08-10T13:10:41.693823Z","shell.execute_reply":"2022-08-10T13:10:41.701336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.703642Z","iopub.execute_input":"2022-08-10T13:10:41.704020Z","iopub.status.idle":"2022-08-10T13:10:41.710231Z","shell.execute_reply.started":"2022-08-10T13:10:41.703987Z","shell.execute_reply":"2022-08-10T13:10:41.709283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['PassengerId'] = df_test['PassengerId']\nresults['Survived'] = pred","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.711581Z","iopub.execute_input":"2022-08-10T13:10:41.712133Z","iopub.status.idle":"2022-08-10T13:10:41.725888Z","shell.execute_reply.started":"2022-08-10T13:10:41.712101Z","shell.execute_reply":"2022-08-10T13:10:41.724653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.shape, df_train.shape, df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.731092Z","iopub.execute_input":"2022-08-10T13:10:41.731932Z","iopub.status.idle":"2022-08-10T13:10:41.738444Z","shell.execute_reply.started":"2022-08-10T13:10:41.731891Z","shell.execute_reply":"2022-08-10T13:10:41.737316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv('Titanic_submissions.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T13:10:41.740000Z","iopub.execute_input":"2022-08-10T13:10:41.740434Z","iopub.status.idle":"2022-08-10T13:10:41.751194Z","shell.execute_reply.started":"2022-08-10T13:10:41.740400Z","shell.execute_reply":"2022-08-10T13:10:41.750250Z"},"trusted":true},"execution_count":null,"outputs":[]}]}