{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T18:07:11.520355Z","iopub.execute_input":"2022-08-01T18:07:11.521486Z","iopub.status.idle":"2022-08-01T18:07:11.545670Z","shell.execute_reply.started":"2022-08-01T18:07:11.521312Z","shell.execute_reply":"2022-08-01T18:07:11.543909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ndf2 = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:07:15.362945Z","iopub.execute_input":"2022-08-01T18:07:15.363555Z","iopub.status.idle":"2022-08-01T18:07:15.412229Z","shell.execute_reply.started":"2022-08-01T18:07:15.363507Z","shell.execute_reply":"2022-08-01T18:07:15.410932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:07:19.691025Z","iopub.execute_input":"2022-08-01T18:07:19.692093Z","iopub.status.idle":"2022-08-01T18:07:19.718147Z","shell.execute_reply.started":"2022-08-01T18:07:19.692036Z","shell.execute_reply":"2022-08-01T18:07:19.716048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf1 = df1[['PassengerId','Sex','Age','Survived']]\nboll = pd.isnull(subdf1['Age'])\nsubdf1[boll]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:07:21.570419Z","iopub.execute_input":"2022-08-01T18:07:21.570997Z","iopub.status.idle":"2022-08-01T18:07:21.597318Z","shell.execute_reply.started":"2022-08-01T18:07:21.570937Z","shell.execute_reply":"2022-08-01T18:07:21.596095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf2 = df2[['PassengerId','Sex','Age']]\nboll2 = pd.isnull(subdf2['Age'])\nsubdf2[boll2]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:07:49.920965Z","iopub.execute_input":"2022-08-01T18:07:49.921503Z","iopub.status.idle":"2022-08-01T18:07:49.945788Z","shell.execute_reply.started":"2022-08-01T18:07:49.921464Z","shell.execute_reply":"2022-08-01T18:07:49.944229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now we have to do one hot encoding to change the string values of Sex to int value **","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:09:12.511617Z","iopub.execute_input":"2022-08-01T18:09:12.512211Z","iopub.status.idle":"2022-08-01T18:09:13.173631Z","shell.execute_reply.started":"2022-08-01T18:09:12.512166Z","shell.execute_reply":"2022-08-01T18:09:13.171264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfle = subdf1\ndfle.Sex = le.fit_transform(dfle.Sex)\ndfle","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:09:17.450382Z","iopub.execute_input":"2022-08-01T18:09:17.450962Z","iopub.status.idle":"2022-08-01T18:09:17.481559Z","shell.execute_reply.started":"2022-08-01T18:09:17.450917Z","shell.execute_reply":"2022-08-01T18:09:17.480276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfle2 = subdf2\ndfle2.Sex = le.fit_transform(dfle2.Sex)\ndfle2","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:09:22.991688Z","iopub.execute_input":"2022-08-01T18:09:22.993210Z","iopub.status.idle":"2022-08-01T18:09:23.017416Z","shell.execute_reply.started":"2022-08-01T18:09:22.993137Z","shell.execute_reply":"2022-08-01T18:09:23.016266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nmean_age_train = math.floor(subdf1.Age.mean())   #this gives integer value\nmean_age_train","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:10:19.670791Z","iopub.execute_input":"2022-08-01T18:10:19.672085Z","iopub.status.idle":"2022-08-01T18:10:19.682358Z","shell.execute_reply.started":"2022-08-01T18:10:19.672014Z","shell.execute_reply":"2022-08-01T18:10:19.681270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_age_test = math.floor(subdf2.Age.mean())\nmean_age_test","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:10:28.006111Z","iopub.execute_input":"2022-08-01T18:10:28.006670Z","iopub.status.idle":"2022-08-01T18:10:28.018353Z","shell.execute_reply.started":"2022-08-01T18:10:28.006610Z","shell.execute_reply":"2022-08-01T18:10:28.016578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf1.Age = subdf1.Age.fillna(mean_age_train)\nsubdf1","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:10:42.031998Z","iopub.execute_input":"2022-08-01T18:10:42.032608Z","iopub.status.idle":"2022-08-01T18:10:42.064987Z","shell.execute_reply.started":"2022-08-01T18:10:42.032556Z","shell.execute_reply":"2022-08-01T18:10:42.062850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf2.Age = subdf2.Age.fillna(mean_age_test)\nsubdf2","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:09.932072Z","iopub.execute_input":"2022-08-01T18:11:09.932762Z","iopub.status.idle":"2022-08-01T18:11:09.958114Z","shell.execute_reply.started":"2022-08-01T18:11:09.932667Z","shell.execute_reply":"2022-08-01T18:11:09.956816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = subdf1[['PassengerId','Sex','Age']]\ny_train = subdf1[['Survived']]\nX_test = subdf2\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:19.591162Z","iopub.execute_input":"2022-08-01T18:11:19.591692Z","iopub.status.idle":"2022-08-01T18:11:19.612311Z","shell.execute_reply.started":"2022-08-01T18:11:19.591623Z","shell.execute_reply":"2022-08-01T18:11:19.610616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:20.715526Z","iopub.execute_input":"2022-08-01T18:11:20.716184Z","iopub.status.idle":"2022-08-01T18:11:20.767551Z","shell.execute_reply.started":"2022-08-01T18:11:20.716116Z","shell.execute_reply":"2022-08-01T18:11:20.766045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:22.990363Z","iopub.execute_input":"2022-08-01T18:11:22.990836Z","iopub.status.idle":"2022-08-01T18:11:22.998443Z","shell.execute_reply.started":"2022-08-01T18:11:22.990798Z","shell.execute_reply":"2022-08-01T18:11:22.996939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:25.050185Z","iopub.execute_input":"2022-08-01T18:11:25.050625Z","iopub.status.idle":"2022-08-01T18:11:25.085454Z","shell.execute_reply.started":"2022-08-01T18:11:25.050590Z","shell.execute_reply":"2022-08-01T18:11:25.084120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newtestdf = subdf2.copy()\nnewtestdf['Survived'] = model.predict(subdf2)\nnewtestdf","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:31.916624Z","iopub.execute_input":"2022-08-01T18:11:31.917088Z","iopub.status.idle":"2022-08-01T18:11:31.942397Z","shell.execute_reply.started":"2022-08-01T18:11:31.917053Z","shell.execute_reply":"2022-08-01T18:11:31.941287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = newtestdf.drop(['Survived'],axis = 'columns')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:46.116522Z","iopub.execute_input":"2022-08-01T18:11:46.117370Z","iopub.status.idle":"2022-08-01T18:11:46.129517Z","shell.execute_reply.started":"2022-08-01T18:11:46.117299Z","shell.execute_reply":"2022-08-01T18:11:46.127009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = newtestdf.Survived","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:50.170960Z","iopub.execute_input":"2022-08-01T18:11:50.171532Z","iopub.status.idle":"2022-08-01T18:11:50.179784Z","shell.execute_reply.started":"2022-08-01T18:11:50.171488Z","shell.execute_reply":"2022-08-01T18:11:50.177781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:52.730480Z","iopub.execute_input":"2022-08-01T18:11:52.731323Z","iopub.status.idle":"2022-08-01T18:11:52.743620Z","shell.execute_reply.started":"2022-08-01T18:11:52.731240Z","shell.execute_reply":"2022-08-01T18:11:52.742110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:11:56.095476Z","iopub.execute_input":"2022-08-01T18:11:56.096032Z","iopub.status.idle":"2022-08-01T18:11:56.112851Z","shell.execute_reply.started":"2022-08-01T18:11:56.095991Z","shell.execute_reply":"2022-08-01T18:11:56.111498Z"},"trusted":true},"execution_count":null,"outputs":[]}]}