{"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":"markdown","source":"## 1. Import required libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn import preprocessing\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:01.373520Z","iopub.execute_input":"2022-07-18T05:07:01.373999Z","iopub.status.idle":"2022-07-18T05:07:02.608544Z","shell.execute_reply.started":"2022-07-18T05:07:01.373901Z","shell.execute_reply":"2022-07-18T05:07:02.607683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Import Dataset","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"../input/titanic/train.csv\")\ntest=pd.read_csv(\"../input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.610875Z","iopub.execute_input":"2022-07-18T05:07:02.611682Z","iopub.status.idle":"2022-07-18T05:07:02.639149Z","shell.execute_reply.started":"2022-07-18T05:07:02.611636Z","shell.execute_reply":"2022-07-18T05:07:02.638232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.640569Z","iopub.execute_input":"2022-07-18T05:07:02.641181Z","iopub.status.idle":"2022-07-18T05:07:02.674453Z","shell.execute_reply.started":"2022-07-18T05:07:02.641146Z","shell.execute_reply":"2022-07-18T05:07:02.673623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.676539Z","iopub.execute_input":"2022-07-18T05:07:02.677140Z","iopub.status.idle":"2022-07-18T05:07:02.698225Z","shell.execute_reply.started":"2022-07-18T05:07:02.677104Z","shell.execute_reply":"2022-07-18T05:07:02.697359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.699501Z","iopub.execute_input":"2022-07-18T05:07:02.700058Z","iopub.status.idle":"2022-07-18T05:07:02.716623Z","shell.execute_reply.started":"2022-07-18T05:07:02.700026Z","shell.execute_reply":"2022-07-18T05:07:02.715869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Show the shape of dataset ","metadata":{}},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.718092Z","iopub.execute_input":"2022-07-18T05:07:02.718626Z","iopub.status.idle":"2022-07-18T05:07:02.724284Z","shell.execute_reply.started":"2022-07-18T05:07:02.718593Z","shell.execute_reply":"2022-07-18T05:07:02.723553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Data Preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:48:46.504925Z","iopub.execute_input":"2022-07-16T06:48:46.505335Z","iopub.status.idle":"2022-07-16T06:48:46.512396Z","shell.execute_reply.started":"2022-07-16T06:48:46.505306Z","shell.execute_reply":"2022-07-16T06:48:46.510838Z"}}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.725606Z","iopub.execute_input":"2022-07-18T05:07:02.726462Z","iopub.status.idle":"2022-07-18T05:07:02.768180Z","shell.execute_reply.started":"2022-07-18T05:07:02.726428Z","shell.execute_reply":"2022-07-18T05:07:02.767380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.769537Z","iopub.execute_input":"2022-07-18T05:07:02.770158Z","iopub.status.idle":"2022-07-18T05:07:02.780357Z","shell.execute_reply.started":"2022-07-18T05:07:02.770124Z","shell.execute_reply":"2022-07-18T05:07:02.779423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()/train.shape[0]*100","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:08:41.564234Z","iopub.execute_input":"2022-07-18T05:08:41.564685Z","iopub.status.idle":"2022-07-18T05:08:41.576349Z","shell.execute_reply.started":"2022-07-18T05:08:41.564647Z","shell.execute_reply":"2022-07-18T05:08:41.575278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.893035Z","iopub.status.idle":"2022-07-18T05:07:02.893665Z","shell.execute_reply.started":"2022-07-18T05:07:02.893473Z","shell.execute_reply":"2022-07-18T05:07:02.893493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train['Cabin']\ndel test['Cabin']","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.894807Z","iopub.status.idle":"2022-07-18T05:07:02.895208Z","shell.execute_reply.started":"2022-07-18T05:07:02.895023Z","shell.execute_reply":"2022-07-18T05:07:02.895041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.897540Z","iopub.status.idle":"2022-07-18T05:07:02.898259Z","shell.execute_reply.started":"2022-07-18T05:07:02.898052Z","shell.execute_reply":"2022-07-18T05:07:02.898073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.899420Z","iopub.status.idle":"2022-07-18T05:07:02.900013Z","shell.execute_reply.started":"2022-07-18T05:07:02.899814Z","shell.execute_reply":"2022-07-18T05:07:02.899837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train.isnull())","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.901074Z","iopub.status.idle":"2022-07-18T05:07:02.901672Z","shell.execute_reply.started":"2022-07-18T05:07:02.901467Z","shell.execute_reply":"2022-07-18T05:07:02.901494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.fillna(train.mean(),inplace=True)\ntest.fillna(test.mean(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.902733Z","iopub.status.idle":"2022-07-18T05:07:02.903370Z","shell.execute_reply.started":"2022-07-18T05:07:02.903157Z","shell.execute_reply":"2022-07-18T05:07:02.903194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.904398Z","iopub.status.idle":"2022-07-18T05:07:02.904997Z","shell.execute_reply.started":"2022-07-18T05:07:02.904767Z","shell.execute_reply":"2022-07-18T05:07:02.904811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Embarked\"].unique()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.906096Z","iopub.status.idle":"2022-07-18T05:07:02.906689Z","shell.execute_reply.started":"2022-07-18T05:07:02.906486Z","shell.execute_reply":"2022-07-18T05:07:02.906513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.fillna(method = 'ffill', inplace=True)\ntest.fillna(method = 'ffill', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.907708Z","iopub.status.idle":"2022-07-18T05:07:02.908310Z","shell.execute_reply.started":"2022-07-18T05:07:02.908098Z","shell.execute_reply":"2022-07-18T05:07:02.908130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.909332Z","iopub.status.idle":"2022-07-18T05:07:02.909930Z","shell.execute_reply.started":"2022-07-18T05:07:02.909692Z","shell.execute_reply":"2022-07-18T05:07:02.909718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train.isnull())","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.910955Z","iopub.status.idle":"2022-07-18T05:07:02.911539Z","shell.execute_reply.started":"2022-07-18T05:07:02.911340Z","shell.execute_reply":"2022-07-18T05:07:02.911367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Label encoding","metadata":{}},{"cell_type":"code","source":"le = preprocessing.LabelEncoder()\ntrain[\"Embarked\"]=le.fit_transform(train[\"Embarked\"])\nle = preprocessing.LabelEncoder()\ntrain[\"Sex\"]=le.fit_transform(train[\"Sex\"])\nle = preprocessing.LabelEncoder()\ntest[\"Embarked\"]=le.fit_transform(test[\"Embarked\"])\nle = preprocessing.LabelEncoder()\ntest[\"Sex\"]=le.fit_transform(test[\"Sex\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.912555Z","iopub.status.idle":"2022-07-18T05:07:02.913119Z","shell.execute_reply.started":"2022-07-18T05:07:02.912927Z","shell.execute_reply":"2022-07-18T05:07:02.912951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.914130Z","iopub.status.idle":"2022-07-18T05:07:02.914729Z","shell.execute_reply.started":"2022-07-18T05:07:02.914533Z","shell.execute_reply":"2022-07-18T05:07:02.914559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Survived\")[\"Sex\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.915773Z","iopub.status.idle":"2022-07-18T05:07:02.916367Z","shell.execute_reply.started":"2022-07-18T05:07:02.916174Z","shell.execute_reply":"2022-07-18T05:07:02.916199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Survived\")[\"Pclass\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.917407Z","iopub.status.idle":"2022-07-18T05:07:02.918023Z","shell.execute_reply.started":"2022-07-18T05:07:02.917767Z","shell.execute_reply":"2022-07-18T05:07:02.917814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.919076Z","iopub.status.idle":"2022-07-18T05:07:02.919645Z","shell.execute_reply.started":"2022-07-18T05:07:02.919446Z","shell.execute_reply":"2022-07-18T05:07:02.919473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train['Name']\ndel train['Ticket']\ndel test['Name']\ndel test['Ticket']","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.920672Z","iopub.status.idle":"2022-07-18T05:07:02.921273Z","shell.execute_reply.started":"2022-07-18T05:07:02.921058Z","shell.execute_reply":"2022-07-18T05:07:02.921085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nax=sns.heatmap(train.corr(), annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.922460Z","iopub.status.idle":"2022-07-18T05:07:02.922870Z","shell.execute_reply.started":"2022-07-18T05:07:02.922666Z","shell.execute_reply":"2022-07-18T05:07:02.922685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.924097Z","iopub.status.idle":"2022-07-18T05:07:02.924461Z","shell.execute_reply.started":"2022-07-18T05:07:02.924285Z","shell.execute_reply":"2022-07-18T05:07:02.924302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Data splitting","metadata":{}},{"cell_type":"code","source":"x_train= train[[\"Pclass\",\"Embarked\",\"SibSp\",\"Age\",\"Sex\",\"PassengerId\"]]\ny_train= train[\"Survived\"]\nx_test= test[[\"Pclass\",\"Embarked\",\"SibSp\",\"Age\",\"Sex\",\"PassengerId\"]]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.926105Z","iopub.status.idle":"2022-07-18T05:07:02.926849Z","shell.execute_reply.started":"2022-07-18T05:07:02.926580Z","shell.execute_reply":"2022-07-18T05:07:02.926610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6. Model selection","metadata":{}},{"cell_type":"code","source":"clf = LogisticRegression()\nclf.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.928140Z","iopub.status.idle":"2022-07-18T05:07:02.928507Z","shell.execute_reply.started":"2022-07-18T05:07:02.928335Z","shell.execute_reply":"2022-07-18T05:07:02.928352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = clf.predict(x_test)\npredict","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.930223Z","iopub.status.idle":"2022-07-18T05:07:02.930947Z","shell.execute_reply.started":"2022-07-18T05:07:02.930709Z","shell.execute_reply":"2022-07-18T05:07:02.930729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(x_test,predict)*100","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.931841Z","iopub.status.idle":"2022-07-18T05:07:02.932194Z","shell.execute_reply.started":"2022-07-18T05:07:02.932026Z","shell.execute_reply":"2022-07-18T05:07:02.932043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Hence, our accuracy of our model is 100%","metadata":{}},{"cell_type":"code","source":"pd.DataFrame({\n    \"Survived\" : predict\n},columns=[\"Survived\"])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.934323Z","iopub.status.idle":"2022-07-18T05:07:02.935208Z","shell.execute_reply.started":"2022-07-18T05:07:02.934996Z","shell.execute_reply":"2022-07-18T05:07:02.935017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=test[\"PassengerId\"]\ntest1","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.936632Z","iopub.status.idle":"2022-07-18T05:07:02.937013Z","shell.execute_reply.started":"2022-07-18T05:07:02.936832Z","shell.execute_reply":"2022-07-18T05:07:02.936850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fl = pd.DataFrame({\n    \"PassengerId\":test1.values,\n    \"Survived\" : predict\n},columns=[\"PassengerId\",\"Survived\"])\nfl","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.938135Z","iopub.status.idle":"2022-07-18T05:07:02.939016Z","shell.execute_reply.started":"2022-07-18T05:07:02.938782Z","shell.execute_reply":"2022-07-18T05:07:02.938827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fl.set_index(\"PassengerId\", inplace=True)\nfl.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.940261Z","iopub.status.idle":"2022-07-18T05:07:02.941055Z","shell.execute_reply.started":"2022-07-18T05:07:02.940858Z","shell.execute_reply":"2022-07-18T05:07:02.940880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:07:02.942448Z","iopub.status.idle":"2022-07-18T05:07:02.943232Z","shell.execute_reply.started":"2022-07-18T05:07:02.943024Z","shell.execute_reply":"2022-07-18T05:07:02.943045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}