IIS_2023_1/madyshev_egor_lab_1/main.py

51 lines
2.5 KiB
Python
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.

import random
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Perceptron
rs = random.randrange(100)
X, y = make_moons(n_samples=200, noise=0.3, random_state=rs)
X_train, X_Test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
print("Линейная регрессия")
linerModel = LinearRegression().fit(X_train, y_train)
print("результат модели на учебных данных =", linerModel.score(X_train, y_train))
print("результат модели на тестовых данных =", linerModel.score(X_Test, y_test))
print("Многослойный персептрон с 10-ю нейронами в скрытом слое")
mlp = MLPClassifier(hidden_layer_sizes=(10), alpha = 0.01, max_iter=2000).fit(X_train, y_train)
print("результат модели на учебных данных =", mlp.score(X_train, y_train))
print("результат модели на тестовых данных =", mlp.score(X_Test, y_test))
print("Персептрон ")
perceptron = Perceptron().fit(X_train, y_train)
print("результат модели на учебных данных =", perceptron.score(X_train, y_train))
print("результат модели на тестовых данных =", perceptron.score(X_Test, y_test))
plt.xlabel("Свойство 1")
plt.ylabel("Свойство 2")
plt.title("Сгенерированные данные")
plt.scatter(X[:, 0], X[:, 1], c = y, cmap = plt.cm.Set1)
plt.show()
h = 0.01
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
def showPlot(name, model):
plt.title(name)
c = model.predict(np.c_[xx.ravel(), yy.ravel()])
c = np.where(c >= 0.5, 1, 0)
c = c.reshape(xx.shape)
plt.contourf(xx, yy, c, cmap = plt.cm.Set1, alpha=0.2)
plt.scatter(X[:, 0], X[:, 1], c = y, cmap = plt.cm.Set1)
plt.show()
showPlot("Линейная регрессия", linerModel)
showPlot("Многослойный персептрон", mlp)
showPlot("Персептрон", perceptron)