{"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\nfrom sklearn.preprocessing import StandardScaler\nfrom scipy import stats\nimport numpy as np\npd.options.display.max_columns = None\npd.options.display.max_rows = None\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import r2_score\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nimport seaborn as sns\nimport plotly.express as px","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-16T12:51:53.802517Z","iopub.execute_input":"2022-07-16T12:51:53.802892Z","iopub.status.idle":"2022-07-16T12:51:53.810577Z","shell.execute_reply.started":"2022-07-16T12:51:53.802860Z","shell.execute_reply":"2022-07-16T12:51:53.809656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/titanic/train.csv')\ntest=pd.read_csv('../input/titanic/test.csv')\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.816159Z","iopub.execute_input":"2022-07-16T12:51:53.817463Z","iopub.status.idle":"2022-07-16T12:51:53.846031Z","shell.execute_reply.started":"2022-07-16T12:51:53.817210Z","shell.execute_reply":"2022-07-16T12:51:53.844770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.848629Z","iopub.execute_input":"2022-07-16T12:51:53.849097Z","iopub.status.idle":"2022-07-16T12:51:53.869136Z","shell.execute_reply.started":"2022-07-16T12:51:53.849043Z","shell.execute_reply":"2022-07-16T12:51:53.867956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.isna().sum())\nprint(train.shape)\nprint('')\nprint(test.isna().sum())\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.872177Z","iopub.execute_input":"2022-07-16T12:51:53.873057Z","iopub.status.idle":"2022-07-16T12:51:53.885003Z","shell.execute_reply.started":"2022-07-16T12:51:53.872997Z","shell.execute_reply":"2022-07-16T12:51:53.883839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.dtypes)\nprint('')\ntest.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.886669Z","iopub.execute_input":"2022-07-16T12:51:53.886999Z","iopub.status.idle":"2022-07-16T12:51:53.904937Z","shell.execute_reply.started":"2022-07-16T12:51:53.886970Z","shell.execute_reply":"2022-07-16T12:51:53.903559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.groupby(['Sex'])['Age'].mean().round(0))\nprint('')\nprint(test.groupby(['Sex'])['Age'].mean().round(0))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.907557Z","iopub.execute_input":"2022-07-16T12:51:53.908266Z","iopub.status.idle":"2022-07-16T12:51:53.922035Z","shell.execute_reply.started":"2022-07-16T12:51:53.908220Z","shell.execute_reply":"2022-07-16T12:51:53.920919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'] = train['Age'].fillna(train.groupby('Sex')['Age'].transform('mean'))\ntrain['Age']=train['Age'].round(0)\n\ntest['Age'] = test['Age'].fillna(test.groupby('Sex')['Age'].transform('mean'))\ntest['Age']=test['Age'].round(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.923294Z","iopub.execute_input":"2022-07-16T12:51:53.923654Z","iopub.status.idle":"2022-07-16T12:51:53.938046Z","shell.execute_reply.started":"2022-07-16T12:51:53.923624Z","shell.execute_reply":"2022-07-16T12:51:53.936735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Embarked'].fillna(train['Embarked'].mode()[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.939212Z","iopub.execute_input":"2022-07-16T12:51:53.939934Z","iopub.status.idle":"2022-07-16T12:51:53.950270Z","shell.execute_reply.started":"2022-07-16T12:51:53.939892Z","shell.execute_reply":"2022-07-16T12:51:53.949430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.952118Z","iopub.execute_input":"2022-07-16T12:51:53.952650Z","iopub.status.idle":"2022-07-16T12:51:53.978344Z","shell.execute_reply.started":"2022-07-16T12:51:53.952617Z","shell.execute_reply":"2022-07-16T12:51:53.977090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\nsns.barplot(x='Sex',y='Survived',hue='Pclass',data=train,)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:53.979712Z","iopub.execute_input":"2022-07-16T12:51:53.980031Z","iopub.status.idle":"2022-07-16T12:51:54.335287Z","shell.execute_reply.started":"2022-07-16T12:51:53.980002Z","shell.execute_reply":"2022-07-16T12:51:54.334302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:54.336705Z","iopub.execute_input":"2022-07-16T12:51:54.337771Z","iopub.status.idle":"2022-07-16T12:51:54.355502Z","shell.execute_reply.started":"2022-07-16T12:51:54.337724Z","shell.execute_reply":"2022-07-16T12:51:54.354517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived = 'survived'\nnot_survived = 'not survived'\nfig, axes = plt.subplots(nrows=1, ncols=2,figsize=(16, 8))\nwomen = train[train['Sex']=='female']\nmen = train[train['Sex']=='male']\nax = sns.distplot(women[women['Survived']==1].Age.dropna(), bins=18, label = survived, ax = axes[0], kde =False, color=\"green\")\nax = sns.distplot(women[women['Survived']==0].Age.dropna(), bins=40, label = not_survived, ax = axes[0], kde =False, color=\"red\")\nax.legend()\nax.set_title('Survival Rate for Female')\nax = sns.distplot(men[men['Survived']==1].Age.dropna(), bins=18, label = survived, ax = axes[1], kde = False, color=\"green\")\nax = sns.distplot(men[men['Survived']==0].Age.dropna(), bins=40, label = not_survived, ax = axes[1], kde = False, color=\"red\")\nax.legend()\n_ = ax.set_title('Survival Rate for Male');","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:54.358687Z","iopub.execute_input":"2022-07-16T12:51:54.359144Z","iopub.status.idle":"2022-07-16T12:51:54.916226Z","shell.execute_reply.started":"2022-07-16T12:51:54.359099Z","shell.execute_reply":"2022-07-16T12:51:54.914936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.FacetGrid(train, col='Survived')\ng.map(plt.hist, 'Age', bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:54.917998Z","iopub.execute_input":"2022-07-16T12:51:54.918445Z","iopub.status.idle":"2022-07-16T12:51:55.332686Z","shell.execute_reply.started":"2022-07-16T12:51:54.918411Z","shell.execute_reply":"2022-07-16T12:51:55.331530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for template in [\"plotly\"]:\n    fig = px.scatter(train,\n                     x=\"PassengerId\", y=\"Age\", color=\"Survived\",\n                     log_x=True, size_max=20,\n                     template=template, title=\"Which Age Survived?\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.334238Z","iopub.execute_input":"2022-07-16T12:51:55.334692Z","iopub.status.idle":"2022-07-16T12:51:55.398828Z","shell.execute_reply.started":"2022-07-16T12:51:55.334648Z","shell.execute_reply":"2022-07-16T12:51:55.397710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in train.columns:\n    if train[col].isnull().mean()*100>40:\n        train.drop(col,axis=1,inplace=True)\n    \n\n\nfor col in test.columns:\n    if test[col].isnull().mean()*100>40:\n        test.drop(col,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.400403Z","iopub.execute_input":"2022-07-16T12:51:55.400871Z","iopub.status.idle":"2022-07-16T12:51:55.416756Z","shell.execute_reply.started":"2022-07-16T12:51:55.400828Z","shell.execute_reply":"2022-07-16T12:51:55.415169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Fare'].fillna(test['Fare'].mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.417855Z","iopub.execute_input":"2022-07-16T12:51:55.418238Z","iopub.status.idle":"2022-07-16T12:51:55.430683Z","shell.execute_reply.started":"2022-07-16T12:51:55.418194Z","shell.execute_reply":"2022-07-16T12:51:55.429752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.isna().sum())\nprint(train.shape)\n\nprint('')\n\nprint(test.isna().sum())\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.431887Z","iopub.execute_input":"2022-07-16T12:51:55.432365Z","iopub.status.idle":"2022-07-16T12:51:55.452847Z","shell.execute_reply.started":"2022-07-16T12:51:55.432332Z","shell.execute_reply":"2022-07-16T12:51:55.451581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Corr = train.select_dtypes(exclude=[\"object\"]).corr()\nabs_corr_on_saleprice = abs(Corr[['Survived']].copy())\nabs_corr_on_saleprice.drop('Survived', axis=0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.454416Z","iopub.execute_input":"2022-07-16T12:51:55.455128Z","iopub.status.idle":"2022-07-16T12:51:55.471804Z","shell.execute_reply.started":"2022-07-16T12:51:55.455078Z","shell.execute_reply":"2022-07-16T12:51:55.470456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"abs_corr_on_saleprice.sort_values(by='Survived', ascending=False).plot(kind='bar', figsize=(12,8))\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.473441Z","iopub.execute_input":"2022-07-16T12:51:55.474300Z","iopub.status.idle":"2022-07-16T12:51:55.697143Z","shell.execute_reply.started":"2022-07-16T12:51:55.474249Z","shell.execute_reply":"2022-07-16T12:51:55.696018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\nsns.barplot(x='Pclass',y='Survived',hue='Sex',data=train,)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:55.702501Z","iopub.execute_input":"2022-07-16T12:51:55.703417Z","iopub.status.idle":"2022-07-16T12:51:56.069204Z","shell.execute_reply.started":"2022-07-16T12:51:55.703372Z","shell.execute_reply":"2022-07-16T12:51:56.068033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_age(train):\n\n  train.loc[ train['Age'] <= 16, 'Age'] = 0\n  train.loc[(train['Age'] > 16) & (train['Age'] <= 32), 'Age'] = 1\n  train.loc[(train['Age'] > 32) & (train['Age'] <= 48), 'Age'] = 2\n  train.loc[(train['Age'] > 48) & (train['Age'] <= 64), 'Age'] = 3\n  train.loc[ train['Age'] > 64, 'Age']\n  train['Age'] = train['Age'].astype(int)\n\n\n  return train\n\ntrain=set_age(train)\ntest=set_age(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.070526Z","iopub.execute_input":"2022-07-16T12:51:56.070963Z","iopub.status.idle":"2022-07-16T12:51:56.089835Z","shell.execute_reply.started":"2022-07-16T12:51:56.070929Z","shell.execute_reply":"2022-07-16T12:51:56.088449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_fare(train):\n\n  train.loc[ train['Fare'] <= 7.91, 'Fare'] = 0\n  train.loc[(train['Fare'] > 7.91) & (train['Fare'] <= 14.454), 'Fare'] = 1\n  train.loc[(train['Fare'] > 14.454) & (train['Fare'] <= 31), 'Fare']   = 2\n  train.loc[ train['Fare'] > 31, 'Fare'] = 3\n  train['Fare'] = train['Fare'].astype(int)\n\n  return train\n\n\ntrain=set_fare(train)\ntest=set_fare(test)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.091436Z","iopub.execute_input":"2022-07-16T12:51:56.091904Z","iopub.status.idle":"2022-07-16T12:51:56.125977Z","shell.execute_reply.started":"2022-07-16T12:51:56.091866Z","shell.execute_reply":"2022-07-16T12:51:56.124954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.127448Z","iopub.execute_input":"2022-07-16T12:51:56.128619Z","iopub.status.idle":"2022-07-16T12:51:56.149883Z","shell.execute_reply.started":"2022-07-16T12:51:56.128573Z","shell.execute_reply":"2022-07-16T12:51:56.149026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"integer_columns=train.select_dtypes(include=['int64','float64']).columns.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.151462Z","iopub.execute_input":"2022-07-16T12:51:56.151964Z","iopub.status.idle":"2022-07-16T12:51:56.159161Z","shell.execute_reply.started":"2022-07-16T12:51:56.151934Z","shell.execute_reply":"2022-07-16T12:51:56.157952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()\nfor col in train.columns:\n    if train[col].dtypes == object:\n        train[col]= le.fit_transform(train[col])\n\n\n\nfor col in test.columns:\n    if test[col].dtypes == object:\n        test[col]= le.fit_transform(test[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.160639Z","iopub.execute_input":"2022-07-16T12:51:56.161078Z","iopub.status.idle":"2022-07-16T12:51:56.182800Z","shell.execute_reply.started":"2022-07-16T12:51:56.161033Z","shell.execute_reply":"2022-07-16T12:51:56.181837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.dtypes)\nprint('')\nprint(test.dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.184289Z","iopub.execute_input":"2022-07-16T12:51:56.184913Z","iopub.status.idle":"2022-07-16T12:51:56.194103Z","shell.execute_reply.started":"2022-07-16T12:51:56.184879Z","shell.execute_reply":"2022-07-16T12:51:56.192696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['Ticket','Name', 'PassengerId'], axis=1)\ntest = test.drop(['Ticket','Name'], axis=1)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.195687Z","iopub.execute_input":"2022-07-16T12:51:56.196743Z","iopub.status.idle":"2022-07-16T12:51:56.217321Z","shell.execute_reply.started":"2022-07-16T12:51:56.196694Z","shell.execute_reply":"2022-07-16T12:51:56.216447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.218838Z","iopub.execute_input":"2022-07-16T12:51:56.219195Z","iopub.status.idle":"2022-07-16T12:51:56.239885Z","shell.execute_reply.started":"2022-07-16T12:51:56.219163Z","shell.execute_reply":"2022-07-16T12:51:56.238926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train.drop(['Survived'], axis = 1)\nY=train['Survived']\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.241412Z","iopub.execute_input":"2022-07-16T12:51:56.241889Z","iopub.status.idle":"2022-07-16T12:51:56.263614Z","shell.execute_reply.started":"2022-07-16T12:51:56.241844Z","shell.execute_reply":"2022-07-16T12:51:56.262726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.264826Z","iopub.execute_input":"2022-07-16T12:51:56.265496Z","iopub.status.idle":"2022-07-16T12:51:56.273892Z","shell.execute_reply.started":"2022-07-16T12:51:56.265426Z","shell.execute_reply":"2022-07-16T12:51:56.273071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop(\"Survived\", axis=1)\nY_train = train[\"Survived\"]\nX_test  = test.drop(\"PassengerId\", axis=1).copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.275344Z","iopub.execute_input":"2022-07-16T12:51:56.276022Z","iopub.status.idle":"2022-07-16T12:51:56.292424Z","shell.execute_reply.started":"2022-07-16T12:51:56.275984Z","shell.execute_reply":"2022-07-16T12:51:56.291107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# decision_tree\n\ndecision_tree = DecisionTreeClassifier()\ndecision_tree.fit(X_train, Y_train)\nY_pred = decision_tree.predict(X_test)\nacc_decision_tree = round(decision_tree.score(X_train, Y_train) * 100, 2)\nacc_decision_tree","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.293933Z","iopub.execute_input":"2022-07-16T12:51:56.294282Z","iopub.status.idle":"2022-07-16T12:51:56.319671Z","shell.execute_reply.started":"2022-07-16T12:51:56.294251Z","shell.execute_reply":"2022-07-16T12:51:56.318432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Forest\n\nrandom_forest = RandomForestClassifier(n_estimators=100)\nrandom_forest.fit(X_train, Y_train)\nY_pred = random_forest.predict(X_test)\nrandom_forest.score(X_train, Y_train)\nacc_random_forest = round(random_forest.score(X_train, Y_train) * 100, 2)\nacc_random_forest","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.320862Z","iopub.execute_input":"2022-07-16T12:51:56.321404Z","iopub.status.idle":"2022-07-16T12:51:56.551293Z","shell.execute_reply.started":"2022-07-16T12:51:56.321373Z","shell.execute_reply":"2022-07-16T12:51:56.550127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# KNN Classifier\n\nclassifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)\nclassifier.fit(X_train, Y_train)\n\nacc_LinearRegression = round(classifier.score(X_train, Y_train) * 100, 2)\nacc_LinearRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.552775Z","iopub.execute_input":"2022-07-16T12:51:56.553269Z","iopub.status.idle":"2022-07-16T12:51:56.600470Z","shell.execute_reply.started":"2022-07-16T12:51:56.553222Z","shell.execute_reply":"2022-07-16T12:51:56.599398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1 = pd.DataFrame({\n        \"PassengerId\": test[\"PassengerId\"],\n        \"Survived\": Y_pred\n    })\n# submission1.to_csv('submission1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T12:51:56.601612Z","iopub.execute_input":"2022-07-16T12:51:56.601931Z","iopub.status.idle":"2022-07-16T12:51:56.608501Z","shell.execute_reply.started":"2022-07-16T12:51:56.601895Z","shell.execute_reply":"2022-07-16T12:51:56.607341Z"},"trusted":true},"execution_count":null,"outputs":[]}]}