доделка

This commit is contained in:
Дмитрий Александров 2023-09-21 20:19:20 +04:00
parent 9a7b986e00
commit efa81f50bf
2 changed files with 36 additions and 78 deletions

View File

@ -7,21 +7,11 @@ 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
rs = random.randrange(50)
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"])
@ -29,70 +19,38 @@ def start():
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))
def test(col, model):
global axis
global arr_res
global X_test
global X_train
global y_train
global 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")
model.fit(X_train, y_train)
res_y = model.predict(X_test)
print(model.score(X_test, y_test))
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")
axis[0, col].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
axis[1, col].scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm)
axis[2, col].scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm)
axis[2, col].scatter(X_test[:, 0], X_test[:, 1], c=res_y, cmap=cm)
axis[3, col].plot([i for i in range(len(res_y))], y_test, c="g")
axis[3, col].plot([i for i in range(len(res_y))], res_y, c="r")
poly.fit(X_train, y_train)
res_y = poly.predict(X_test)
print(poly.score(X_test, y_test))
def start():
lin = LinearRegression()
poly = Pipeline([('poly', PolynomialFeatures(degree=3)),
('linear', LinearRegression())])
ridge = Pipeline([('poly', PolynomialFeatures(degree=3)),
('ridge', Ridge(alpha=1.0))])
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")
test(0, lin)
test(1, poly)
test(2, ridge)
plt.show()
start()

View File

@ -11,7 +11,7 @@
Файл lab1.py содержит и запускает программу, аргументов и настройки ~~вроде~~ не требует,
###Описание программы
Генерирует один из 10 наборов данных, показывает окно с графиками и пишет оценку моделей обучения по заданию.
Генерирует один из 50 наборов данных, показывает окно с графиками и пишет оценку моделей обучения по заданию.
Использует библиотеки matplotlib для демонстрации графиков и sklearn для создания и использования моделей.
###Результаты тестирования