4lab/week4_scikit_learn.ipynb

87 lines
3.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "df20729e-7575-4880-a900-46b2b8bd82e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 10\n",
" 1 1.00 1.00 1.00 9\n",
" 2 1.00 1.00 1.00 11\n",
"\n",
" accuracy 1.00 30\n",
" macro avg 1.00 1.00 1.00 30\n",
"weighted avg 1.00 1.00 1.00 30\n",
"\n"
]
}
],
"source": [
"# Импорт необходимых модулей\n",
"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",
"\n",
"# 1. Загрузка данных\n",
"# load_iris() загружает встроенный набор данных Iris (ирисы Фишера)\n",
"# return_X_y=True возвращает данные в виде матрицы признаков (X) и вектора меток (y)\n",
"X, y = load_iris(return_X_y=True)\n",
"\n",
"# 2. Разделение данных на обучающую и тестовую выборки\n",
"# test_size=0.2 означает, что 20% данных пойдут в тестовую выборку\n",
"# random_state можно задать для воспроизводимости результатов\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"# 3. Создание и обучение модели нейронной сети\n",
"# MLPClassifier - многослойный перцептрон\n",
"# hidden_layer_sizes=(10,) - один скрытый слой с 10 нейронами\n",
"# activation='relu' - функция активации Rectified Linear Unit\n",
"# max_iter=500 - максимальное количество итераций обучения\n",
"clf = MLPClassifier(hidden_layer_sizes=(10,), activation='relu', max_iter=10000)\n",
"clf.fit(X_train, y_train) # Обучение модели на тренировочных данных\n",
"\n",
"# 4. Оценка модели\n",
"# classification_report выводит отчет с основными метриками качества\n",
"print(classification_report(y_test, clf.predict(X_test)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af5eae95-22ed-46c6-8f3e-befb67a2d9e6",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}