IIS_2023_1/ilbekov_dmitriy_lab_2/lab2.py
2023-10-15 21:40:08 +04:00

81 lines
3.0 KiB
Python

from sklearn.linear_model import LinearRegression, Lasso
from sklearn.feature_selection import RFE
from sklearn.preprocessing import MinMaxScaler
import numpy as np
# Генерация синтетических данных
def create_data():
np.random.seed(0)
size = 750
X = np.random.uniform(0, 1, (size, 14))
Y = (10 * np.sin(np.pi * X[:, 0] * X[:, 1]) + 20 * (X[:, 2] - .5) ** 2 +
10 * X[:, 3] + 5 * X[:, 4] ** 5 + np.random.normal(0, 1))
X[:, 10:] = X[:, :4] + np.random.normal(0, .025, (size, 4))
return X, Y
# Нормализация значений рангов
def rank_to_dict(ranks):
ranks = np.abs(ranks)
names = ["x%s" % i for i in range(1, 15)]
minmax = MinMaxScaler()
ranks = minmax.fit_transform(np.array(ranks).reshape(14, 1)).ravel()
ranks = map(lambda x: round(x, 2), ranks)
return dict(zip(names, ranks))
# Вывод отсортированных признаков по важности в табличном виде
def print_sorted_features_by_models(ranks_by_model: {}):
sorted_ranks = sorted(ranks_by_model.items(), key=lambda item: sum(item[1].values()), reverse=True)
print("{:<40}".format(""), end="")
for i in range(1, 15):
print("{:<10}".format(i), end="")
print()
for model, rank_dict in sorted_ranks:
sorted_features = sorted(rank_dict.items(), key=lambda item: item[1], reverse=True)
print("{:<40}".format(model), end="")
for feature, rank in sorted_features:
print("{:<10}".format(f"{feature}: {rank}"), end="")
print()
print()
# Получение средних значений моделей и ТОП 4 самых важных признака
def average_values(ranks_by_model: {}):
mean = {}
for model, rank_dict in ranks_by_model.items():
for feature, rank in rank_dict.items():
if feature not in mean:
mean[feature] = 0
mean[feature] += rank
mean = {feature: round(rank / len(ranks_by_model), 2) for feature, rank in mean.items()}
mean_sorted = sorted(mean.items(), key=lambda item: item[1], reverse=True)
print("Средние значения")
print(mean_sorted)
print("\nТОП 4 самых важных признака по среднему значению: ")
for feature, rank in mean_sorted[:4]:
print('Признак - {0}, значение важности - {1}'.format(feature, rank))
X, Y = create_data()
# ЛИНЕЙНАЯ РЕГРЕССИЯ
linear_regression = LinearRegression()
linear_regression.fit(X, Y)
# ЛАССО
lasso = Lasso(alpha=.01)
lasso.fit(X, Y)
# РЕКУРСИВНОЕ СОКРАЩЕНИЕ ПРИЗНАКОВ
rfe = RFE(linear_regression)
rfe.fit(X, Y)
ranks_by_model = {
"Линейная регрессия": rank_to_dict(linear_regression.coef_),
"Лассо": rank_to_dict(lasso.coef_),
"РЕКУРСИВНОЕ СОКРАЩЕНИЕ ПРИЗНАКОВ (RFE)": rank_to_dict(rfe.ranking_),
}
print_sorted_features_by_models(ranks_by_model)
average_values(ranks_by_model)