{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "569c036e-c55b-47f1-8992-b4fe1350262b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Количество образцов: 569\n",
      "Количество признаков: 30\n",
      "\n",
      "Классы: ['malignant' 'benign']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18588 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Точность модели: 0.956\n",
      "\n",
      "Отчет о классификации:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "   malignant       0.97      0.90      0.94        42\n",
      "      benign       0.95      0.99      0.97        72\n",
      "\n",
      "    accuracy                           0.96       114\n",
      "   macro avg       0.96      0.95      0.95       114\n",
      "weighted avg       0.96      0.96      0.96       114\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 22304 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 39496 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 74296 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# %% [markdown]\n",
    "## Классификация опухолей молочной железы с помощью MLPClassifier\n",
    "\n",
    "### Цель задачи\n",
    "# Продемонстрировать работу нейронной сети MLPClassifier на встроенном медицинском датасете.\n",
    "# Необходимо предсказать, является ли опухоль злокачественной или доброкачественной.\n",
    "\n",
    "# %%\n",
    "# Импорт библиотек\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, ConfusionMatrixDisplay\n",
    "\n",
    "# %% [markdown]\n",
    "### Загрузка и анализ данных\n",
    "# %%\n",
    "data = load_breast_cancer()\n",
    "X = data.data\n",
    "y = data.target\n",
    "feature_names = data.feature_names\n",
    "target_names = data.target_names\n",
    "\n",
    "df = pd.DataFrame(X, columns=feature_names)\n",
    "df['target'] = y\n",
    "df['class'] = df['target'].map({i: name for i, name in enumerate(target_names)})\n",
    "\n",
    "print(f\"Количество образцов: {X.shape[0]}\")\n",
    "print(f\"Количество признаков: {X.shape[1]}\")\n",
    "print(\"\\nКлассы:\", target_names)\n",
    "\n",
    "# Распределение классов\n",
    "plt.figure(figsize=(6, 4))\n",
    "df['class'].value_counts().plot(kind='bar')\n",
    "plt.title(\"Распределение классов\")\n",
    "plt.ylabel(\"Количество\")\n",
    "plt.xticks(rotation=0)\n",
    "plt.show()\n",
    "\n",
    "# %% [markdown]\n",
    "### Подготовка данных\n",
    "# %%\n",
    "# Разделение на обучающую и тестовую выборки\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.2, random_state=42, stratify=y)\n",
    "\n",
    "# Масштабирование\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "# %% [markdown]\n",
    "### Обучение модели\n",
    "# %%\n",
    "mlp = MLPClassifier(hidden_layer_sizes=(50, 30),\n",
    "                    activation='relu',\n",
    "                    solver='adam',\n",
    "                    max_iter=1000,\n",
    "                    random_state=42,\n",
    "                    early_stopping=True,\n",
    "                    validation_fraction=0.1)\n",
    "\n",
    "mlp.fit(X_train_scaled, y_train)\n",
    "\n",
    "# %% [markdown]\n",
    "### Оценка модели\n",
    "# %%\n",
    "y_pred = mlp.predict(X_test_scaled)\n",
    "\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"Точность модели: {accuracy:.3f}\")\n",
    "\n",
    "print(\"\\nОтчет о классификации:\")\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))\n",
    "\n",
    "# Матрица ошибок\n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=target_names)\n",
    "disp.plot(cmap=plt.cm.Blues)\n",
    "plt.title(\"Матрица ошибок\")\n",
    "plt.show()\n",
    "\n",
    "# Кривая обучения\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(mlp.loss_curve_, label=\"Ошибка на обучении\")\n",
    "if hasattr(mlp, 'validation_scores_'):\n",
    "    plt.plot(mlp.validation_scores_, label=\"Оценка на валидации\")\n",
    "plt.xlabel(\"Итерации\")\n",
    "plt.ylabel(\"Ошибка\")\n",
    "plt.title(\"Кривая обучения\")\n",
    "plt.grid()\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "# %% [markdown]\n",
    "### Интерпретация результатов\n",
    "# %%\n",
    "if hasattr(mlp, 'coefs_'):\n",
    "    importance = np.abs(mlp.coefs_[0]).sum(axis=1)\n",
    "    sorted_idx = np.argsort(importance)[::-1]\n",
    "\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.bar(range(X.shape[1]), importance[sorted_idx])\n",
    "    plt.xticks(range(X.shape[1]), feature_names[sorted_idx], rotation=90)\n",
    "    plt.title(\"Важность признаков (по весам в первом слое MLP)\")\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "\n",
    "# %% [markdown]\n",
    "### Выводы\n",
    "# 1. Модель показала высокую точность (>95%) на тестовой выборке.\n",
    "# 2. Наиболее важные признаки касаются размера и текстуры опухоли.\n",
    "# 3. Кривая обучения показывает хорошую сходимость и стабильность.\n",
    "# 4. Можно улучшать модель, изменяя архитектуру или используя подбор гиперпараметров.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
