2023-11-02 23:09:40 +04:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from sklearn.datasets import make_classification
|
|
|
|
from sklearn.linear_model import Perceptron
|
|
|
|
from sklearn.neural_network import MLPClassifier
|
2023-11-03 13:11:00 +04:00
|
|
|
from sklearn.model_selection import train_test_split
|
2023-11-02 23:09:40 +04:00
|
|
|
from sklearn.metrics import accuracy_score
|
|
|
|
|
|
|
|
# Наполнение искусственными данными
|
|
|
|
rs = np.random.RandomState(42)
|
|
|
|
X, y = make_classification(n_samples=500, n_features=2, n_redundant=0, n_informative=2, random_state=rs,
|
2023-11-03 13:11:00 +04:00
|
|
|
n_clusters_per_class=1)
|
2023-11-02 23:09:40 +04:00
|
|
|
|
|
|
|
# Обучающие и тестовые наборы данных
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=rs)
|
|
|
|
|
|
|
|
# Список моделей для обучения
|
|
|
|
models = [
|
2023-11-03 13:11:00 +04:00
|
|
|
('Перцептрон', Perceptron()),
|
|
|
|
('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))
|
2023-11-02 23:09:40 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
fig, axs = plt.subplots(1, len(models), figsize=(12, 4))
|
|
|
|
|
|
|
|
# Визуализация графиков
|
|
|
|
for i, (name, model) in enumerate(models):
|
2023-11-03 13:11:00 +04:00
|
|
|
model.fit(X_train, y_train)
|
|
|
|
y_pred = model.predict(X_test)
|
|
|
|
accuracy = accuracy_score(y_test, y_pred)
|
2023-11-02 23:09:40 +04:00
|
|
|
|
2023-11-03 13:11:00 +04:00
|
|
|
axs[i].scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap=plt.cm.Paired)
|
|
|
|
axs[i].set_title(f'{name} (Accuracy: {accuracy:.2f})')
|
|
|
|
axs[i].set_xlabel("Размер обучающего набора")
|
|
|
|
axs[i].set_ylabel("Средняя точность модели")
|
2023-11-02 23:09:40 +04:00
|
|
|
|
|
|
|
plt.show()
|