from dtreeviz.trees import dtreeviz
viz = dtreeviz(clf,
iris['data'],
iris['target'],
target_name='',
feature_names=np.array(iris['feature_names']),
class_names={0:'setosa',1:'versicolor',2:'virginica'},scale=2)
viz
#用交叉验证选特征
from sklearn.svm import SVC
from sklearn.feature_selection import RFECV
# Instantiate RFECV visualizer with a random forest regressor
rfecv = RFECV(RandomForestRegressor())
rfecv.fit(X, y) # Fit the data to the visualizer
print("Optimal number of features : %d" % rfecv.n_features_)
# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()
决策树的可视化及通过决策树筛选变量
最新推荐文章于 2022-10-28 22:56:37 发布