在加载用于数据分析的文本文件时抛出一些错误,最后2行引发错误

时间:2017-08-05 16:27:55

标签: python numpy machine-learning

加载文本文件进行数据分析时,

会抛出一些错误,最后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))

1 个答案:

答案 0 :(得分: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))