RuntimeError9861 2017-12-05 14:12 采纳率: 0%
浏览 13355

ValueError: bad input shape () 求问这个报错该怎么解决

对着《python机器学习经典实例》打代码,发现会报错bad input shape ()。感觉问题出在最后一行,菜鸟想问下该怎么解决?非常感谢
数据如下:
med,low,5more,more,med,med,good
med,low,5more,more,med,high,vgood
med,low,5more,more,big,low,unacc
med,low,5more,more,big,med,good
med,low,5more,more,big,high,vgood
low,vhigh,2,2,small,low,unacc
low,vhigh,2,2,small,med,unacc
low,vhigh,2,2,small,high,unacc
...


import numpy as np
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier

#读取数据
input_path=u'/Users/zhangbei//Desktop/数据挖掘/机器学习/Python-Machine-Learning-Cookbook-master/Chapter02/car.data.txt'
fo=open(input_path)
lines=fo.readlines()
x=[]
for line in lines:
    line=line.strip()
    x.append(line.split(','))
x=np.array(x)

#把字符串特征转换为数值
encoder=[]
x_encoded=np.empty(x.shape)
for i,item in enumerate(x[0]):
    encoder.append(preprocessing.LabelEncoder())
    x_encoded[:,i]=encoder[-1].fit_transform(x[:,i])

x_encoded.astype(int)
x=x_encoded[:,:-1]
y=x_encoded[:,-1]


#转换测试数据
input_data=np.array(['vhigh','vhight','2','2','small','low'])
data_encoded=[-1]*len(input_data)
print data_encoded
for i,item in enumerate(input_data):
    data_encoded[i]=int(encoder[i].transform((input_data[i])))
  • 写回答

4条回答 默认 最新

  • u012493548 2018-03-01 05:57
    关注

    把那一段转换测试数据去掉就好了,这段是一条数据测试用的

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型