{"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-11T16:06:00.534263Z","iopub.execute_input":"2022-08-11T16:06:00.534798Z","iopub.status.idle":"2022-08-11T16:06:00.563002Z","shell.execute_reply.started":"2022-08-11T16:06:00.534674Z","shell.execute_reply":"2022-08-11T16:06:00.561706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:06:35.074281Z","iopub.execute_input":"2022-08-11T16:06:35.074729Z","iopub.status.idle":"2022-08-11T16:06:35.621160Z","shell.execute_reply.started":"2022-08-11T16:06:35.074692Z","shell.execute_reply":"2022-08-11T16:06:35.619974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:06:47.150314Z","iopub.execute_input":"2022-08-11T16:06:47.151658Z","iopub.status.idle":"2022-08-11T16:06:47.289981Z","shell.execute_reply.started":"2022-08-11T16:06:47.151607Z","shell.execute_reply":"2022-08-11T16:06:47.288855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import rcParams\nrcParams['figure.figsize'] = 11.7,8.27\nimport seaborn as sns\nsns.set_theme(style=\"darkgrid\")\nsns.set_palette('viridis')\nprint(\"Setup Complete\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:07:37.234818Z","iopub.execute_input":"2022-08-11T16:07:37.235235Z","iopub.status.idle":"2022-08-11T16:07:37.430924Z","shell.execute_reply.started":"2022-08-11T16:07:37.235201Z","shell.execute_reply":"2022-08-11T16:07:37.429689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/titanic/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:07:57.064590Z","iopub.execute_input":"2022-08-11T16:07:57.065833Z","iopub.status.idle":"2022-08-11T16:07:57.106096Z","shell.execute_reply.started":"2022-08-11T16:07:57.065751Z","shell.execute_reply":"2022-08-11T16:07:57.104922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:08:17.034009Z","iopub.execute_input":"2022-08-11T16:08:17.034864Z","iopub.status.idle":"2022-08-11T16:08:17.080396Z","shell.execute_reply.started":"2022-08-11T16:08:17.034817Z","shell.execute_reply":"2022-08-11T16:08:17.078893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:11:40.855372Z","iopub.execute_input":"2022-08-11T16:11:40.855819Z","iopub.status.idle":"2022-08-11T16:11:40.865383Z","shell.execute_reply.started":"2022-08-11T16:11:40.855765Z","shell.execute_reply":"2022-08-11T16:11:40.863856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(df.isnull().transpose(),cmap=\"viridis\", cbar_kws={\"label\": 'missing Data'})","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:12:20.194806Z","iopub.execute_input":"2022-08-11T16:12:20.195719Z","iopub.status.idle":"2022-08-11T16:12:21.224899Z","shell.execute_reply.started":"2022-08-11T16:12:20.195677Z","shell.execute_reply":"2022-08-11T16:12:21.223733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(df.corr(), cmap=\"YlGnBu\",annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:21:29.395435Z","iopub.execute_input":"2022-08-11T16:21:29.395862Z","iopub.status.idle":"2022-08-11T16:21:29.866843Z","shell.execute_reply.started":"2022-08-11T16:21:29.395827Z","shell.execute_reply":"2022-08-11T16:21:29.865813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newDf = df.drop('Cabin', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:22:49.559798Z","iopub.execute_input":"2022-08-11T16:22:49.560200Z","iopub.status.idle":"2022-08-11T16:22:49.567104Z","shell.execute_reply.started":"2022-08-11T16:22:49.560167Z","shell.execute_reply":"2022-08-11T16:22:49.565994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newDf['Age'] = newDf['Age'].fillna(newDf['Age'].mean())\nnewDf = newDf.dropna(axis=0, how='any')\nprint(newDf.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:23:27.074678Z","iopub.execute_input":"2022-08-11T16:23:27.075876Z","iopub.status.idle":"2022-08-11T16:23:27.088915Z","shell.execute_reply.started":"2022-08-11T16:23:27.075822Z","shell.execute_reply":"2022-08-11T16:23:27.087934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(newDf)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:23:44.004518Z","iopub.execute_input":"2022-08-11T16:23:44.004953Z","iopub.status.idle":"2022-08-11T16:23:52.809876Z","shell.execute_reply.started":"2022-08-11T16:23:44.004917Z","shell.execute_reply":"2022-08-11T16:23:52.808973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survied = newDf[newDf['Survived'] == 1]['Sex'].value_counts()\nvictims = newDf[newDf['Survived'] == 0]['Sex'].value_counts()\n\nplt.pie(survied,labels= ['female', 'male'],\n        shadow=0.6,\n        explode=[0.2, 0],\n        radius=1.2,)\nplt.legend()\nplt.title('Titanic Surviors by Gender')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:29:52.405682Z","iopub.execute_input":"2022-08-11T16:29:52.406826Z","iopub.status.idle":"2022-08-11T16:29:52.658146Z","shell.execute_reply.started":"2022-08-11T16:29:52.406764Z","shell.execute_reply":"2022-08-11T16:29:52.656859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=newDf, x = 'Survived', hue='Sex',).set(xticklabels = ['Did not Survive', 'Survied'], title = 'Titanic Survival Data')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:30:11.380005Z","iopub.execute_input":"2022-08-11T16:30:11.381172Z","iopub.status.idle":"2022-08-11T16:30:11.596390Z","shell.execute_reply.started":"2022-08-11T16:30:11.381128Z","shell.execute_reply":"2022-08-11T16:30:11.595516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data=newDf, x = 'Sex', y ='Survived').set(title='Co - realation of Survival w.r.t Gender')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:30:44.885376Z","iopub.execute_input":"2022-08-11T16:30:44.886589Z","iopub.status.idle":"2022-08-11T16:30:45.165820Z","shell.execute_reply.started":"2022-08-11T16:30:44.886542Z","shell.execute_reply":"2022-08-11T16:30:45.163233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data = newDf, x = 'Age', y = 'Survived').set(title = 'Co - realation of Survival w.r.t Age')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:30:58.875794Z","iopub.execute_input":"2022-08-11T16:30:58.876192Z","iopub.status.idle":"2022-08-11T16:31:00.674766Z","shell.execute_reply.started":"2022-08-11T16:30:58.876161Z","shell.execute_reply":"2022-08-11T16:31:00.673648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data=newDf, x = 'Pclass', y = 'Survived')\nplt.figure()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:31:23.635730Z","iopub.execute_input":"2022-08-11T16:31:23.636625Z","iopub.status.idle":"2022-08-11T16:31:24.007521Z","shell.execute_reply.started":"2022-08-11T16:31:23.636581Z","shell.execute_reply":"2022-08-11T16:31:24.006590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=newDf, x ='Pclass', hue='Survived')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:31:47.380029Z","iopub.execute_input":"2022-08-11T16:31:47.380444Z","iopub.status.idle":"2022-08-11T16:31:47.849756Z","shell.execute_reply.started":"2022-08-11T16:31:47.380406Z","shell.execute_reply":"2022-08-11T16:31:47.848659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data=newDf, x = 'Fare', y= 'Survived').set(title = 'Co-relatibility of Fare w.r.t Survival')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:32:07.395324Z","iopub.execute_input":"2022-08-11T16:32:07.395751Z","iopub.status.idle":"2022-08-11T16:32:10.717464Z","shell.execute_reply.started":"2022-08-11T16:32:07.395714Z","shell.execute_reply":"2022-08-11T16:32:10.716228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data=newDf, x = 'Parch', y= 'Survived')\nplt.figure()\nsns.lineplot(data=newDf, x = 'SibSp', y= 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:32:22.996481Z","iopub.execute_input":"2022-08-11T16:32:22.996911Z","iopub.status.idle":"2022-08-11T16:32:23.844981Z","shell.execute_reply.started":"2022-08-11T16:32:22.996866Z","shell.execute_reply":"2022-08-11T16:32:23.843753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=newDf)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:32:38.894991Z","iopub.execute_input":"2022-08-11T16:32:38.895389Z","iopub.status.idle":"2022-08-11T16:32:39.175887Z","shell.execute_reply.started":"2022-08-11T16:32:38.895355Z","shell.execute_reply":"2022-08-11T16:32:39.174750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newDf = newDf[newDf['Age'] < 35]\nsns.boxplot(data=newDf, y = 'Age', x = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:34:01.994443Z","iopub.execute_input":"2022-08-11T16:34:01.994903Z","iopub.status.idle":"2022-08-11T16:34:02.210473Z","shell.execute_reply.started":"2022-08-11T16:34:01.994861Z","shell.execute_reply":"2022-08-11T16:34:02.208845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newDf = newDf[newDf['Fare'] <35]\nsns.boxplot(data=newDf, x = 'Survived', y = 'Fare')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:34:23.515421Z","iopub.execute_input":"2022-08-11T16:34:23.515861Z","iopub.status.idle":"2022-08-11T16:34:23.728200Z","shell.execute_reply.started":"2022-08-11T16:34:23.515825Z","shell.execute_reply":"2022-08-11T16:34:23.727357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['Age', 'Sex', \"Pclass\", \"SibSp\"]\nx = newDf[features]\nx['Sex'] = x['Sex'].map({'female': 0, 'male':1})\ny = newDf['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:34:40.515524Z","iopub.execute_input":"2022-08-11T16:34:40.516628Z","iopub.status.idle":"2022-08-11T16:34:40.525377Z","shell.execute_reply.started":"2022-08-11T16:34:40.516583Z","shell.execute_reply":"2022-08-11T16:34:40.523900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xTrain, xTest, yTrain, yTest = train_test_split(x,y, random_state=200,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:35:07.915803Z","iopub.execute_input":"2022-08-11T16:35:07.916250Z","iopub.status.idle":"2022-08-11T16:35:07.925028Z","shell.execute_reply.started":"2022-08-11T16:35:07.916213Z","shell.execute_reply":"2022-08-11T16:35:07.923519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = XGBClassifier(booster = 'gbtree', n_estimators=350, learning_rate=0.7, colsample_bytree=0.4)\nmodel.fit(xTrain, yTrain)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:35:18.280163Z","iopub.execute_input":"2022-08-11T16:35:18.280572Z","iopub.status.idle":"2022-08-11T16:35:19.144248Z","shell.execute_reply.started":"2022-08-11T16:35:18.280538Z","shell.execute_reply":"2022-08-11T16:35:19.142988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(xTest)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:35:27.514967Z","iopub.execute_input":"2022-08-11T16:35:27.515382Z","iopub.status.idle":"2022-08-11T16:35:27.526750Z","shell.execute_reply.started":"2022-08-11T16:35:27.515348Z","shell.execute_reply":"2022-08-11T16:35:27.525717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDf = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:35:53.305374Z","iopub.execute_input":"2022-08-11T16:35:53.305755Z","iopub.status.idle":"2022-08-11T16:35:53.321300Z","shell.execute_reply.started":"2022-08-11T16:35:53.305722Z","shell.execute_reply":"2022-08-11T16:35:53.320091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newTestDf = testDf[features]\nnewTestDf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:36:20.600175Z","iopub.execute_input":"2022-08-11T16:36:20.600637Z","iopub.status.idle":"2022-08-11T16:36:20.611681Z","shell.execute_reply.started":"2022-08-11T16:36:20.600597Z","shell.execute_reply":"2022-08-11T16:36:20.610582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newTestDf['Age'] = newTestDf['Age'].fillna(newTestDf['Age'].mean())\nnewTestDf['Sex'] = newTestDf['Sex'].map({'female': 0, 'male':1})\nnewTestDf.isnull().sum()\ntestPreds = model.predict(newTestDf)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:37:12.285774Z","iopub.execute_input":"2022-08-11T16:37:12.286239Z","iopub.status.idle":"2022-08-11T16:37:12.305203Z","shell.execute_reply.started":"2022-08-11T16:37:12.286202Z","shell.execute_reply":"2022-08-11T16:37:12.304205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testPreds = model.predict(newTestDf)\ntestPreds","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:37:34.459455Z","iopub.execute_input":"2022-08-11T16:37:34.459882Z","iopub.status.idle":"2022-08-11T16:37:34.474564Z","shell.execute_reply.started":"2022-08-11T16:37:34.459835Z","shell.execute_reply":"2022-08-11T16:37:34.473362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': testDf['PassengerId'],\n                       'Survived': testPreds\n                      })\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:46:59.154487Z","iopub.execute_input":"2022-08-11T16:46:59.154912Z","iopub.status.idle":"2022-08-11T16:46:59.163067Z","shell.execute_reply.started":"2022-08-11T16:46:59.154871Z","shell.execute_reply":"2022-08-11T16:46:59.162125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}