【问题标题】:throws some error while loading a text file for data analysis last 2 line throws an error加载文本文件以进行数据分析时抛出一些错误最后 2 行抛出错误
【发布时间】:2017-08-05 16:27:55
【问题描述】:

在加载文本文件进行数据分析时抛出一些错误最后 2 行抛出错误

import numpy as np
f=open('decision_tree_data.txt','r')
x_train=[]
y_train=[]
for line  in f:
line = np.asarray(line.split(),dtype=np.float32)
x_train.append(line[:-1])
y_train.append(line[:-1])
x_train = np.asmatrix(x_train)
y_train = np.reshape(y_train,(len(y_train),1))

【问题讨论】:

  • 会抛出什么错误?
  • AttributeError: 'list' 对象没有属性 'reshape',,,,,ValueError 1 x_train = np.asmatrix(x_train)2 y_train = np.reshape(y_train,(len(y_train), 1)) except (AttributeError, TypeError): ---> return _wrapit(obj, method, *args, **kwds)ValueError: cannot reshape array of size 56 into shape (14,1)
  • 请以正确的格式将错误添加到问题中,而不是在 cmets 中。

标签: python numpy machine-learning


【解决方案1】:

检查您的代码的缩进。以下代码为我成功编译,并确保您已安装 numpy 模块。

import numpy as np
f=open('decision_tree_data.txt','r')
x_train=[]
y_train=[]

for line in f:
    line=np.asarray(line.split(),dtype=np.float32)
    x_train.append(line[:-1])
    y_train.append(line[-1])

x_train=np.asmatrix(x_train)
y_train=np.reshape(y_train,(len(y_train),1))

【讨论】:

  • #1) valueError: cannot reshape array of size 56 into shape (14,1) #2)AttributeError: 'list' object has no attribute 'reshape'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2013-10-12
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2011-03-28
相关资源
最近更新 更多