{"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 matplotlib.pyplot as plt\nimport matplotlib.mlab as mlab\nimport seaborn as sns\nimport matplotlib\nimport pandas as pd\nimport numpy as np\nfrom numpy import *\nplt.style.use('ggplot')\nfrom matplotlib.pyplot import figure\nimport numpy as np\nimport pandas as pd\nimport os\nmatplotlib.rcParams['figure.figsize'] = (12,8)\npd.options.mode.chained_assignment = None\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T09:44:29.581293Z","iopub.execute_input":"2022-07-05T09:44:29.581686Z","iopub.status.idle":"2022-07-05T09:44:29.594474Z","shell.execute_reply.started":"2022-07-05T09:44:29.581654Z","shell.execute_reply":"2022-07-05T09:44:29.592858Z"},"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-07-05T09:44:31.441637Z","iopub.execute_input":"2022-07-05T09:44:31.442038Z","iopub.status.idle":"2022-07-05T09:44:31.464171Z","shell.execute_reply.started":"2022-07-05T09:44:31.442008Z","shell.execute_reply":"2022-07-05T09:44:31.463272Z"},"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-07-05T09:44:35.398469Z","iopub.execute_input":"2022-07-05T09:44:35.398896Z","iopub.status.idle":"2022-07-05T09:44:35.423871Z","shell.execute_reply.started":"2022-07-05T09:44:35.398864Z","shell.execute_reply":"2022-07-05T09:44:35.422930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_heatmap(dataset):\n    cols = dataset.columns\n    colours = ['white', 'black'] \n    sns.heatmap(dataset[cols].isnull(), cmap=sns.color_palette(colours))\nget_heatmap(test_data)\n#карта отсутствующих данных","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:44:37.896219Z","iopub.execute_input":"2022-07-05T09:44:37.897172Z","iopub.status.idle":"2022-07-05T09:44:38.536941Z","shell.execute_reply.started":"2022-07-05T09:44:37.897129Z","shell.execute_reply":"2022-07-05T09:44:38.535545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_data['Fare']\ndel test_data['Cabin']\ndel test_data['Name']\ndel test_data['Embarked']\ndel test_data['Ticket']\ntest_data","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:44:58.832027Z","iopub.execute_input":"2022-07-05T09:44:58.832975Z","iopub.status.idle":"2022-07-05T09:44:58.859120Z","shell.execute_reply.started":"2022-07-05T09:44:58.832934Z","shell.execute_reply":"2022-07-05T09:44:58.857519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_numeric = test_data.select_dtypes(include=[np.number])\nnumeric_cols = test_data_numeric.columns.values\nprint(numeric_cols)\n#отбор числовых колонок\n\nmed = test_data['Age'].median()\nprint(med)\ntest_data['Age'] = test_data['Age'].fillna(med)\n#заполняем пропуски средним значением возраста","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:05.133149Z","iopub.execute_input":"2022-07-05T09:45:05.133599Z","iopub.status.idle":"2022-07-05T09:45:05.147082Z","shell.execute_reply.started":"2022-07-05T09:45:05.133564Z","shell.execute_reply":"2022-07-05T09:45:05.145714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['Sex'].value_counts()\ntest_data['Sex'] = test_data['Sex'].astype('category')\ntest_data['Sex_cat'] = test_data['Sex'].cat.codes\ntest_data\n#кодируем пол с помощью кодирования метки","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:08.795900Z","iopub.execute_input":"2022-07-05T09:45:08.797027Z","iopub.status.idle":"2022-07-05T09:45:08.827329Z","shell.execute_reply.started":"2022-07-05T09:45:08.796966Z","shell.execute_reply":"2022-07-05T09:45:08.826051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test_data.columns:\n    pct_missing = np.mean(test_data[col].isnull())\n    print('{} - {}%'.format(col, round(pct_missing*100)))\n# пропущенные значения отсутствуют","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:11.691771Z","iopub.execute_input":"2022-07-05T09:45:11.692248Z","iopub.status.idle":"2022-07-05T09:45:11.703138Z","shell.execute_reply.started":"2022-07-05T09:45:11.692213Z","shell.execute_reply":"2022-07-05T09:45:11.701463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flg, ax = plt.subplots()\nax.barh(test_data.Age, test_data.PassengerId)\nplt.title('Возрастная категория')\nplt.xlabel('Количество людей')\nplt.ylabel('Возраст')\nax.legend().set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:17.992158Z","iopub.execute_input":"2022-07-05T09:45:17.993101Z","iopub.status.idle":"2022-07-05T09:45:18.920602Z","shell.execute_reply.started":"2022-07-05T09:45:17.993054Z","shell.execute_reply":"2022-07-05T09:45:18.918661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratio_Survived = train_data['Survived'].value_counts()\nfig = plt. figure(figsize=(5,5))\naxes = fig.add_axes ([1, 1, 1, 1])\naxes.pie(ratio_Survived, labels=ratio_Survived.index, autopct='%.1f%%')\naxes.legend(title = 'Survived', loc = 'center left',bbox_to_anchor=(1, 1, 1, 1))\naxes.set(title = 'Выжившие')\naxes.legend().set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:21.664043Z","iopub.execute_input":"2022-07-05T09:45:21.664628Z","iopub.status.idle":"2022-07-05T09:45:21.789999Z","shell.execute_reply.started":"2022-07-05T09:45:21.664549Z","shell.execute_reply":"2022-07-05T09:45:21.787617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/titanic/train.csv')\ntrain_data.head()\n\ndel train_data['Fare']\ndel train_data['Cabin']\ndel train_data['Name']\ndel train_data['Embarked']\ndel train_data['Ticket']\n\ntrain_data_numeric = train_data.select_dtypes(include=[np.number])\nnumeric_cols = train_data_numeric.columns.values\nprint(numeric_cols)\n#отбор числовых колонок\n        \nmed = train_data['Age'].median()\nprint(med)\ntrain_data['Age'] = train_data['Age'].fillna(med)\n#заполняем пропуски средним значением возраста","metadata":{"execution":{"iopub.status.busy":"2022-07-05T09:45:49.720397Z","iopub.execute_input":"2022-07-05T09:45:49.721025Z","iopub.status.idle":"2022-07-05T09:45:49.743654Z","shell.execute_reply.started":"2022-07-05T09:45:49.720986Z","shell.execute_reply":"2022-07-05T09:45:49.742048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\ny = train_data[\"Survived\"]\n\nfeatures = ['Age',\"Pclass\",'Sex', \"SibSp\"]\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-07-05T09:45:54.824177Z","iopub.execute_input":"2022-07-05T09:45:54.824608Z","iopub.status.idle":"2022-07-05T09:45:55.030768Z","shell.execute_reply.started":"2022-07-05T09:45:54.824573Z","shell.execute_reply":"2022-07-05T09:45:55.029400Z"},"trusted":true},"execution_count":null,"outputs":[]}]}