IIS_2023_1/istyukov_timofey_lab1/lab1.py
2023-12-10 15:35:33 +04:00

101 lines
3.9 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.

# 12 вариант
# Данные: make_classification (n_samples=500, n_features=2, n_redundant=0,
# n_informative=2, random_state=rs, n_clusters_per_class=1)
# Модели:
# -- Линейную регрессию
# -- Персептрон
# -- Гребневую полиномиальную регрессию (со степенью 4, alpha = 1.0)
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LinearRegression, Perceptron, Ridge
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
cm_bright_1 = ListedColormap(['#7FFFD4', '#00FFFF'])
cm_bright_2 = ListedColormap(['#FF69B4', '#FF1493'])
def main():
X, y = make_classification(
n_samples=500,
n_features=2,
n_redundant=0,
n_informative=2,
random_state=0,
n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=10, random_state=40)
# модели на основе сгенерированных данных
my_linear_regression(X_train, X_test, y_train, y_test)
my_perceptron(X_train, X_test, y_train, y_test)
my_poly_ridge(X_train, X_test, y_train, y_test)
# Линейная регрессия
def my_linear_regression(X_train, X_test, y_train, y_test):
lin_reg_model = LinearRegression() # создание модели регрессии
lin_reg_model.fit(X_train, y_train) # обучение
y_pred = lin_reg_model.predict(X_test) # предсказание по тестовым даннным
# вывод в консоль
print()
print('===> Линейная регрессия <===')
print('Оценка точности: ', lin_reg_model.score(X_train, y_train))
# вывод в график
plt.title('Линейная регрессия')
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright_1)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright_2, alpha=0.8)
plt.plot(X_test, y_pred, color='red', linewidth=1)
plt.savefig('1_linear_regression.png')
plt.show()
# Персептрон
def my_perceptron(X_train, X_test, y_train, y_test):
perceptron_model = Perceptron()
perceptron_model.fit(X_train, y_train)
y_pred = perceptron_model.predict(X_test)
# вывод в консоль
print()
print('===> Персептрон <===')
print('Оценка точности: ', perceptron_model.score(X_train, y_train))
# вывод в график
plt.title('Персептрон')
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright_1)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright_2, alpha=0.8)
plt.plot(X_test, y_pred, color='red', linewidth=1)
plt.savefig('2_perceptron.png')
plt.show()
# Гребневая полиномиальная регрессия (степень=4, alpha=1.0)
def my_poly_ridge(X_train, X_test, y_train, y_test):
poly_rige_model = make_pipeline(PolynomialFeatures(degree=4), Ridge(alpha=1.0))
poly_rige_model.fit(X_train, y_train)
y_pred = poly_rige_model.predict(X_test)
# вывод в консоль
print()
print('===> Гребневая полиномиальная регрессия <===')
print('Оценка точности: ', poly_rige_model.score(X_train, y_train))
# вывод в график
plt.title('Гребневая полиномиальная регрессия')
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright_1)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright_2, alpha=0.8)
plt.plot(X_test, y_pred, color='red', linewidth=1)
plt.savefig('3_poly_ridge.png')
plt.show()
main()