0

我正在尝试使用 3-sigma 规则从我的数据中过滤出明显的测量错误。x 是测量点的 numpy 数组,y 是测量值的数组。为了从我的数据中删除错误的点,我压缩 x.tolist() 和 y.tolist(),然后过滤每个元组的第二个元素,然后我需要将我的 zip 转换回两个列表。我试图首先将我的元组列表转换为列表列表,然后将其转换为 numpy 2D 数组,然后取两个 1D 切片。看起来第一个切片是正确的,但随后它输出以下内容:

x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]

IndexError: too many indices for array   

我不明白我在做什么错。这是代码:


x = np.array(readCol(0, l))
y = np.array(readCol(1, l))
n = len(y)

stdev = np.std(y)
mean = np.mean(y)

print("Stdev is: " + str(stdev))
print("Mean is: " + str(mean))

def flt(n):
    global mean
    global stdev
    global x
    if abs(n[1] - mean) < 3*stdev:
        return True
    else:
        print('flt function finds an error: ' + str(n[1]))
        return False


def filtration(N):
    print(Fore.RED + 'Filtration function launched')
    global y
    global x
    global stdev
    global mean
    zap = zip(x.tolist(), y.tolist())
    for i in range(N):
        print(Fore.RED + ' Filtration step number ' + str(i) + Style.RESET_ALL)
        y = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 1]
        print(Back.GREEN + 'This is y: \n' + Style.RESET_ALL)
        print(y)
        x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]
        print(Back.GREEN + 'This is x: \n' + Style.RESET_ALL)
        print(x)
        print('filtration fuction main step')
        stdev = np.std(y)
        print('second step')
        mean = np.mean(y)
        print('third step')
4

1 回答 1

0

您是否尝试过逐步测试问题线?

x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]

例如:

temp = np.array(list(map(list, list(filter(flt, list(zap))))))
print(temp.shape, temp.dtype)
x = temp[:, 0]

可能需要进一步细分,但由于[:,0]是这一行中唯一的索引操作,我将从那里开始。

如果不进一步研究代码和/或一些示例,我不会试图推测嵌套lists在做什么。

错误听起来temp不是 2d,这与您的预期相反。这可能是因为temp是对象 dtype,并且由长度不同的列表组成。当人们从下载的数据库中制作数组时,这似乎是一个常见的问题。

于 2019-12-14T22:06:10.690 回答