antonov_dmitry_lab_1 #9

Merged
Alexey merged 6 commits from antonov_dmitry_lab_1 into main 2023-10-02 19:38:21 +04:00
Showing only changes of commit 9bf1c4845a - Show all commits

View File

@ -1,59 +1,97 @@
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 skimage.metrics import mean_squared_error
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
X, y = make_classification(
n_features=2,
n_redundant=0,
n_informative=2,
random_state=0,
n_clusters_per_class=1
)
X, y = make_classification(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)
linearly_dataset = (X, y)
moon_dataset = make_moons(noise=0.3, random_state=0)
circles_dataset = make_circles(noise=0.2, factor=0.5, random_state=1)
datasets = [moon_dataset, circles_dataset, linearly_dataset]
"""
Данные:
· moon_dataset
· circles_dataset
· linearly_dataset
"""
for ds_cnt, ds in enumerate(datasets):
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=.4, random_state=42
)
"""
Модели:
· Линейную регрессию
· Полиномиальную регрессию (со степенью 3)
· Гребневую полиномиальную регрессию (со степенью 3, alpha = 1.0)
"""
alphas = np.logspace(-5, 3, 5)
# Линейная
linear_regression = LinearRegression()
linear_regression.fit(X_train, y_train)
linear_predictions = linear_regression.predict(X_test)
linear_mse = mean_squared_error(y_test, linear_predictions)
current_subplot = plt.subplot(3, 5 + 1, i)
# Полиномиальная (degree=3)
poly_regression = make_pipeline(PolynomialFeatures(degree=3), LinearRegression())
poly_regression.fit(X_train, y_train)
poly_predictions = poly_regression.predict(X_test)
poly_mse = mean_squared_error(y_test, poly_predictions)
current_subplot.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
current_subplot.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
# Гребневая (degree=3, alpha=1.0)
poly_regression_alpha = make_pipeline(PolynomialFeatures(degree=3), Ridge(alpha=1.0))
poly_regression_alpha.fit(X_train, y_train)
poly_alpha_predictions = poly_regression_alpha.predict(X_test)
poly_alpha_mse = mean_squared_error(y_test, poly_alpha_predictions)
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
# График данных
plt.figure(figsize=(10, 6))
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap='coolwarm')
plt.title('Датасет №' + str(ds_cnt))
plt.xlabel('X')
plt.ylabel('Y')
h = .02 # шаг регулярной сетки
x0_min, x0_max = X[:, 0].min() - .5, X[:, 0].max() + .5
x1_min, x1_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx0, xx1 = np.meshgrid(np.arange(x0_min, x0_max, h), np.arange(x1_min, x1_max, h))
# График линейной модели
plt.figure(figsize=(10, 6))
plt.scatter(X_test[:, 0], X_test[:, 1], c=linear_predictions, cmap='coolwarm')
plt.title('Линейная ds'+ str(ds_cnt))
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Z = clf.decision_function(np.c_[xx0.ravel(), xx1.ravel()]) # 1
Z = clf.predict_proba(np.c_[xx0.ravel(), xx1.ravel()])[:, 1] # 2
Z = clf.predict(np.c_[xx0.ravel(), xx1.ravel()]) # 3
# График полиномиальной модели (degree=3)
plt.figure(figsize=(10, 6))
plt.scatter(X_test[:, 0], X_test[:, 1], c=poly_predictions, cmap='coolwarm')
plt.title('Полиномиальная (degree=3) ds' + str(ds_cnt))
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
hasattr(clf, "decision_function")
# График гребневой модели (degree=3, alpha=1.0)
plt.figure(figsize=(10, 6))
plt.scatter(X_test[:, 0], X_test[:, 1], c=poly_alpha_predictions, cmap='coolwarm')
plt.title('Гребневая (degree=3, alpha=1.0) ds' + str(ds_cnt))
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Z = Z.reshape(xx0.shape)
current_subplot.contourf(xx0, xx1, Z, cmap=cm, alpha=.8)
current_subplot.set_xlim(xx0.min(), xx0.max())
current_subplot.set_ylim(xx0.min(), xx1.max())
current_subplot.set_xticks(())
current_subplot.set_yticks(())
current_subplot.set_title(name)
current_subplot.text(xx0.max() - .3, xx1.min() + .3, ('%.2f' % score).lstrip('0'),
size=15, horizontalalignment='right')
# Сравнение качества
print('Линейная MSE:', linear_mse)
print('Полиномиальная (degree=3) MSE:', poly_mse)
print('Гребневая (degree=3, alpha=1.0) MSE:', poly_alpha_mse)
current_subplot.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
figure.subplots_adjust(left=.02, right=.98)
current_subplot.set_title("Input data")
current_subplot.text(xx0.max() - .3, xx1.min() + .3, ('%.2f' % score).lstrip('0'), size=15,
horizontalalignment='right')