{"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)\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import RepeatedKFold\nimport re\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-11T21:16:51.051794Z","iopub.execute_input":"2022-08-11T21:16:51.052383Z","iopub.status.idle":"2022-08-11T21:16:51.986377Z","shell.execute_reply.started":"2022-08-11T21:16:51.052263Z","shell.execute_reply":"2022-08-11T21:16:51.985118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:02.171329Z","iopub.execute_input":"2022-08-11T21:17:02.171782Z","iopub.status.idle":"2022-08-11T21:17:02.212209Z","shell.execute_reply.started":"2022-08-11T21:17:02.171746Z","shell.execute_reply":"2022-08-11T21:17:02.211105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:08.510586Z","iopub.execute_input":"2022-08-11T21:17:08.511144Z","iopub.status.idle":"2022-08-11T21:17:08.544403Z","shell.execute_reply.started":"2022-08-11T21:17:08.511099Z","shell.execute_reply":"2022-08-11T21:17:08.542802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"women = train_data.loc[train_data.Sex == 'female'][\"Survived\"]\nrate_women = sum(women)/len(women)\n\nprint(\"% of women who survived:\", rate_women)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:14.625485Z","iopub.execute_input":"2022-08-11T21:17:14.626041Z","iopub.status.idle":"2022-08-11T21:17:14.642627Z","shell.execute_reply.started":"2022-08-11T21:17:14.625987Z","shell.execute_reply":"2022-08-11T21:17:14.641413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"men = train_data.loc[train_data.Sex == 'male'][\"Survived\"]\nrate_men = sum(men)/len(men)\n\nprint(\"% of men who survived:\", rate_men)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:20.704839Z","iopub.execute_input":"2022-08-11T21:17:20.705303Z","iopub.status.idle":"2022-08-11T21:17:20.715137Z","shell.execute_reply.started":"2022-08-11T21:17:20.705265Z","shell.execute_reply":"2022-08-11T21:17:20.713827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\ny = train_data[\"Survived\"]\n\nfeatures = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\"]\nX = pd.get_dummies(train_data[features])\nX_test = pd.get_dummies(test_data[features])\n\nmodel = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)\nmodel.fit(X, y)\npredictions = model.predict(X_test)\n\noutput = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:25.489784Z","iopub.execute_input":"2022-08-11T21:17:25.490770Z","iopub.status.idle":"2022-08-11T21:17:25.859482Z","shell.execute_reply.started":"2022-08-11T21:17:25.490731Z","shell.execute_reply":"2022-08-11T21:17:25.858200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lmplot(data = train_data, x = 'Age', y = 'Fare', hue = 'Survived', fit_reg = False, size = 4, aspect = 3)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:28.442660Z","iopub.execute_input":"2022-08-11T21:17:28.443237Z","iopub.status.idle":"2022-08-11T21:17:28.981804Z","shell.execute_reply.started":"2022-08-11T21:17:28.443186Z","shell.execute_reply":"2022-08-11T21:17:28.980288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def countplot(var):\n    sns.countplot(data = train_data, x = var, hue = 'Survived', palette=sns.color_palette())\n    plt.show()\ncountplot('Sex')\ncountplot('Pclass')\ncountplot('Embarked')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:34.499950Z","iopub.execute_input":"2022-08-11T21:17:34.500402Z","iopub.status.idle":"2022-08-11T21:17:35.182050Z","shell.execute_reply.started":"2022-08-11T21:17:34.500366Z","shell.execute_reply":"2022-08-11T21:17:35.180747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Validation:\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error\n\n# split data into training and validation data, for both features and target\n# The split is based on a random number generator. Supplying a numeric value to\n# the random_state argument guarantees we get the same split every time we\n# run this script.\ntrain_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 1)\n\n# Specify the model\ntitanic_model = DecisionTreeRegressor(random_state=1)\n\n# Fit titanic_model with the training data.\ntitanic_model.fit(train_X, train_y) \n\n# Make validation predictions and calculate mean absolute error:\n\n# Predict with all validation observations\nval_predictions = titanic_model.predict(val_X)\n\n# print the top few validation predictions\nprint(val_predictions)\n\n#calculate mean absolute error\nval_mae = mean_absolute_error(val_y, val_predictions)\n# print the MAE\nprint(val_mae)\nprint(\"Validation MAE: {:,.0f}\".format(val_mae))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:17:40.292215Z","iopub.execute_input":"2022-08-11T21:17:40.292764Z","iopub.status.idle":"2022-08-11T21:17:40.319082Z","shell.execute_reply.started":"2022-08-11T21:17:40.292717Z","shell.execute_reply":"2022-08-11T21:17:40.317780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing Values\n#Step 1: Preliminary investigation\n# Shape of training data (num_rows, num_columns)\nprint(train_X.shape)\n\n# Number of missing values in each column of training data\nmissing_val_count_by_column = (train_X.isnull().sum())\nprint(missing_val_count_by_column[missing_val_count_by_column > 0])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:21:03.114983Z","iopub.execute_input":"2022-08-11T21:21:03.115446Z","iopub.status.idle":"2022-08-11T21:21:03.125424Z","shell.execute_reply.started":"2022-08-11T21:21:03.115409Z","shell.execute_reply":"2022-08-11T21:21:03.124062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Cross Validation\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\n\nmy_pipeline = Pipeline(steps=[\n    ('preprocessor', SimpleImputer()),\n    ('model', RandomForestRegressor(n_estimators=50, random_state=0))\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:23:57.682294Z","iopub.execute_input":"2022-08-11T21:23:57.682739Z","iopub.status.idle":"2022-08-11T21:23:57.702656Z","shell.execute_reply.started":"2022-08-11T21:23:57.682704Z","shell.execute_reply":"2022-08-11T21:23:57.701282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\n\n# Multiply by -1 since sklearn calculates *negative* MAE\nscores = -1 * cross_val_score(my_pipeline, X, y,\n                              cv=5,\n                              scoring='neg_mean_absolute_error')\n\nprint(\"Average MAE score:\", scores.mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T21:24:20.132384Z","iopub.execute_input":"2022-08-11T21:24:20.132842Z","iopub.status.idle":"2022-08-11T21:24:20.559941Z","shell.execute_reply.started":"2022-08-11T21:24:20.132807Z","shell.execute_reply":"2022-08-11T21:24:20.558619Z"},"trusted":true},"execution_count":null,"outputs":[]}]}