This commit is contained in:
Ctrl-Tim 2023-12-10 14:22:51 +04:00
parent 3e19f8fcb2
commit ba4a6f1402
5 changed files with 112 additions and 1 deletions

View File

@ -65,7 +65,7 @@
<recent name="D:\ulstukek\Course4\IIS\IISLabs\IIS_2023_1\zavrazhnova_svetlana_lab_1" />
</key>
</component>
<component name="RunManager">
<component name="RunManager" selected="Python.zavrazhnova_svetlana_lab3_2">
<configuration name="zavrazhnova_svetlana_lab3_2" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="IIS_2023_1" />
<option name="INTERPRETER_OPTIONS" value="" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -0,0 +1,111 @@
# 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 matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, accuracy_score, balanced_accuracy_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
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('Оценка точности:')
print('MAE:', mean_absolute_error(y_test, y_pred))
print('MSE:', mean_squared_error(y_test, y_pred))
print()
# вывод в график
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('Оценка точности:')
print('По тренировочным данным: ', perceptron_model.score(X_train, y_train))
print('По тестовым данным: ', accuracy_score(y_test, y_pred))
print()
# вывод в график
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('Оценка точности:')
print('MAE:', mean_absolute_error(y_test, y_pred))
print('MSE:', mean_squared_error(y_test, y_pred))
print()
# вывод в график
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()