Value Error: object of too small depth for desired array

Value Error: object of too small depth for desired array

我想关联 zip_list 中的 s1 和 s2 变量。但是,我有这个错误:

"return multiarray.correlate2(a, v, 模式) ValueError:所需数组的对象深度太小

有没有人可以帮助我?


s1 = [] 
s2 = []
date = []

for f in files:
    with open(f) as f:
    f.next()
    rows = csv.reader(f)
    for row in rows:
        item_list = []
        for row_item in row:
            output_string = map(lambda x: '0' if x=='NULL' else x, row_item.split(","))
            item_list.append(output_string)
        date = item_list[0]
        s1 = item_list[2]
        s2 = item_list[3]

        zip_list = []
        for x, y in zip(s1, s2):
            pos = {"s1": x, "s2": y}
            zip_list.append(pos)
            print zip_list

        for line in zip_list:
            print np.correlate(x,y)


    input values:

    s1: ['113']
        ['116']
        ['120']
        ['120']
        ['117']
        ['127']
        ['124']
        ['118']
        ['124']
        ['128']
        ['128']
        ['125']
        ['112']
        ['122']
        ['125']
        ['133']
        ['128']

        s2: ['125']
        ['123']
        ['120']
        ['115'] 
        ['124']
        ['120']
        ['120']
        ['119']
        ['119']
        ['122']
        ['121']
        ['116'] 
        ['116']
        ['119']
        ['116']
        ['113']

        zip_list: [{'s2': '114', 's1': '52'}]
        [{'s2': '114', 's1': '52'}]
        [{'s2': '121', 's1': '67'}]
        [{'s2': '121', 's1': '67'}]
        [{'s2': '124', 's1': '72'}]
        [{'s2': '124', 's1': '72'}]
        [{'s2': '124', 's1': '76'}]
        [{'s2': '124', 's1': '76'}]
        [{'s2': '122', 's1': '80'}]
        [{'s2': '122', 's1': '80'}]
        [{'s2': '115', 's1': '74'}]
        [{'s2': '115', 's1': '74'}]
        [{'s2': '114', 's1': '69'}]
        [{'s2': '114', 's1': '69'}]
        [{'s2': '115', 's1': '64'}]
        [{'s2': '115', 's1': '64'}]
        [{'s2': '111', 's1': '63'}]
        [{'s2': '111', 's1': '63'}]
        [{'s2': '112', 's1': '56'}]
        [{'s2': '112', 's1': '56'}]
        [{'s2': '116', 's1': '49'}]
        [{'s2': '116', 's1': '49'}]
        [{'s2': '119', 's1': '54'}]
        [{'s2': '119', 's1': '54'}]
        [{'s2': '119', 's1': '54'}]

首先,将您的代码减少到最低限度将使您更深入地了解失败的地方:

import numpy as np

s1 = np.array([['113'],['116'],['120'],['120'],['117'],['127'],['124'],['118'],
    ['124'],['128'],['128'],['125'],['112'],['122'],['125'],['133'],['128']])

s2 = np.array([['125'],['123'],['120'],['115'] ,['124'],['120'],['120'],['119'],
    ['119'],['122'],['121'],['116'],['116'],['119'],['116'],['113']])

这是两个 3 个字符长的字符串的 numpy 数组:

>>> s1.dtype
dtype('<U3')

关联字符串不是您可能会使用 numpy 库执行的操作(存在其他可进行单词分析的库),因此您很可能将这些用作实际数字。先转换它们:

s1 = s1.astype(np.int)
s2 = s2.astype(np.int)

现在,错误实际上是由于您使用了仅在循环中使用但在该循环之外引用的标识符。更具体地说,您的代码片段在这里:

    zip_list = []
    for x, y in zip(s1, s2):
        pos = {"s1": x, "s2": y}
        zip_list.append(pos)

    for line in zip_list:
        print np.correlate(x,y) # <- x and y here take the last known values

如我添加的评论所示,xy 将引用这两个标识符的最后设​​置,这是在它们的最后 运行 到第一个期间for 循环。这意味着,在您尝试关联的那一行,您实际上正在执行这段代码:

np.correlate(np.array(['133'], dtype='<U3'), np.array(['113'], dtype='<U3')) # Doesn't make sense

此外,您一遍又一遍地执行此操作以获得完全相同的值,因为 x 和 y 尚未反弹到不同的值。根据您使用的 numpy 版本,您会收到不同的错误消息。我的和你的有点不同,但差别不大。

如果您真的想 "correlate" 每行两个数字(不太可能,因为这与成对乘法相同),您应该将第二个 for 循环更改为:

for a,b in zip_list:
    print(np.correlate(a,b))

如果你想将两个一维数组 s1 和 s2 相关联(这很有可能),只需去掉第二个 for 循环(第一个也不是必需的)并编写:

>>> np.correlate(np.squeeze(s1), np.squeeze(s2)) # see note below
array([232662, 234543])

这是 2 个不等大小(大小 mm+1)的一维数组(squeeze 函数摆脱了不必要的二维性质)的相关性函数的 "valid" 模式。