首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ValueError命令期间使用训练数据的DTreeViz

在ValueError命令期间使用训练数据的DTreeViz
EN

Stack Overflow用户
提问于 2021-06-10 03:27:56
回答 1查看 302关注 0票数 1

我已经创建了一个DecisionTreeClassifier clf来建模数据,并试图使用dtreeviz包来可视化树。

代码语言:javascript
复制
clf = DecisionTreeClassifier(max_depth=3)
clf.fit(X_train, y_train)

为了使数据可通过dtreeviz函数进行消化,我对X_train和y_train进行了转换。

代码语言:javascript
复制
from dtreeviz.trees import dtreeviz
from sklearn import preprocessing
import graphviz

# Create integer representation of target column
label_encoder = preprocessing.LabelEncoder()
label_encoder.fit(y_train)
print ("Categorical classes:", label_encoder.classes_)
y_train_encoded = label_encoder.transform(y_train)

X_train_mod = X_train.to_numpy()
print("X_train type: ", type(X_train_mod))
print("Dimensions: ", np.ndim(X_train_mod))
print(X_train_mod.shape)

分类类:‘坏’好‘\ X_train类型:\ \维度: 2\ (700,61)

但是,dtreeviz命令出现错误时失败:

代码语言:javascript
复制
dtreeviz(clf, x_data=X_train_mod, y_data=y_train_encoded, target_name='Good/Bad', 
         feature_names=X_train.columns.to_list(), class_names=list(label_encoder.classes_))

ValueError                                Traceback (most recent call last)
<ipython-input-130-eec9abdfba5d> in <module>
     14 print(X_train_mod.shape)
     15 
---> 16 dtreeviz(clf, x_data=X_train_mod, y_data=y_train_encoded, target_name='Good/Bad', 
     17          feature_names=X_train.columns.to_list(), class_names=list(label_encoder.classes_))

~/opt/anaconda3/lib/python3.8/site-packages/dtreeviz/trees.py in dtreeviz(tree_model, x_data, y_data, feature_names, target_name, class_names, tree_index, precision, orientation, instance_orientation, show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, highlight_path, X, max_X_features_LR, max_X_features_TD, label_fontsize, ticks_fontsize, fontname, title, title_fontsize, colors, scale)
    795         if fancy:
    796             if shadow_tree.is_classifier():
--> 797                 class_split_viz(node, X_data, y_data,
    798                                 filename=f"{tmp}/node{node.id}_{os.getpid()}.svg",
    799                                 precision=precision,

~/opt/anaconda3/lib/python3.8/site-packages/dtreeviz/trees.py in class_split_viz(node, X_train, y_train, colors, node_heights, filename, ticks_fontsize, label_fontsize, fontname, precision, histtype, X, highlight_node)
   1002 
   1003         bins = _get_bins(overall_feature_range, nbins)
-> 1004         hist, bins, barcontainers = ax.hist(X_hist,
   1005                                             color=X_colors,
   1006                                             align='mid',

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1599     def inner(ax, *args, data=None, **kwargs):
   1600         if data is None:
-> 1601             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1602 
   1603         bound = new_sig.bind(ax, *args, **kwargs)

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, normed, **kwargs)
   6686         input_empty = np.size(x) == 0
   6687         # Massage 'x' for processing.
-> 6688         x = cbook._reshape_2D(x, 'x')
   6689         nx = len(x)  # number of datasets
   6690 

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _reshape_2D(X, name)
   1428         return [np.reshape(x, -1) for x in X]
   1429     else:
-> 1430         raise ValueError("{} must have 2 or fewer dimensions".format(name))
   1431 
   1432 

ValueError: x must have 2 or fewer dimensions

但是,基于X_train ()函数的输出,np.ndim的维度看起来是正确的。我已经与虹膜示例这里进行了比较,以验证所有类型的参数是否匹配。我现在不知道该怎么做。

EN

回答 1

Stack Overflow用户

发布于 2021-11-29 18:26:17

我也遇到了同样的问题。确保您的分类器也接受了编码标签的培训,即使用

代码语言:javascript
复制
clf.fit(X_train, y_train_encoded)

而不是

代码语言:javascript
复制
clf.fit(X_train, y_train)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67914281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档