{"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 matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn import linear_model\nfrom sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.673379Z","iopub.execute_input":"2022-07-19T19:54:22.674099Z","iopub.status.idle":"2022-07-19T19:54:22.680711Z","shell.execute_reply.started":"2022-07-19T19:54:22.674002Z","shell.execute_reply":"2022-07-19T19:54:22.679232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convering the train and test data into a pandas dataframe\nTrain = pd.read_csv(r\"/kaggle/input/titanic/train.csv\")\nTest = pd.read_csv(\"/kaggle/input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.688216Z","iopub.execute_input":"2022-07-19T19:54:22.688623Z","iopub.status.idle":"2022-07-19T19:54:22.710500Z","shell.execute_reply.started":"2022-07-19T19:54:22.688590Z","shell.execute_reply":"2022-07-19T19:54:22.709416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Training data\")\nprint(Train.info())\nprint(\"Test data\")\nprint(Test.info())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.712713Z","iopub.execute_input":"2022-07-19T19:54:22.713931Z","iopub.status.idle":"2022-07-19T19:54:22.752653Z","shell.execute_reply.started":"2022-07-19T19:54:22.713889Z","shell.execute_reply":"2022-07-19T19:54:22.751162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.754500Z","iopub.execute_input":"2022-07-19T19:54:22.754998Z","iopub.status.idle":"2022-07-19T19:54:22.791322Z","shell.execute_reply.started":"2022-07-19T19:54:22.754951Z","shell.execute_reply":"2022-07-19T19:54:22.790013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Test","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.793046Z","iopub.execute_input":"2022-07-19T19:54:22.793437Z","iopub.status.idle":"2022-07-19T19:54:22.818324Z","shell.execute_reply.started":"2022-07-19T19:54:22.793404Z","shell.execute_reply":"2022-07-19T19:54:22.817277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drawing insights from the data\n# how many people survived?\nsns.countplot(x='Survived',data=Train)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:22.820844Z","iopub.execute_input":"2022-07-19T19:54:22.821599Z","iopub.status.idle":"2022-07-19T19:54:23.031928Z","shell.execute_reply.started":"2022-07-19T19:54:22.821558Z","shell.execute_reply":"2022-07-19T19:54:23.030702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# how many females and males survived and how many did not \nsns.countplot(x='Survived',data=Train,hue='Sex')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.033536Z","iopub.execute_input":"2022-07-19T19:54:23.034033Z","iopub.status.idle":"2022-07-19T19:54:23.230889Z","shell.execute_reply.started":"2022-07-19T19:54:23.033985Z","shell.execute_reply":"2022-07-19T19:54:23.229964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking for null values\nTrain.isna()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.232111Z","iopub.execute_input":"2022-07-19T19:54:23.233190Z","iopub.status.idle":"2022-07-19T19:54:23.259398Z","shell.execute_reply.started":"2022-07-19T19:54:23.233148Z","shell.execute_reply":"2022-07-19T19:54:23.258236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# age distribution for all the passengers\nsns.displot(x=\"Age\",data=Train)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.261777Z","iopub.execute_input":"2022-07-19T19:54:23.262658Z","iopub.status.idle":"2022-07-19T19:54:23.611824Z","shell.execute_reply.started":"2022-07-19T19:54:23.262606Z","shell.execute_reply":"2022-07-19T19:54:23.610662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for how many null values there are in each column\nTrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.613329Z","iopub.execute_input":"2022-07-19T19:54:23.613697Z","iopub.status.idle":"2022-07-19T19:54:23.625312Z","shell.execute_reply.started":"2022-07-19T19:54:23.613666Z","shell.execute_reply":"2022-07-19T19:54:23.624078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# null values in the test data set\nTest.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.627754Z","iopub.execute_input":"2022-07-19T19:54:23.628318Z","iopub.status.idle":"2022-07-19T19:54:23.639685Z","shell.execute_reply.started":"2022-07-19T19:54:23.628256Z","shell.execute_reply":"2022-07-19T19:54:23.638694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize the null values\nsns.heatmap(Train.isna())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:23.644407Z","iopub.execute_input":"2022-07-19T19:54:23.645318Z","iopub.status.idle":"2022-07-19T19:54:24.069263Z","shell.execute_reply.started":"2022-07-19T19:54:23.645281Z","shell.execute_reply":"2022-07-19T19:54:24.067780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(Test.isna())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.071061Z","iopub.execute_input":"2022-07-19T19:54:24.071546Z","iopub.status.idle":"2022-07-19T19:54:24.477067Z","shell.execute_reply.started":"2022-07-19T19:54:24.071497Z","shell.execute_reply":"2022-07-19T19:54:24.475626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# percentage of null values in the three columns in training data\nprint(\"Null values in Age column:\",(Train['Age'].isna().sum()/len(Train['Age']))*100,'percent')\nprint(\"Null values in Cabin column:\",(Train['Cabin'].isna().sum()/len(Train['Cabin']))*100,\"percent\")\nprint(\"Null values in Embarked column:\",(Train['Embarked'].isna().sum()/len(Train['Embarked']))*100,\"percent\")\n# percentage of null values in the three columns in test data\nprint(\"Null values in Age column:\",(Test['Age'].isna().sum()/len(Test['Age']))*100,'percent')\nprint(\"Null values in Cabin column:\",(Test['Cabin'].isna().sum()/len(Test['Cabin']))*100,\"percent\")\nprint(\"Null values in Fare column:\",(Test['Fare'].isna().sum()/len(Test['Fare']))*100,\"percent\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.478847Z","iopub.execute_input":"2022-07-19T19:54:24.479346Z","iopub.status.idle":"2022-07-19T19:54:24.492889Z","shell.execute_reply.started":"2022-07-19T19:54:24.479305Z","shell.execute_reply":"2022-07-19T19:54:24.491824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill null values in the age column in the training\nTrain[\"Age\"].fillna(Train[\"Age\"].mean(),inplace=True)\nprint(\"Number of null values in Training Age column:\",Train[\"Age\"].isna().sum())\n# fill null values in the age column in the test data\nTest[\"Age\"].fillna(Test[\"Age\"].mean(),inplace=True)\nprint(\"Number of null values in Testing Age column:\",Test[\"Age\"].isna().sum())\n# fill null values in the fare column in the test data\nTest[\"Fare\"].fillna(Test[\"Fare\"].mean(),inplace=True)\nprint(\"Number of null values in Testing Fare column:\",Test[\"Age\"].isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.494390Z","iopub.execute_input":"2022-07-19T19:54:24.495940Z","iopub.status.idle":"2022-07-19T19:54:24.511963Z","shell.execute_reply.started":"2022-07-19T19:54:24.495896Z","shell.execute_reply":"2022-07-19T19:54:24.510618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the cabin column\nTrain.drop('Cabin',axis=1,inplace=True)\nTest.drop('Cabin',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.513471Z","iopub.execute_input":"2022-07-19T19:54:24.514058Z","iopub.status.idle":"2022-07-19T19:54:24.523142Z","shell.execute_reply.started":"2022-07-19T19:54:24.513998Z","shell.execute_reply":"2022-07-19T19:54:24.521943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.524673Z","iopub.execute_input":"2022-07-19T19:54:24.525450Z","iopub.status.idle":"2022-07-19T19:54:24.552494Z","shell.execute_reply.started":"2022-07-19T19:54:24.525413Z","shell.execute_reply":"2022-07-19T19:54:24.550840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Test","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.554517Z","iopub.execute_input":"2022-07-19T19:54:24.555829Z","iopub.status.idle":"2022-07-19T19:54:24.582950Z","shell.execute_reply.started":"2022-07-19T19:54:24.555778Z","shell.execute_reply":"2022-07-19T19:54:24.581358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop columns not needed\nTrain.drop([\"Ticket\",\"Embarked\",\"Name\"],axis=1,inplace=True)\nTest.drop([\"Ticket\",\"Embarked\",\"Name\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.584615Z","iopub.execute_input":"2022-07-19T19:54:24.585633Z","iopub.status.idle":"2022-07-19T19:54:24.598376Z","shell.execute_reply.started":"2022-07-19T19:54:24.585576Z","shell.execute_reply":"2022-07-19T19:54:24.597215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.600320Z","iopub.execute_input":"2022-07-19T19:54:24.600794Z","iopub.status.idle":"2022-07-19T19:54:24.624843Z","shell.execute_reply.started":"2022-07-19T19:54:24.600732Z","shell.execute_reply":"2022-07-19T19:54:24.623286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Test","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.627208Z","iopub.execute_input":"2022-07-19T19:54:24.628043Z","iopub.status.idle":"2022-07-19T19:54:24.647657Z","shell.execute_reply.started":"2022-07-19T19:54:24.627968Z","shell.execute_reply":"2022-07-19T19:54:24.646454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the sex column into numeric values\nSex=pd.get_dummies(Train['Sex'],drop_first=True)\n# create a new column\nTrain['Sex']=Sex","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.648798Z","iopub.execute_input":"2022-07-19T19:54:24.650081Z","iopub.status.idle":"2022-07-19T19:54:24.659725Z","shell.execute_reply.started":"2022-07-19T19:54:24.650015Z","shell.execute_reply":"2022-07-19T19:54:24.658489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sex=pd.get_dummies(Test['Sex'],drop_first=True)\nTest['Sex']=Sex","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.661115Z","iopub.execute_input":"2022-07-19T19:54:24.662298Z","iopub.status.idle":"2022-07-19T19:54:24.670642Z","shell.execute_reply.started":"2022-07-19T19:54:24.662255Z","shell.execute_reply":"2022-07-19T19:54:24.669262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.672631Z","iopub.execute_input":"2022-07-19T19:54:24.674310Z","iopub.status.idle":"2022-07-19T19:54:24.698447Z","shell.execute_reply.started":"2022-07-19T19:54:24.674249Z","shell.execute_reply":"2022-07-19T19:54:24.697126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.700454Z","iopub.execute_input":"2022-07-19T19:54:24.702738Z","iopub.status.idle":"2022-07-19T19:54:24.712465Z","shell.execute_reply.started":"2022-07-19T19:54:24.702687Z","shell.execute_reply":"2022-07-19T19:54:24.711402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data into X and Y \nXTrain = Train.drop(\"Survived\", axis=1)\nYTrain = Train[\"Survived\"]\nXTest=Test\nprint(XTrain.shape)\nprint(YTrain.shape)\nprint(XTest.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.714147Z","iopub.execute_input":"2022-07-19T19:54:24.715335Z","iopub.status.idle":"2022-07-19T19:54:24.726371Z","shell.execute_reply.started":"2022-07-19T19:54:24.715297Z","shell.execute_reply":"2022-07-19T19:54:24.725337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"XTrain=XTrain.to_numpy()\nprint(XTrain.shape)\nYTrain=YTrain.to_numpy()\nprint(YTrain.shape)\nXTest=XTest.to_numpy()\nprint(XTest.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.727982Z","iopub.execute_input":"2022-07-19T19:54:24.728476Z","iopub.status.idle":"2022-07-19T19:54:24.738064Z","shell.execute_reply.started":"2022-07-19T19:54:24.728412Z","shell.execute_reply":"2022-07-19T19:54:24.736633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # reshape the arrays\n# XTrain1=XTrain.T\n# print(XTrain.shape)\n# # YTrain=YTrain.reshape(-1,1)\n# # print(YTrain.shape)\n# XTest=XTest.T\n# print(XTest.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.739951Z","iopub.execute_input":"2022-07-19T19:54:24.740482Z","iopub.status.idle":"2022-07-19T19:54:24.751728Z","shell.execute_reply.started":"2022-07-19T19:54:24.740434Z","shell.execute_reply":"2022-07-19T19:54:24.750273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=LogisticRegression(max_iter=10000)\nmodel.fit(XTrain,YTrain)\nprint(model.intercept_)\nprint(model.coef_)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.760406Z","iopub.execute_input":"2022-07-19T19:54:24.761517Z","iopub.status.idle":"2022-07-19T19:54:24.836180Z","shell.execute_reply.started":"2022-07-19T19:54:24.761467Z","shell.execute_reply":"2022-07-19T19:54:24.834853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=model.predict(XTest)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.837869Z","iopub.execute_input":"2022-07-19T19:54:24.838245Z","iopub.status.idle":"2022-07-19T19:54:24.844045Z","shell.execute_reply.started":"2022-07-19T19:54:24.838210Z","shell.execute_reply":"2022-07-19T19:54:24.842692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_log = round(model.score(XTrain, YTrain) * 100, 2)\nacc_log","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.846399Z","iopub.execute_input":"2022-07-19T19:54:24.847456Z","iopub.status.idle":"2022-07-19T19:54:24.859753Z","shell.execute_reply.started":"2022-07-19T19:54:24.847404Z","shell.execute_reply":"2022-07-19T19:54:24.857733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submission = pd.DataFrame({\"PassengerId\": Test[\"PassengerId\"],\"Survived\": predict})\nSubmission.to_csv('Submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T19:54:24.861999Z","iopub.execute_input":"2022-07-19T19:54:24.862721Z","iopub.status.idle":"2022-07-19T19:54:24.874510Z","shell.execute_reply.started":"2022-07-19T19:54:24.862655Z","shell.execute_reply":"2022-07-19T19:54:24.873289Z"},"trusted":true},"execution_count":null,"outputs":[]}]}