【问题标题】:Index Error , works for 1D array索引错误,适用于一维数组
【发布时间】:2014-08-30 20:22:43
【问题描述】:
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]

【问题讨论】:

  • Traceback(最近一次调用最后一次):文件“classification.py”,第 21 行,在 output[count] = decision_tree.predict(sample) #testing 结果 IndexError: list assignment index超出范围
  • 错误显示如上
  • 输出是一个空列表output = []所以你不能访问不存在的元素

标签: python csv numpy


【解决方案1】:

欢迎来到 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2023-01-25
    • 2016-09-19
    • 2019-07-28
    相关资源
    最近更新 更多