索引错误,适用于一维数组

时间:2014-07-09 17:16:42

标签: python csv numpy

from sklearn import tree
import numpy as np
from sklearn.preprocessing import Imputer

data = np.genfromtxt('bank_int.csv', delimiter = '  ') 

sample = np.genfromtxt('test_sample.csv', delimiter = ' ') 

output = []
count = 0

train_data = data[:,:-1]
target_data = data[:,-1:]

decision_tree = tree.DecisionTreeClassifier()           #creation of decision tree
decision_tree = decision_tree.fit(train_data, target_data)  #training the tree

for test in data:
    output[count] = decision_tree.predict(sample)       #testing the results
    count += 1

#result = decision_tree.predict(sample)

print output[count]

1 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow。请修改stackoverflow指南中的“how to ask a good question”段落。

现在,您遇到的问题是因为您考虑使用不同的编程语言,可能是C语言。错误IndexError: list assignment index out of range表明您正在索引不存在的元素。

而不是

for test in data:
    output[count] = decision_tree.predict(sample)       #testing the results
    count += 1

尝试:

for test in data:
    output.append(decision_tree.predict(sample))        #testing the results