From 1e252c4c5fc1d20a128f1008eb6b04b6713f42d8 Mon Sep 17 00:00:00 2001 From: stud203791 Date: Mon, 4 May 2026 17:48:14 +0300 Subject: [PATCH] final version --- .../week4_scikit_learn-checkpoint.ipynb | 6 + week4_scikit_learn.ipynb | 164 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 .ipynb_checkpoints/week4_scikit_learn-checkpoint.ipynb create mode 100644 week4_scikit_learn.ipynb diff --git a/.ipynb_checkpoints/week4_scikit_learn-checkpoint.ipynb b/.ipynb_checkpoints/week4_scikit_learn-checkpoint.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/.ipynb_checkpoints/week4_scikit_learn-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/week4_scikit_learn.ipynb b/week4_scikit_learn.ipynb new file mode 100644 index 0000000..884f738 --- /dev/null +++ b/week4_scikit_learn.ipynb @@ -0,0 +1,164 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "7f8f4a9c-fddd-483a-b048-9afd30fb3bc2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "==================================================\n", + "MLPClassifier\n", + "==================================================\n", + "\n", + "1. max_iter = 100 (недообучение)\n", + " Точность на тесте: 0.4000\n", + " precision recall f1-score support\n", + "\n", + " 0 1.00 1.00 1.00 10\n", + " 1 0.00 0.00 0.00 9\n", + " 2 0.18 0.18 0.18 11\n", + "\n", + " accuracy 0.40 30\n", + " macro avg 0.39 0.39 0.39 30\n", + "weighted avg 0.40 0.40 0.40 30\n", + "\n", + "\n", + "2. max_iter = 500 (оптимальное)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Пользователь\\PycharmProjects\\PythonProject7\\week4-feature-selection\\venv\\Lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:785: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn't converged yet.\n", + " warnings.warn(\n", + "C:\\Users\\Пользователь\\PycharmProjects\\PythonProject7\\week4-feature-selection\\venv\\Lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:785: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Точность на тесте: 0.9333\n", + " precision recall f1-score support\n", + "\n", + " 0 1.00 1.00 1.00 10\n", + " 1 1.00 0.78 0.88 9\n", + " 2 0.85 1.00 0.92 11\n", + "\n", + " accuracy 0.93 30\n", + " macro avg 0.95 0.93 0.93 30\n", + "weighted avg 0.94 0.93 0.93 30\n", + "\n", + "\n", + "3. max_iter = 2500 (долгое обучение)\n", + " Точность на тесте: 0.9667\n", + " precision recall f1-score support\n", + "\n", + " 0 1.00 1.00 1.00 10\n", + " 1 1.00 0.89 0.94 9\n", + " 2 0.92 1.00 0.96 11\n", + "\n", + " accuracy 0.97 30\n", + " macro avg 0.97 0.96 0.97 30\n", + "weighted avg 0.97 0.97 0.97 30\n", + "\n", + "\n", + "==================================================\n", + "ВЫВОДЫ\n", + "==================================================\n", + "max_iter=100 → точность: 0.4000 (недообучение)\n", + "max_iter=500 → точность: 0.9333 (оптимально)\n", + "max_iter=2500 → точность: 0.9667 (могло переобучиться)\n" + ] + } + ], + "source": [ + "from sklearn.datasets import load_iris\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.neural_network import MLPClassifier\n", + "from sklearn.metrics import classification_report\n", + "import numpy as np\n", + "\n", + "# Загрузка и разбиение данных\n", + "X, y = load_iris(return_X_y=True)\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", + "\n", + "print(\"=\"*50)\n", + "print(\"MLPClassifier\")\n", + "print(\"=\"*50)\n", + "\n", + "# max_iter = 100 (недообучение)\n", + "print(\"\\n1. max_iter = 100 (недообучение)\")\n", + "clf_100 = MLPClassifier(hidden_layer_sizes=(10,), activation='relu', max_iter=100, random_state=42)\n", + "clf_100.fit(X_train, y_train)\n", + "print(f\" Точность на тесте: {clf_100.score(X_test, y_test):.4f}\")\n", + "print(classification_report(y_test, clf_100.predict(X_test), zero_division=0))\n", + "\n", + "# max_iter = 500 (оптимальное)\n", + "print(\"\\n2. max_iter = 500 (оптимальное)\")\n", + "clf_500 = MLPClassifier(hidden_layer_sizes=(10,), activation='relu', max_iter=500, random_state=42)\n", + "clf_500.fit(X_train, y_train)\n", + "print(f\" Точность на тесте: {clf_500.score(X_test, y_test):.4f}\")\n", + "print(classification_report(y_test, clf_500.predict(X_test)))\n", + "\n", + "# max_iter = 2500 (переобучение)\n", + "print(\"\\n3. max_iter = 2500 (долгое обучение)\")\n", + "clf_2500 = MLPClassifier(hidden_layer_sizes=(10,), activation='relu', max_iter=2500, random_state=42)\n", + "clf_2500.fit(X_train, y_train)\n", + "print(f\" Точность на тесте: {clf_2500.score(X_test, y_test):.4f}\")\n", + "print(classification_report(y_test, clf_2500.predict(X_test)))\n", + "\n", + "# Вывод\n", + "print(\"\\n\" + \"=\"*50)\n", + "print(\"ВЫВОДЫ\")\n", + "print(\"=\"*50)\n", + "print(f\"max_iter=100 → точность: {clf_100.score(X_test, y_test):.4f} (недообучение)\")\n", + "print(f\"max_iter=500 → точность: {clf_500.score(X_test, y_test):.4f} (оптимально)\")\n", + "print(f\"max_iter=2500 → точность: {clf_2500.score(X_test, y_test):.4f} (могло переобучиться)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "555f6188-4462-4b9c-b17d-fa0788a7e3eb", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ccb37ef-fb03-406c-8017-d0b74a00ea38", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}