python - ValueError:特征名称的长度,2330 与特征数量不匹配,5064
问题描述
我正在解决决策树分类问题。我无法绘制决策树图。代码如下:
pima = pd.read_csv("Data.csv", sep=';', encoding ='latin1')
feature_cols = ['Name', 'Material number', 'Production status', 'PKD', 'Marketing Indicator',
'Windmill release', 'Component type', 'Rail', 'token name']
features_df = pd.get_dummies(pima, columns=['Name', 'Material number', 'Production status', 'PKD', 'Marketing Indicator','Windmill release', 'Component type', 'Rail', 'token name'], dummy_na=True)
X = features_df.as_matrix()
y = pima['Error'].as_matrix()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1)
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(y_pred)
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,filled=True, rounded=True,special_characters=True,feature_names = features_df,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('thesis.png')
Image(graph.create_png())
我收到一个错误
ValueError: Length of feature_names, 2330 does not match number of features, 5064
请给一些建议。
这是回溯:
C:/Users/Drashti Bhatt/Desktop/thesis.py:27: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
X = features_df.as_matrix()
C:/Users/Drashti Bhatt/Desktop/thesis.py:28: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
y = pima['Error'].as_matrix()
[0 1 1 ... 1 1 0]
C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\externals\six.py:31: DeprecationWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
"(https://pypi.org/project/six/).", DeprecationWarning)
Traceback (most recent call last):
File "<ipython-input-1-a6b45e60092a>", line 1, in <module> runfile('C:/Users/Drashti Bhatt/Desktop/thesis.py', wdir='C:/Users/Drashti Bhatt/Desktop')
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Drashti Bhatt/Desktop/thesis.py", line 55, in <modulespecial_characters=True,feature_names = features_df,class_names=['0','1'])
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\tree\export.py", line 776, in export_graphviz exporter.export(decision_tree)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\tree\export.py", line 401, in export decision_tree.n_features_))
ValueError: Length of feature_names, 2330 does not match number of features, 5064```
解决方案
推荐阅读
- in-app-purchase - 我取消订阅后HMS IAP没有续费事件
- python - 即使 pycharm 没有发现错误,urllib.request 脚本也会出错
- r - kableExtra column_spec 错误 - 可能是 dplyr 总结
- asp.net - ASP.NET webforms 应用程序中的 Ajax 模式弹出扩展器
- java - 如何在构造函数中对泛型参数进行类型检查?
- android - 当我按下主页按钮或切换到其他应用程序时应用程序重新启动
- python - 我正在创建一个维基百科搜索引擎,但问题是程序在给出输出后关闭,请检查描述
- python - 如何从正则表达式搜索中提取跨度和匹配?
- spring - 我们如何在 Spring Security 中删除登录页面
- python - How to read a column of datatype json and convert into list using Pandas?