IIS_2023_1/alexandrov_dmitrii_lab_1/lab1.py

99 lines
3.2 KiB
Python
Raw Normal View History

2023-09-19 10:19:50 +04:00
import random
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.datasets import make_moons
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
def start():
rs = random.randrange(10)
rs = 5
X, y = make_moons(n_samples=250, 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)
lin = LinearRegression()
poly = Pipeline([('poly', PolynomialFeatures(degree=3)),
('linear', LinearRegression())])
ridge = Pipeline([('poly', PolynomialFeatures(degree=3)),
('ridge', Ridge(alpha=1.0))])
figure = plt.figure(1, figsize=(16, 9))
axis = figure.subplots(4, 3)
cm = ListedColormap(['#FF0000', "#0000FF"])
arr_res = list(range(len(y_test)))
X_scale = list(range(len(y_test)))
lin.fit(X_train, y_train)
res_y = lin.predict(X_test)
print(lin.score(X_test, y_test))
axis[0, 0].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
axis[1, 0].scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm)
axis[3, 0].plot([i for i in range(len(res_y))], y_test, c="g")
axis[3, 0].plot([i for i in range(len(res_y))], res_y, c="r")
for i in range(len(X_test)):
arr_res[i] = [X_test[i], res_y[i], y_test[i]]
arr_res = sorted(arr_res, key=lambda x: x[1])
for i in range(len(X_test)):
X_scale[i] = arr_res[i][0]
res_y[i] = arr_res[i][1]
arr_res[i] = arr_res[i][2]
axis[2, 0].plot(X_scale, arr_res, c="g")
axis[2, 0].plot(X_scale, res_y, c="r")
poly.fit(X_train, y_train)
res_y = poly.predict(X_test)
print(poly.score(X_test, y_test))
axis[0, 1].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
axis[1, 1].scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm)
axis[3, 1].plot([i for i in range(len(res_y))], y_test, c="g")
axis[3, 1].plot([i for i in range(len(res_y))], res_y, c="r")
for i in range(len(X_test)):
arr_res[i] = [X_test[i], res_y[i], y_test[i]]
arr_res = sorted(arr_res, key=lambda x: x[1])
for i in range(len(X_test)):
X_scale[i] = arr_res[i][0]
res_y[i] = arr_res[i][1]
arr_res[i] = arr_res[i][2]
axis[2, 1].plot(X_scale, arr_res, c="g")
axis[2, 1].plot(X_scale, res_y, c="r")
ridge.fit(X_train, y_train)
res_y = ridge.predict(X_test)
print(ridge.score(X_test, y_test))
axis[0, 2].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
axis[1, 2].scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm)
axis[3, 2].plot([i for i in range(len(res_y))], y_test, c="g")
axis[3, 2].plot([i for i in range(len(res_y))], res_y, c="r")
for i in range(len(X_test)):
arr_res[i] = [X_test[i], res_y[i], y_test[i]]
arr_res = sorted(arr_res, key=lambda x: x[1])
for i in range(len(X_test)):
X_scale[i] = arr_res[i][0]
res_y[i] = arr_res[i][1]
arr_res[i] = arr_res[i][2]
axis[2, 2].plot(X_scale, arr_res, c="g")
axis[2, 2].plot(X_scale, res_y, c="r")
plt.show()
start()