值错误:所需数组的深度太小的对象

时间:2016-02-16 07:44:35

标签: python arrays correlation

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

"返回multiarray.correlate2(a,v,mode) 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'}]

1 个答案:

答案 0 :(得分:3)

首先,将代码降低到最低限度可以让您更深入地了解失败的地方:

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-loop(并且第一个也不是必需的)并写:

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

这是使用函数的“不等大小(大小squeezem)的2个一维数组(m+1函数摆脱不必要的2D性质)的相关性”有效的“模式。