变量环境
import os
import sys
os.environ["PATH"] += os.pathsep + r'C:\Users\BC53\AppData\Local\Programs\Python\Python36\Scripts\graphviz-2.38\release\bin'
Permutation Importance
打乱某一列数据,看预测准确性下降的程度
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import webbrowser
data = pd.read_csv(r'D:\BC53\我的文档\XTC\PYTHON\FIFA 2018 Statistics.csv')
y = (data['Man of the Match'] == "Yes") # Convert from string "Yes"/"No" to binary
feature_names = [i for i in data.columns if data[i].dtype in [np.int64]]
X = data[feature_names]
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
my_model = RandomForestClassifier(n_estimators=100,
random_state=0).fit(train_X, train_y)
import eli5
from eli5.sklearn import PermutationImportance
perm = PermutationImportance(my_model, random_state=1).fit(val_X, val_y)
html_obj=eli5.show_weights(perm, feature_names = val_X.columns.tolist())
# Write html object to a file (adjust file path; Windows path is used here)
with open(r'D:\BC53\我的文档\XTC\tmp\iris-perm.htm','wb') as f:
f.write(html_obj.data.encode("UTF-8"))
# Open the stored HTML file on the default browser
url = r'D:\BC53\我的文档\XTC\tmp\iris-perm.htm'
webbrowser.open(url, new=2)
第一个数——下降的程度,第二个数——不同排列方式结果的差异
Partial Dependence Plots
一个特征“怎样”影响预测【敏感性分析】
缺点:假设特征间各自独立
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
data = pd.read_csv(r'D:\BC53\我的文档\XTC\PYTHON\FIFA 2018 Statistics.csv')
y = (data['Man of the Match'] == "Yes") # Convert from string "Yes"/"No" to binary
feature_names = [i for i in data.columns if data[i].dtype in [np.int64]]
X = data[feature_names]
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
tree_model = DecisionTreeClassifier(random_state=0, max_depth=5, min_samples_split=5).fit(train_X, train_y)
from sklearn import tree
import graphviz
tree_graph = tree.export_graphviz(tree_model, out_file=None, feature_names=feature_names)
graphviz.Source(tree_graph)
from matplotlib import pyplot as plt
from sklearn.inspection import PartialDependenceDisplay
# Create and plot the data
disp1 = PartialDependenceDisplay.from_estimator(tree_model, val_X, ['Goal Scored'])
plt.show()
feature_to_plot = 'Distance Covered (Kms)'
disp2 = PartialDependenceDisplay.from_estimator(tree_model, val_X, [feature_to_plot])
plt.show()
# Build Random Forest model
rf_model = RandomForestClassifier(random_state=0).fit(train_X, train_y)
disp3 = PartialDependenceDisplay.from_estimator(rf_model, val_X, [feature_to_plot])
plt.show()
2D Partial Dependence Plots
fig, ax = plt.subplots(figsize=(8, 6))
f_names = [('Goal Scored', 'Distance Covered (Kms)')]
# Similar to previous PDP plot except we use tuple of features instead of single feature
disp4 = PartialDependenceDisplay.from_estimator(tree_model, val_X, f_names, ax=ax)
plt.show()
报错:type object ‘PartialDependenceDisplay’ has no attribute ‘from_estimator’
解决方法:pip install --upgrade scikit-learn【估计要高版本Python3】