{"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.preprocessing import LabelEncoder","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-16T14:37:22.980337Z","iopub.execute_input":"2022-07-16T14:37:22.980705Z","iopub.status.idle":"2022-07-16T14:37:23.157381Z","shell.execute_reply.started":"2022-07-16T14:37:22.980670Z","shell.execute_reply":"2022-07-16T14:37:23.156330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/titanic/train.csv')\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:29:15.479288Z","iopub.execute_input":"2022-07-16T14:29:15.479864Z","iopub.status.idle":"2022-07-16T14:29:15.497812Z","shell.execute_reply.started":"2022-07-16T14:29:15.479831Z","shell.execute_reply":"2022-07-16T14:29:15.497042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(columns = ['PassengerId', 'Name', 'Ticket'], inplace = True)\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:32:07.812504Z","iopub.execute_input":"2022-07-16T14:32:07.812907Z","iopub.status.idle":"2022-07-16T14:32:07.831290Z","shell.execute_reply.started":"2022-07-16T14:32:07.812875Z","shell.execute_reply":"2022-07-16T14:32:07.830148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:32:28.259112Z","iopub.execute_input":"2022-07-16T14:32:28.259511Z","iopub.status.idle":"2022-07-16T14:32:28.281671Z","shell.execute_reply.started":"2022-07-16T14:32:28.259477Z","shell.execute_reply":"2022-07-16T14:32:28.280522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:33:27.590097Z","iopub.execute_input":"2022-07-16T14:33:27.590581Z","iopub.status.idle":"2022-07-16T14:33:27.601376Z","shell.execute_reply.started":"2022-07-16T14:33:27.590532Z","shell.execute_reply":"2022-07-16T14:33:27.600076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(columns = ['Cabin'], inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:33:48.688457Z","iopub.execute_input":"2022-07-16T14:33:48.688844Z","iopub.status.idle":"2022-07-16T14:33:48.695214Z","shell.execute_reply.started":"2022-07-16T14:33:48.688812Z","shell.execute_reply":"2022-07-16T14:33:48.694123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.dropna(axis = 0, inplace = True)\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:03.033360Z","iopub.execute_input":"2022-07-16T14:35:03.033713Z","iopub.status.idle":"2022-07-16T14:35:03.048946Z","shell.execute_reply.started":"2022-07-16T14:35:03.033684Z","shell.execute_reply":"2022-07-16T14:35:03.048016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le_sex = LabelEncoder()\nle_embarked = LabelEncoder()\ndata['Sex'] = le_sex.fit_transform(data['Sex'])\ndata['Embarked'] = le_embarked.fit_transform(data['Embarked'])\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:53.658785Z","iopub.execute_input":"2022-07-16T14:36:53.659104Z","iopub.status.idle":"2022-07-16T14:36:53.675078Z","shell.execute_reply.started":"2022-07-16T14:36:53.659077Z","shell.execute_reply":"2022-07-16T14:36:53.674146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le_sex.classes_, le_embarked.classes_","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:42:20.495970Z","iopub.execute_input":"2022-07-16T14:42:20.496343Z","iopub.status.idle":"2022-07-16T14:42:20.502989Z","shell.execute_reply.started":"2022-07-16T14:42:20.496297Z","shell.execute_reply":"2022-07-16T14:42:20.502264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:59.982046Z","iopub.execute_input":"2022-07-16T14:36:59.982597Z","iopub.status.idle":"2022-07-16T14:36:59.998586Z","shell.execute_reply.started":"2022-07-16T14:36:59.982542Z","shell.execute_reply":"2022-07-16T14:36:59.997676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.corr()['Survived'].sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:58:05.803255Z","iopub.execute_input":"2022-07-16T14:58:05.803675Z","iopub.status.idle":"2022-07-16T14:58:05.814382Z","shell.execute_reply.started":"2022-07-16T14:58:05.803639Z","shell.execute_reply":"2022-07-16T14:58:05.813112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.abs(data.corr())['Survived'].sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:38:57.568216Z","iopub.execute_input":"2022-07-16T14:38:57.568607Z","iopub.status.idle":"2022-07-16T14:38:57.578342Z","shell.execute_reply.started":"2022-07-16T14:38:57.568575Z","shell.execute_reply":"2022-07-16T14:38:57.577224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(np.abs(data.corr()))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:38:13.092718Z","iopub.execute_input":"2022-07-16T14:38:13.093127Z","iopub.status.idle":"2022-07-16T14:38:13.360459Z","shell.execute_reply.started":"2022-07-16T14:38:13.093093Z","shell.execute_reply":"2022-07-16T14:38:13.359074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Survived'].plot.hist()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:23:37.407869Z","iopub.execute_input":"2022-07-16T15:23:37.408442Z","iopub.status.idle":"2022-07-16T15:23:37.619400Z","shell.execute_reply.started":"2022-07-16T15:23:37.408405Z","shell.execute_reply":"2022-07-16T15:23:37.618364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(data['Age'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:39:32.714201Z","iopub.execute_input":"2022-07-16T14:39:32.714606Z","iopub.status.idle":"2022-07-16T14:39:32.955466Z","shell.execute_reply.started":"2022-07-16T14:39:32.714572Z","shell.execute_reply":"2022-07-16T14:39:32.954490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(data['Pclass'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:39:52.349978Z","iopub.execute_input":"2022-07-16T14:39:52.350401Z","iopub.status.idle":"2022-07-16T14:39:52.566167Z","shell.execute_reply.started":"2022-07-16T14:39:52.350365Z","shell.execute_reply":"2022-07-16T14:39:52.565040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(data['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:40:05.518929Z","iopub.execute_input":"2022-07-16T14:40:05.519976Z","iopub.status.idle":"2022-07-16T14:40:05.678072Z","shell.execute_reply.started":"2022-07-16T14:40:05.519930Z","shell.execute_reply":"2022-07-16T14:40:05.676913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(data['Survived'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:42:45.417585Z","iopub.execute_input":"2022-07-16T14:42:45.418791Z","iopub.status.idle":"2022-07-16T14:42:45.585835Z","shell.execute_reply.started":"2022-07-16T14:42:45.418737Z","shell.execute_reply":"2022-07-16T14:42:45.584952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data, x = 'Survived', hue = 'Sex', kind = 'kde')\nsns.displot(data, x = 'Survived', hue = 'Sex', multiple = 'dodge')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:48:54.233629Z","iopub.execute_input":"2022-07-16T14:48:54.234620Z","iopub.status.idle":"2022-07-16T14:48:55.104763Z","shell.execute_reply.started":"2022-07-16T14:48:54.234578Z","shell.execute_reply":"2022-07-16T14:48:55.103639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data, x = 'Survived', hue = 'Pclass', multiple = 'dodge')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:54:13.401337Z","iopub.execute_input":"2022-07-16T14:54:13.402064Z","iopub.status.idle":"2022-07-16T14:54:13.898782Z","shell.execute_reply.started":"2022-07-16T14:54:13.402013Z","shell.execute_reply":"2022-07-16T14:54:13.897572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data[data['Pclass'] == 1], x = 'Survived', hue = 'Sex', multiple = 'dodge').set(title = 'class 1')\nsns.displot(data[data['Pclass'] == 2], x = 'Survived', hue = 'Sex', multiple = 'dodge').set(title = 'class 2')\nsns.displot(data[data['Pclass'] == 3], x = 'Survived', hue = 'Sex', multiple = 'dodge').set(title = 'class 3')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:56:58.425920Z","iopub.execute_input":"2022-07-16T14:56:58.426297Z","iopub.status.idle":"2022-07-16T14:56:59.747359Z","shell.execute_reply.started":"2022-07-16T14:56:58.426266Z","shell.execute_reply":"2022-07-16T14:56:59.746366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data, x = 'Age', hue = 'Survived', multiple = 'stack')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:18:31.089781Z","iopub.execute_input":"2022-07-16T15:18:31.090469Z","iopub.status.idle":"2022-07-16T15:18:31.663064Z","shell.execute_reply.started":"2022-07-16T15:18:31.090428Z","shell.execute_reply":"2022-07-16T15:18:31.661984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(data[['Survived', 'Sex', 'Pclass']], hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:59:41.042562Z","iopub.execute_input":"2022-07-16T14:59:41.043300Z","iopub.status.idle":"2022-07-16T14:59:42.311659Z","shell.execute_reply.started":"2022-07-16T14:59:41.043262Z","shell.execute_reply":"2022-07-16T14:59:42.310761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data[['Sex', 'Pclass']].to_numpy()\ny = data['Survived'].to_numpy()\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:01:41.229970Z","iopub.execute_input":"2022-07-16T15:01:41.230421Z","iopub.status.idle":"2022-07-16T15:01:41.240154Z","shell.execute_reply.started":"2022-07-16T15:01:41.230380Z","shell.execute_reply":"2022-07-16T15:01:41.238774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\nmodel.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:05:41.317838Z","iopub.execute_input":"2022-07-16T15:05:41.318629Z","iopub.status.idle":"2022-07-16T15:05:41.335825Z","shell.execute_reply.started":"2022-07-16T15:05:41.318581Z","shell.execute_reply":"2022-07-16T15:05:41.334819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.coef_, model.intercept_","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:06:17.363642Z","iopub.execute_input":"2022-07-16T15:06:17.364748Z","iopub.status.idle":"2022-07-16T15:06:17.371505Z","shell.execute_reply.started":"2022-07-16T15:06:17.364702Z","shell.execute_reply":"2022-07-16T15:06:17.370595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def f(x1, x2):\n    y = 3.14 - 2.25*x1 - 0.99*x2\n    z = 1 / (1 + np.exp(-y))\n    return 1 if z > 0.5 else 0\n\npredictions = []\nfor x1, x2 in X_test:\n    predictions.append(f(x1, x2))\n    \nsum(predictions == y_test)/len(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:11:39.261437Z","iopub.execute_input":"2022-07-16T15:11:39.261833Z","iopub.status.idle":"2022-07-16T15:11:39.277515Z","shell.execute_reply.started":"2022-07-16T15:11:39.261800Z","shell.execute_reply":"2022-07-16T15:11:39.276135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"probability of survival = 1 / ( 1 + exp(-3.14 + 2.25*sex + 0.99*class)), sex: male=1 female=0, class: 1 2 3.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}