我正在使用 graphviz 绘制分类决策树。
在适应功能之前,我使用“preprocessing.StandardScaler()”来缩放它们
因此,当我绘制决策树时,我会根据“转换后的值”绘制它
有没有办法在绘制它之前“inverse_trasform”分类器,以便决策树绘制节点处的实际值而不是转换后的值?
是的,我试过 scale.inverse_transform(rf_clf) .... 但当然不要工作...
从 sklearn.datasets 导入数据集
iris = datasets.load_iris()
从字典创建数据框
species = [iris.target_names[x] for x in iris.target]
iris = pd.DataFrame(iris['data'], columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width'])
iris['Species'] = species
转换为数组
Features = np.array(iris[['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']])
levels = {'setosa':0, 'versicolor':1, 'virginica':2}
Labels = np.array([levels[x] for x in iris['Species']])
分裂
nr.seed(1115)
indx = range(Features.shape[0])
indx = ms.train_test_split(indx, test_size = 100)
X_train = Features[indx[0],:]
y_train = np.ravel(Labels[indx[0]])
X_test = Features[indx[1],:]
y_test = np.ravel(Labels[indx[1]])
缩放:
scale = preprocessing.StandardScaler()
scale.fit(X_train)
X_train = scale.transform(X_train)
拟合分类器
rf_clf = tree.DecisionTreeClassifier() ###simple TREE
rf_clf.fit(X_train, y_train)*
用 graphviz 绘制决策树:
dot_data = tree.export_graphviz(rf_clf, out_file=None,
feature_names=['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width'],
class_names=['setosa', 'versicolor', 'virginica'],
filled=True, rounded=True,
special_characters=True)
print(dot_data)
graph = graphviz.Source(dot_data)
graph
第一个节点的结果是“Petal_width<= 0.53”,第二个节点是“petal lenght <= -0.788”,这是一个实数的负数。
我宁愿让树以英寸为单位承载真正的价值......