IIS_2023_1/faskhutdinov_idris_lab_1/main.py

91 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.

# 6 вариант
# Данные: make_classification (n_samples=500, n_features=2,
# n_redundant=0, n_informative=2, random_state=rs, n_clusters_per_class=1)
# Модели:
# · Линейная регрессия
# · Полиномиальная регрессия (со степенью 4)
# · Гребневая полиномиальная регрессия (со степенью 4, alpha = 1.0)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import Ridge
# Задаем параметры генерации данных
n_samples = 500
n_features = 2
n_redundant = 0
n_informative = 2
random_state = 42
n_clusters_per_class = 1
def main():
# Генерация данных
X, y = make_classification(n_samples=n_samples,
n_features=n_features,
n_redundant=n_redundant,
n_informative=n_informative,
random_state=random_state,
n_clusters_per_class=n_clusters_per_class)
# Тестовая и обучающая выборки
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.65, random_state=random_state)
# Создание моделей
linear_regression(X_train, X_test, y_train, y_test)
polin_regression_4(X_train, X_test, y_train, y_test)
gr_polin_regression_4(X_train, X_test, y_train, y_test)
# Создание линейной регрессии
def linear_regression(X_train, X_test, y_train, y_test):
linear_regression = LogisticRegression()
linear_regression.fit(X_train, y_train)
linear_regression_score = linear_regression.score(X_train, y_train)
linear_pred = linear_regression.predict(X_test)
plt.scatter(X_test[:, 0], X_test[:, 1], c=linear_pred, cmap="bwr")
plt.title("Линейная регрессия")
plt.xlabel("Признак 1")
plt.ylabel("Признак 2")
plt.show()
print("Линейная регрессия: {}".format(linear_regression_score))
#Создание полиномиальной регрессии со степенью 4
def polin_regression_4(X_train, X_test, y_train, y_test):
poly_regression = make_pipeline(PolynomialFeatures(degree=4), LogisticRegression())
poly_regression.fit(X_train, y_train)
polynomial_regression_score = poly_regression.score(X_train, y_train)
poly_pred = poly_regression.predict(X_test)
plt.scatter(X_test[:, 0], X_test[:, 1], c=poly_pred, cmap="bwr")
plt.title("Полиномиальная регрессия")
plt.xlabel("Признак 1")
plt.ylabel("Признак 2")
plt.show()
print("Полиномиальная регрессия: {}".format(polynomial_regression_score))
#Создание гребневой полиномиальной регрессии со степенью 4 и alpha = 1.0
def gr_polin_regression_4(X_train, X_test, y_train, y_test):
ridge_regression = make_pipeline(PolynomialFeatures(degree=4), Ridge(alpha=1.0))
ridge_regression.fit(X_train, y_train)
ridge_regression_score = ridge_regression.score(X_train, y_train)
ridge_pred = ridge_regression.predict(X_test)
plt.scatter(X_test[:, 0], X_test[:, 1], c=ridge_pred, cmap="bwr")
plt.title("Гребневая полиномиальная регрессия")
plt.xlabel("Признак 1")
plt.ylabel("Признак 2")
plt.show()
print("Гребневая полиномиальная регрессия: {}".format(ridge_regression_score))
main()