如何解决这个TypeError?

时间:2018-04-02 18:29:02

标签: python machine-learning classification data-mining

28 def separateByClass(dataset):
29    separated = {}
30    for i in range(len(dataset)):
31        vector = dataset[i]
32        if vector[-1] not in separated:
33            separated[vector[-1]] = []
34        separated[vector[-1]].append(vector)
35    return separated

此函数基本上将数据集分为类

例如:

dataset = [[1,20,1], [2,21,0], [3,22,1]]

应该将结果作为

Separated instances: {0: [[2, 21, 0]], 1: [[1, 20, 1], [3, 22, 1]]}

这是我的追溯:

Traceback (most recent call last):
  File "test.py", line 118, in <module>
    main()
  File "test.py", line 111, in main
    summaries = summarizeByClass(trainingSet)
  File "test.py", line 55, in summarizeByClass
    separated = separateByClass(dataset)
  File "test.py", line 32, in separateByClass
    if vector[-1] not in separated:
TypeError: 'float' object is not subscriptable

2 个答案:

答案 0 :(得分:0)

vector这是您尝试索引的浮动广告。正如错误所暗示的那样,你不能这样做。

答案 1 :(得分:0)

你当然有{1}}输入的1个元素包含一个浮点而不是一个列表。

您可以添加类似的支票

dataset