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
2 changed files with 73 additions and 0 deletions
Showing only changes of commit 94a76f47d8 - Show all commits

View File

@ -0,0 +1,14 @@
# Лаб 1
Работа с типовыми наборами данных и различными моделями
# Вариант 3
Данные: make_classification (n_samples=500, n_features=2,
n_redundant=0, n_informative=2, random_state=rs, n_clusters_per_class=1)
# Модели:
1. Линейную регрессию
1. Полиномиальную регрессию (со степенью 3)
1. Гребневую полиномиальную регрессию (со степенью 3, alpha = 1.0)
# Screenshots
<p>
<img src="screens/Screenshot_2022-07-20-15-27-01.png" width="200" title="пример 1">
</p>

View File

@ -0,0 +1,59 @@
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 sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
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]
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)
alphas = np.logspace(-5, 3, 5)
current_subplot = plt.subplot(3, 5 + 1, i)
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)
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
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))
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
hasattr(clf, "decision_function")
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')
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')