今天写关于决策树的一些算法的时候,卧槽,mmp,竟然出现了红色的警示错误,

oneRowX: [0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]
newRowX: [1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]
Traceback (most recent call last):File "D:/pythoncode/jqxi/tree/tree.py", line 64, in <module>predictedY = clf.predict(newRowX)File "F:\python\python3\lib\site-packages\sklearn\tree\tree.py", line 412, in predictX = self._validate_X_predict(X, check_input)File "F:\python\python3\lib\site-packages\sklearn\tree\tree.py", line 373, in _validate_X_predictX = check_array(X, dtype=DTYPE, accept_sparse="csr")File "F:\python\python3\lib\site-packages\sklearn\utils\validation.py", line 441, in check_array"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[1. 0. 0. 0. 1. 1. 0. 0. 1. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

正确解决办法

# encoding=utf-8
__author__ = 'SK'from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import tree
from sklearn import preprocessing
from sklearn.externals.six import StringIO# Read in the csv file and put features into list of dict and list of class label
allElectronicsData = open(r'D://pythoncode/jqxi/tree/AllElectronics.csv', 'rt')
reader = csv.reader(allElectronicsData)
headers = next(reader)print(headers)featureList = []
labelList = []for row in reader:labelList.append(row[len(row)-1])rowDict = {}for i in range(1, len(row)-1):rowDict[headers[i]] = row[i]featureList.append(rowDict)print(featureList)# Vetorize features
vec = DictVectorizer()
dummyX = vec.fit_transform(featureList) .toarray()print("dummyX: " + str(dummyX))
print(vec.get_feature_names())print("labelList: " + str(labelList))# vectorize class labels
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)
print("dummyY: " + str(dummyY))# Using decision tree for classification
# clf = tree.DecisionTreeClassifier()
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummyX, dummyY)
print("clf: " + str(clf))# Visualize model
with open("allElectronicInformationGainOri.dot", 'w') as f:f = tree.export_graphviz(clf, feature_names=vec.get_feature_names(), out_file=f)oneRowX = dummyX[0, :]
print("oneRowX: " + str(oneRowX))newRowX = oneRowX
newRowX[0] = 1
newRowX[2] = 0
print("newRowX: " + str(newRowX))predictedY = clf.predict([newRowX])print("predictedY: " + str(predictedY))

解决Python的your data either using array.reshape(-1, 1) if your data has a single featur相关推荐

  1. 成功解决Python的Reshape your data either using array.reshape(-1, 1) if your data has a single feature or

    成功解决Python的Reshape your data either using array.reshape(-1, 1) if your data has a single feature or ...

  2. 《python机器学习经典实例》Expected 2D array, got 1D array instead和Reshape your data either using array.问题(已解决)

    问题描述: ValueError: Expected 2D array, got 1D array instead: array=[2.  1.5]. Reshape your data either ...

  3. 成功解决ValueError: Expected 2D array, got 1D array instead: Reshape your data either using array.reshap

    成功解决ValueError: Expected 2D array, got 1D array instead: Reshape your data either using array.reshap ...

  4. python Clipping input data to the valid range for imshow with RGB data解决方法

    文章目录 遇到的问题 全部代码 参考 遇到的问题 第一点,遇到的问题是 Clipping input data to the valid range for imshow with RGB data ...

  5. python输出到文件乱码_解决python解析文件时输出乱码

    首先获取到json模块,encoding指定文件编码utf-8,errors报错时忽略错误,print()输出结果看看是否有问题. # -*- coding: utf-8 -*- import jso ...

  6. python解析xml提交到hdfs_完美解决python针对hdfs上传和下载的问题

    当我们使用python的hdfs包进行上传和下载文件的时候,总会出现如下问题 requests.packages.urllib3.exceptions.NewConnectionError:: Fai ...

  7. python函数注释 参数 省略号_解决python 输出是省略号的问题

    这个问题非常非常重要,搞了一晚上都没解决好,但是真的很简单很简单, 如果你也 是用的numpy array, 如果你也想得到输出矩阵的全部内容,而不是省略形式, [[ 0.10284943 0.095 ...

  8. python json.loads()中文问题-解决Python下json.loads()中文字符出错的问题

    Python:2.7 IDE:Pycharm5.0.3 今天遇到一个问题,就是在使用json.load()时,中文字符被转化为Unicode码的问题,解决方案找了半天,无解.全部代码贴出,很简单的一个 ...

  9. 成功解决AttributeError: type object 'scipy.interpolate.interpnd.array' has no attribute '__reduce_cython

    成功解决AttributeError: type object 'scipy.interpolate.interpnd.array' has no attribute '__reduce_cython ...

最新文章

  1. 旷视孙剑博士提出LGD,训练速度提升51%,适用于目标检测的知识蒸馏
  2. Kafka Architecture
  3. VTK:可视化之SelectWindowRegion
  4. POJ2255Tree Recovery
  5. jQuery结构分析
  6. 2019 年度十大 AI 安防热点事件丨年终盘点
  7. vue pdfjs 在线预览
  8. Mysql对应的dul_MySQL InnoDB 数据字典介绍
  9. Java面向对象的构造方法例子
  10. 【转】远程桌面 剪切板复制文件失效解决方法
  11. Spring源码之BeanDefinitionRegistryPostProcessor内部执行流程
  12. samba4的负载均衡群集
  13. 台式计算机大全,电脑品牌大全..3MT产品库
  14. 冰汽朋克侦查机器人_寒霜朋克全系统解析攻略 冰汽时代全法典全科技详解
  15. Centos 7.5 1804 镜像下载
  16. android studio 页面布局
  17. 龟叔 python_Python 三十而立
  18. 【数据结构】AVL树(高度平衡的二叉搜索树)
  19. Honeywell RTU2020使用.软件篇
  20. 7-28 天梯赛的善良 (PTA C语言)最简

热门文章

  1. 干货分享|数据可视化报表制作技巧
  2. 配置secureCRT
  3. 存储误删除所有lun的数据恢复方法
  4. linux usb信息查看工具,Linux下,查看USB设备信息
  5. 云存储架构的技术特点与三个发展方向
  6. 除以用计算机按哪个键,电脑键盘÷号是哪个键?小编教你打出÷符号的操作方法...
  7. python去掉两边空格,Python去除字符串两端空格的方法
  8. 效率高、数据精,河段河道地形测量这样操作!
  9. 百度SRE面经-为三面攒人品
  10. PHP与FLASH之间的交互