Update main.py

This commit is contained in:
a-shdv 2023-11-03 13:11:00 +04:00
parent ce7cfa4365
commit a8f3b6c692
2 changed files with 12 additions and 20 deletions

View File

@ -3,43 +3,35 @@ import matplotlib.pyplot as plt
from sklearn.datasets import make_classification from sklearn.datasets import make_classification
from sklearn.linear_model import Perceptron from sklearn.linear_model import Perceptron
from sklearn.neural_network import MLPClassifier from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split, learning_curve from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score from sklearn.metrics import accuracy_score
# Наполнение искусственными данными # Наполнение искусственными данными
rs = np.random.RandomState(42) rs = np.random.RandomState(42)
X, y = make_classification(n_samples=500, n_features=2, n_redundant=0, n_informative=2, random_state=rs, X, y = make_classification(n_samples=500, n_features=2, n_redundant=0, n_informative=2, random_state=rs,
n_clusters_per_class=1) n_clusters_per_class=1)
# Обучающие и тестовые наборы данных # Обучающие и тестовые наборы данных
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=rs) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=rs)
# Список моделей для обучения # Список моделей для обучения
models = [ models = [
('Персептрон', Perceptron()), ('Перцептрон', Perceptron()),
('MLP (10 нейронов)', MLPClassifier(hidden_layer_sizes=(10,), alpha=0.01, random_state=rs)), ('MLP (10 нейронов)', MLPClassifier(hidden_layer_sizes=(10,), alpha=0.01, random_state=rs)),
('MLP (100 нейронов)', MLPClassifier(hidden_layer_sizes=(100,), alpha=0.01, random_state=rs)) ('MLP (100 нейронов)', MLPClassifier(hidden_layer_sizes=(100,), alpha=0.01, random_state=rs))
] ]
fig, axs = plt.subplots(1, len(models), figsize=(12, 4)) fig, axs = plt.subplots(1, len(models), figsize=(12, 4))
# Визуализация графиков # Визуализация графиков
for i, (name, model) in enumerate(models): for i, (name, model) in enumerate(models):
model.fit(X_train, y_train) model.fit(X_train, y_train)
y_pred = model.predict(X_test) y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred) accuracy = accuracy_score(y_test, y_pred)
# Построение кривых обуч ения axs[i].scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap=plt.cm.Paired)
train_sizes, train_scores, valid_scores = learning_curve( axs[i].set_title(f'{name} (Accuracy: {accuracy:.2f})')
model, X, y, train_sizes=[50, 80, 110], cv=5) axs[i].set_xlabel("Размер обучающего набора")
axs[i].plot(train_sizes, train_scores.mean(axis=1), 'o-', color="r", axs[i].set_ylabel("Средняя точность модели")
label="Оценка обучения")
axs[i].plot(train_sizes, valid_scores.mean(axis=1), 'o-', color="g",
label="Оценка кросс-валидации")
axs[i].set_title(f'{name} (Точность: {accuracy:.2f})')
axs[i].set_xlabel("Training examples")
axs[i].set_ylabel("Score")
axs[i].legend(loc="best")
axs[i].grid()
plt.show() plt.show()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 53 KiB