如何确定使用机器学习模型未达到目标的原因
How to determine the cause of not achieving a target in using machine learning model
我想知道是否有可能知道特定变量对模型测试样本数据的影响。下面的模型澄清了这个问题;
给定一个数据集来预测学生的分数。
ID Studies hours Games hours lectures hours social Activities Score
0 1 20 5 15 2 78
1 2 15 6 13 3 69
2 3 31 2 16 1 95
3 4 22 2 15 2 80
4 5 19 7 15 4 71
5 6 10 8 10 8 52
6 7 13 7 11 6 59
7 8 34 1 16 1 96
8 9 25 6 15 1 83
9 10 22 3 16 2 76
10 11 17 7 15 1 66
11 12 28 2 14 2 87
12 13 21 3 16 3 77
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from numpy import absolute
from xgboost import XGBModel
import pickle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline
from xgboost import plot_importance
data = pd.read_csv("student_score.csv")
def perfomance(data):
X = data.iloc[:,:-1]
y = data.iloc[:,-1:]
model = XGBModel(booster='gbtree')
#model = XGBModel(booster='gblinear')
model.fit(X, y)
cv = RepeatedKFold(n_splits=3, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(model, X,y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# force scores to be positive
scores = np.absolute(scores)
metrics = ('Mean MAE: %.3f (%.3f)' % (scores.mean(), scores.std()) )
# save the model to disk
filename = 'score.sav'
pickle.dump(model, open(filename, 'wb'))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
# load the model from disk
loaded_model = pickle.load(open('score.sav', 'rb'))
result = loaded_model.predict(X_test)
print(result)
plt.rcParams["figure.figsize"] = (20,15)
plot_importance(model)
plt.show()
特征重要性:
[5.6058721e-04 6.7560148e-01 3.1960118e-01 4.2312010e-03 5.4962843e-06]
特征重要性是模型排序的一般重要性。
我现在需要的是:
当我选择 A 样本测试时说 test = pd.DataFrame([{"Studies hours":15, "Games hours":6, "lectures hours":13,"social Activities":3}])
并预测; loaded_model.predict(test)
我得到了 68 分,具体哪个变量(不是一般重要性)没有使这个特定的样本测试不是 100 分而是 68 分?
例如,模型应该告诉我学习时间很糟糕或少于预期。
机器学习模型可以做到吗?
您可以查看使用 SHAP. A waterfall or force 图解释模型对特定示例的决定可以显示为什么模型根据输入变量对特定示例得分为 68。
您描述的主题称为模型可解释性或可解释性。模型越复杂,它就越准确,但越难解释(一般来说)。 SHAP 值是我看到人们通常解释每个特征对预测的影响以及每个特征值对给定观察的预测的最常见方式。 SHAP 值最常见的可视化是力图。它看起来像这样:
我从中获取此图片的博客解释了如何为任何模型构建力图:Explain Any Models with the SHAP Values — Use the KernelExplainer
我想知道是否有可能知道特定变量对模型测试样本数据的影响。下面的模型澄清了这个问题;
给定一个数据集来预测学生的分数。
ID Studies hours Games hours lectures hours social Activities Score
0 1 20 5 15 2 78
1 2 15 6 13 3 69
2 3 31 2 16 1 95
3 4 22 2 15 2 80
4 5 19 7 15 4 71
5 6 10 8 10 8 52
6 7 13 7 11 6 59
7 8 34 1 16 1 96
8 9 25 6 15 1 83
9 10 22 3 16 2 76
10 11 17 7 15 1 66
11 12 28 2 14 2 87
12 13 21 3 16 3 77
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from numpy import absolute
from xgboost import XGBModel
import pickle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline
from xgboost import plot_importance
data = pd.read_csv("student_score.csv")
def perfomance(data):
X = data.iloc[:,:-1]
y = data.iloc[:,-1:]
model = XGBModel(booster='gbtree')
#model = XGBModel(booster='gblinear')
model.fit(X, y)
cv = RepeatedKFold(n_splits=3, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(model, X,y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# force scores to be positive
scores = np.absolute(scores)
metrics = ('Mean MAE: %.3f (%.3f)' % (scores.mean(), scores.std()) )
# save the model to disk
filename = 'score.sav'
pickle.dump(model, open(filename, 'wb'))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
# load the model from disk
loaded_model = pickle.load(open('score.sav', 'rb'))
result = loaded_model.predict(X_test)
print(result)
plt.rcParams["figure.figsize"] = (20,15)
plot_importance(model)
plt.show()
特征重要性:
[5.6058721e-04 6.7560148e-01 3.1960118e-01 4.2312010e-03 5.4962843e-06]
特征重要性是模型排序的一般重要性。
我现在需要的是:
当我选择 A 样本测试时说 test = pd.DataFrame([{"Studies hours":15, "Games hours":6, "lectures hours":13,"social Activities":3}])
并预测; loaded_model.predict(test)
我得到了 68 分,具体哪个变量(不是一般重要性)没有使这个特定的样本测试不是 100 分而是 68 分?
例如,模型应该告诉我学习时间很糟糕或少于预期。
机器学习模型可以做到吗?
您可以查看使用 SHAP. A waterfall or force 图解释模型对特定示例的决定可以显示为什么模型根据输入变量对特定示例得分为 68。
您描述的主题称为模型可解释性或可解释性。模型越复杂,它就越准确,但越难解释(一般来说)。 SHAP 值是我看到人们通常解释每个特征对预测的影响以及每个特征值对给定观察的预测的最常见方式。 SHAP 值最常见的可视化是力图。它看起来像这样:
我从中获取此图片的博客解释了如何为任何模型构建力图:Explain Any Models with the SHAP Values — Use the KernelExplainer