price-builder-backend/services/ml/feature_importances.py

27 lines
980 B
Python

import matplotlib.pyplot as plt
import joblib
import numpy as np
from services.ml.modelBuilder import X_train
# Загрузка модели и признаков
model_rf = joblib.load('laptop_price_model.pkl')
feature_columns = joblib.load('feature_columns.pkl')
# Получение важности признаков
importances = model_rf.feature_importances_
indices = np.argsort(importances)[::-1]
# Вывод наиболее важных признаков
print("Важность признаков:")
for f in range(X_train.shape[1]):
print(f"{f + 1}. {feature_columns[indices[f]]} ({importances[indices[f]]})")
# Визуализация важности признаков
plt.figure(figsize=(12, 8))
plt.title("Важность признаков (Random Forest)")
plt.bar(range(X_train.shape[1]), importances[indices], align='center')
plt.xticks(range(X_train.shape[1]), [feature_columns[i] for i in indices], rotation=90)
plt.tight_layout()
plt.show()