尝试标准化训练数据时获取元组

时间:2019-05-31 20:07:10

标签: python

我有一套培训和测试套件。我需要对其进行规范化,但似乎无法对其进行迭代,但会收到此错误TypeError: list indices must be integers or slices, not tuple。我将只介绍其中的一小部分,以便您了解其结构。我尝试使用enumeraterange,但遇到相同的错误。

training_data = [
    [3.6216, 8.6661, -2.8073, -0.44699, 0],
    [4.5459, 8.1674, -2.4586, -1.4621, 0],
    [3.866, -2.6383, 1.9242, 0.10645, 0],
    [3.4566, 9.5228, -4.0112, -3.5944, 0],
    [0.32924, -4.4552, 4.5718, -0.9888, 0],
    [4.3684, 9.6718, -3.9606, -3.1625, 0],
    [3.5912, 3.0129, 0.72888, 0.56421, 0],
    [2.0922, -6.81, 8.4636, -0.60216, 0],
]

代码:

def minMaxNrom(training_data, testing_data):
    for i in enumerate(training_data):
        for j in enumerate(training_data):
            new_data = training_data[i][j] - min(j) / max(j) - min(j)    

    for i in enumerate(testing_data):
        for j in enumerate(testing_data[i]):
            new_testing_data = testing_data[i][j] - min(j) / max(j) - min(j)


    return (new_data, new_testing_data)

1 个答案:

答案 0 :(得分:1)

for i in enumertae(list)生成一个tuple,即(index:value),您要查找的是索引,因此请使用for i,v in enumerate(list)

def minMaxNrom(training_data, testing_data):
    for i,v1 in enumerate(training_data):
        for j,v2 in enumerate(v1):
            new_data = training_data[i][j] - (min(v1) / max(v1)) - min(v1)    

    for i,v1 in enumerate(testing_data):
        for j,v2 in enumerate(v1):
            new_testing_data = testing_data[i][j] - (min(v1) / max(v1)) - min(v1)

    return (new_data, new_testing_data)