值错误:对象深度太小,无法满足所需数组的需求



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

"return multiarray.correlate2(a, v, mode(值错误:对象深度太小,无法满足所需数组">

有人可以帮助我吗?


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 版本,您将收到不同的错误消息。我的和你的有点不同,但差别不大。

如果你真的想"关联"每行的两个数字(不太可能,因为这与成对乘法相同(,你应该将第二个 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 函数摆脱了不必要的 2D 性质(的相关性。

最新更新