{ "cells": [ { "cell_type": "markdown", "id": "ebfd86d8-637c-4d39-bda3-fc144ba8f184", "metadata": {}, "source": [ "# Решающие деревья" ] }, { "cell_type": "markdown", "id": "ffd87cac-d792-4b3a-9f75-adea1ba08f52", "metadata": {}, "source": [ "### 1. Загрузка выборки из файла titanic.csv" ] }, { "cell_type": "code", "execution_count": 17, "id": "33f4c4d8-11aa-4647-8e94-8c211a18942c", "metadata": {}, "outputs": [], "source": [ "from sklearn.tree import DecisionTreeClassifier\n", "import pandas as pd\n", "\n", "# Шаг 1: Загрузим данные\n", "data = pd.read_csv('titanic.csv')" ] }, { "cell_type": "markdown", "id": "93cbfe78-75ae-4815-b5a1-93f06bb366e9", "metadata": {}, "source": [ "### 2. Оставим нужные признаки: Pclass, Fare, Age и Sex" ] }, { "cell_type": "code", "execution_count": 18, "id": "c3a99cf2-7864-4e59-a5f3-c4a1c80fe468", "metadata": {}, "outputs": [], "source": [ "features = data[['Pclass', 'Fare', 'Age', 'Sex', 'Survived']].copy()" ] }, { "cell_type": "markdown", "id": "708757cc-244d-435f-8f92-d126baef1c6f", "metadata": {}, "source": [ "### 3. Преобразуем пол в числовой формат (male -> 0, female -> 1)" ] }, { "cell_type": "code", "execution_count": 19, "id": "f22dcc08-d6db-4687-b189-93b359a9c3f9", "metadata": {}, "outputs": [], "source": [ "features['Sex'] = features['Sex'].map({'male': 0, 'female': 1})" ] }, { "cell_type": "markdown", "id": "55500700-5fef-4d82-ab87-442fe23184c9", "metadata": {}, "source": [ "### 5. Удалим строки с пропусками в признаках" ] }, { "cell_type": "code", "execution_count": 20, "id": "520e50cd-9a9c-4de5-b7be-592af3b4cb97", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Pclass | \n", "Fare | \n", "Age | \n", "Sex | \n", "Survived | \n", "
---|---|---|---|---|---|
0 | \n", "3 | \n", "7.2500 | \n", "22.0 | \n", "0 | \n", "0 | \n", "
1 | \n", "1 | \n", "71.2833 | \n", "38.0 | \n", "1 | \n", "1 | \n", "
2 | \n", "3 | \n", "7.9250 | \n", "26.0 | \n", "1 | \n", "1 | \n", "
3 | \n", "1 | \n", "53.1000 | \n", "35.0 | \n", "1 | \n", "1 | \n", "
4 | \n", "3 | \n", "8.0500 | \n", "35.0 | \n", "0 | \n", "0 | \n", "
DecisionTreeClassifier(random_state=241)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
DecisionTreeClassifier(random_state=241)