{"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 numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport os\nimport seaborn as sns\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import cross_val_score","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T08:28:40.650658Z","iopub.execute_input":"2022-07-05T08:28:40.651509Z","iopub.status.idle":"2022-07-05T08:28:42.477906Z","shell.execute_reply.started":"2022-07-05T08:28:40.651405Z","shell.execute_reply":"2022-07-05T08:28:42.476449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Читаем файлы","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:42.480109Z","iopub.execute_input":"2022-07-05T08:28:42.480776Z","iopub.status.idle":"2022-07-05T08:28:42.513014Z","shell.execute_reply.started":"2022-07-05T08:28:42.480741Z","shell.execute_reply":"2022-07-05T08:28:42.511908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:42.514819Z","iopub.execute_input":"2022-07-05T08:28:42.515527Z","iopub.status.idle":"2022-07-05T08:28:42.562115Z","shell.execute_reply.started":"2022-07-05T08:28:42.515474Z","shell.execute_reply":"2022-07-05T08:28:42.560907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Процент погибших и выживших","metadata":{}},{"cell_type":"code","source":"train['Survived'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:42.566263Z","iopub.execute_input":"2022-07-05T08:28:42.567181Z","iopub.status.idle":"2022-07-05T08:28:42.590194Z","shell.execute_reply.started":"2022-07-05T08:28:42.567118Z","shell.execute_reply":"2022-07-05T08:28:42.587646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Как на спасение повлияли цена билета и класс**","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,5))\nsns.swarmplot(x='Pclass', y='Fare', data=train, hue='Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:42.592025Z","iopub.execute_input":"2022-07-05T08:28:42.596158Z","iopub.status.idle":"2022-07-05T08:28:50.000685Z","shell.execute_reply.started":"2022-07-05T08:28:42.596083Z","shell.execute_reply":"2022-07-05T08:28:49.999045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Шанс на спасение в зависимости от пола и класса билета**","metadata":{}},{"cell_type":"code","source":"train.groupby(['Pclass', 'Sex'])['Survived'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.003982Z","iopub.execute_input":"2022-07-05T08:28:50.004826Z","iopub.status.idle":"2022-07-05T08:28:50.018441Z","shell.execute_reply.started":"2022-07-05T08:28:50.004698Z","shell.execute_reply":"2022-07-05T08:28:50.017515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"**Гистограмма по столбцу возраст**","metadata":{}},{"cell_type":"code","source":"sns.histplot(train['Age'].dropna())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.020130Z","iopub.execute_input":"2022-07-05T08:28:50.021312Z","iopub.status.idle":"2022-07-05T08:28:50.271995Z","shell.execute_reply.started":"2022-07-05T08:28:50.021254Z","shell.execute_reply":"2022-07-05T08:28:50.270647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Посмотрим какие значения содержит столбец \"Стоимость\"","metadata":{}},{"cell_type":"code","source":"plt.hist(train['Fare'], bins=10)\nplt.xlabel('Fare')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.273747Z","iopub.execute_input":"2022-07-05T08:28:50.274067Z","iopub.status.idle":"2022-07-05T08:28:50.474829Z","shell.execute_reply.started":"2022-07-05T08:28:50.274038Z","shell.execute_reply":"2022-07-05T08:28:50.473307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Много нулевых значений, что навряд ли является правдой, заменим на Nan объединими оба датасета, чтобы выполнять над ними одни и те же операции**","metadata":{}},{"cell_type":"code","source":"# объединим данные\nfull_data = [train, test]\n\nfor data in full_data:\n    data.loc[data['Fare'] == 0, 'Fare'] = np.NaN","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.476549Z","iopub.execute_input":"2022-07-05T08:28:50.476944Z","iopub.status.idle":"2022-07-05T08:28:50.485135Z","shell.execute_reply.started":"2022-07-05T08:28:50.476911Z","shell.execute_reply":"2022-07-05T08:28:50.483850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Посмотрим на столбец Embarked","metadata":{}},{"cell_type":"code","source":"sns.countplot(x='Embarked', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.490840Z","iopub.execute_input":"2022-07-05T08:28:50.491466Z","iopub.status.idle":"2022-07-05T08:28:50.675001Z","shell.execute_reply.started":"2022-07-05T08:28:50.491425Z","shell.execute_reply":"2022-07-05T08:28:50.673638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Посмотрим на столбец SibSp","metadata":{}},{"cell_type":"code","source":"sns.countplot(x='SibSp', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.676599Z","iopub.execute_input":"2022-07-05T08:28:50.677210Z","iopub.status.idle":"2022-07-05T08:28:50.883171Z","shell.execute_reply.started":"2022-07-05T08:28:50.677166Z","shell.execute_reply":"2022-07-05T08:28:50.881862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Посмотрим на столбец Parch","metadata":{}},{"cell_type":"code","source":"sns.countplot(x='Parch', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:50.885353Z","iopub.execute_input":"2022-07-05T08:28:50.885847Z","iopub.status.idle":"2022-07-05T08:28:51.087508Z","shell.execute_reply.started":"2022-07-05T08:28:50.885801Z","shell.execute_reply":"2022-07-05T08:28:51.086279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Большинство пассажиров были без родственников","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.089438Z","iopub.execute_input":"2022-07-05T08:28:51.089934Z","iopub.status.idle":"2022-07-05T08:28:51.118000Z","shell.execute_reply.started":"2022-07-05T08:28:51.089888Z","shell.execute_reply":"2022-07-05T08:28:51.117125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"В именах у нас Мистер и Мисс, создадим новый столбец","metadata":{}},{"cell_type":"code","source":"for data in full_data:\n    data['Title'] = data['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.119044Z","iopub.execute_input":"2022-07-05T08:28:51.119984Z","iopub.status.idle":"2022-07-05T08:28:51.129439Z","shell.execute_reply.started":"2022-07-05T08:28:51.119943Z","shell.execute_reply":"2022-07-05T08:28:51.128544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Смотрим, что получилось","metadata":{}},{"cell_type":"code","source":"train['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.130702Z","iopub.execute_input":"2022-07-05T08:28:51.131692Z","iopub.status.idle":"2022-07-05T08:28:51.144277Z","shell.execute_reply.started":"2022-07-05T08:28:51.131635Z","shell.execute_reply":"2022-07-05T08:28:51.143376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Заменим все мужские имена на Mr и женские на Miss, а также заменим мужчин на ноль/женщин на один","metadata":{}},{"cell_type":"code","source":"for data in full_data:\n    data['Title'].replace(['Mme', 'Ms', 'Lady', 'Mlle', 'the Countess', 'Dona'], 'Miss', inplace=True)\n    data['Title'].replace(['Major', 'Col', 'Capt', 'Don', 'Sir', 'Jonkheer'], 'Mr', inplace=True)\n    \n    data['Sex'].replace(['male', 'female'], [1,0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.145477Z","iopub.execute_input":"2022-07-05T08:28:51.146854Z","iopub.status.idle":"2022-07-05T08:28:51.165064Z","shell.execute_reply.started":"2022-07-05T08:28:51.146811Z","shell.execute_reply":"2022-07-05T08:28:51.163520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Создадим столбец, в который запишем длину строки билета\nСоздадим отдельный столбец каюты","metadata":{}},{"cell_type":"code","source":"for data in full_data:\n    data['Ticket_2letter'] = data.Ticket.apply(lambda x: x[:2])\n    data['Ticket_len'] = data.Ticket.apply(lambda x: len(x))\n    \n    data['Cabin_num'] = data.Ticket.apply(lambda x: len(x.split()))\n    data['Cabin_1letter'] = data.Ticket.apply(lambda x: x[:1]) #извлечём начальную букву номера билетf\n    \n    data['Fam_size'] = data['SibSp'] + data['Parch'] + 1 #посмотрим как повлияло кол-во родственников на выживание, создаём новый столбец размер семью (сумма SibSp и Parch)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.166983Z","iopub.execute_input":"2022-07-05T08:28:51.168137Z","iopub.status.idle":"2022-07-05T08:28:51.190497Z","shell.execute_reply.started":"2022-07-05T08:28:51.168091Z","shell.execute_reply":"2022-07-05T08:28:51.188994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Посмотрим, какие семьи получились","metadata":{}},{"cell_type":"code","source":"train.Fam_size.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.193430Z","iopub.execute_input":"2022-07-05T08:28:51.193819Z","iopub.status.idle":"2022-07-05T08:28:51.202612Z","shell.execute_reply.started":"2022-07-05T08:28:51.193787Z","shell.execute_reply":"2022-07-05T08:28:51.201272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Сгруппируем семьи по категориям","metadata":{}},{"cell_type":"code","source":"for data in full_data:\n    data['Fam_type'] = pd.cut(data.Fam_size, [0,1,4,7,11], labels=['Solo', 'Small', 'Big', 'Very big'])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.204388Z","iopub.execute_input":"2022-07-05T08:28:51.205193Z","iopub.status.idle":"2022-07-05T08:28:51.218343Z","shell.execute_reply.started":"2022-07-05T08:28:51.205124Z","shell.execute_reply":"2022-07-05T08:28:51.217265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Начинаем разработки модели","metadata":{}},{"cell_type":"code","source":"y = train['Survived'] #присвоем столбец\nfeatures = ['Pclass', 'Fare', 'Title', 'Embarked', 'Fam_type', 'Ticket_len', 'Ticket_2letter'] #возьмём такой набор данных\nX = train[features] #записываем данные этих столбцов","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.219895Z","iopub.execute_input":"2022-07-05T08:28:51.220252Z","iopub.status.idle":"2022-07-05T08:28:51.229531Z","shell.execute_reply.started":"2022-07-05T08:28:51.220222Z","shell.execute_reply":"2022-07-05T08:28:51.228332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Запишем в разные переменные названия числовых столбцов и столбцов с категориальными данными","metadata":{}},{"cell_type":"code","source":"numerical_cols = ['Fare']\ncategorical_cols = ['Pclass', 'Title', 'Embarked', 'Fam_type', 'Ticket_len', 'Ticket_2letter']\n        \n\nnumerical_transformer = SimpleImputer(strategy='median') #пропущенные значения в столбцах заполним медианой\n       \n\ncategorical_transformer = Pipeline(steps=[     #пропущенные значения заполняем чаще всего встречающемися \n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore')) #если встретится что-то неизвестное, кидаем в игнор (чтобы не возникала ошибка)\n])\n      \n\npreprocessor = ColumnTransformer(    #объединим предпроцессинг для числовых и категориальных данных\n    transformers=[\n        ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ])\n       \n\ntitanic_pipeline = Pipeline(steps=[     #объединим предпроцессинг и моделирование\n    ('preprocessor', preprocessor),\n    ('model', RandomForestClassifier(random_state=0, n_estimators=500, max_depth=5))\n])\n\n\n#обучение модели\ntitanic_pipeline.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:51.230920Z","iopub.execute_input":"2022-07-05T08:28:51.231510Z","iopub.status.idle":"2022-07-05T08:28:52.367291Z","shell.execute_reply.started":"2022-07-05T08:28:51.231475Z","shell.execute_reply":"2022-07-05T08:28:52.365993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# применим модель на тестовых данных\n\nX_test = test[features]\npredictions = titanic_pipeline.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:28:52.369088Z","iopub.execute_input":"2022-07-05T08:28:52.369652Z","iopub.status.idle":"2022-07-05T08:28:52.476365Z","shell.execute_reply.started":"2022-07-05T08:28:52.369599Z","shell.execute_reply":"2022-07-05T08:28:52.475020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})\noutput.to_csv('titanic.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:30:24.095704Z","iopub.execute_input":"2022-07-05T08:30:24.096137Z","iopub.status.idle":"2022-07-05T08:30:24.106650Z","shell.execute_reply.started":"2022-07-05T08:30:24.096101Z","shell.execute_reply":"2022-07-05T08:30:24.105009Z"},"trusted":true},"execution_count":null,"outputs":[]}]}