【发布时间】:2017-01-27 07:45:40
【问题描述】:
这是我的代码:
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn import preprocessing
import os
import subprocess
def categorical_split():
colors = ['blue', 'green', 'yellow', 'green', 'red']
sizes = ['small', 'large', 'medium', 'large', 'small']
size_encoder = preprocessing.LabelEncoder()
sizes = size_encoder.fit_transform(sizes).reshape(-1, 1)
color_encoder = preprocessing.LabelEncoder()
colors = size_encoder.fit_transform(colors).reshape(-1, 1)
dt = DecisionTreeClassifier( random_state=99)
dt.fit(colors, sizes)
with open("dt.dot", 'w') as f:
export_graphviz(dt, out_file=f,
feature_names='colors')
command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
subprocess.check_call(command)
categorical_split()
由于 scikit-learn 中的决策树不能直接处理分类变量,我不得不使用 LabelEncoder。在图表上,我们看到像c<=1.5 这样的拆分。这种拆分表明分类变量被视为序数变量,拆分是保留顺序。如果我的数据没有顺序,这种方法是有害的。有办法解决吗?如果您打算建议一次性编码,请提供一个示例(代码)它将如何提供帮助。
【问题讨论】:
标签: authentication scikit-learn decision-tree ordinal