值错误:对象深度不足,无法容纳所需数组 [英] Value Error: object of too small depth for desired array

查看:145
本文介绍了值错误:对象深度不足,无法容纳所需数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to correlate s1 and s2 variables in my zip_list. However, I have this error:

return multiarray.correlate2(a,v,mode)
ValueError:深度太深的对象对于所需数组

"return multiarray.correlate2(a, v, mode) ValueError: object of too small depth for desired array"

有人可以帮助我吗?

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'}]


推荐答案

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

First, reducing your code to the bare minimum will give you more insight in where it fails:

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数组-字符长的字符串:

Those are two numpy arrays of 3-character-long strings:

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

与字符串无关可能与numpy库有关(存在其他进行词分析的库),因此最有可能在将它们用作实际数字之后使用。首先转换它们:

Correlating strings is not something you'd likely do with the numpy library (there exist other libraries that do word analyses), so you're most likely after using these as actual numbers. Convert them first:

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

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

Now, the error actually comes from your use of an identifier which was used only in a loop, but referenced outside of that loop. More specifically, your piece of code here:

    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

如我添加的注释所示, x y 将引用这两个标识符的最后一个设置,这是他们在第一个for循环中的最后一次运行期间。这意味着,在您试图关联的那一行上,您实际上是在执行这段代码:

As shown in the comment I've added, x and y will refer to the last setting of these two identifiers, which was during their last run through the first for-loop. That means, at the line where you're trying to correlate, you're actually executing this piece of code:

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

此外,您要一遍又一遍地进行操作,因为x和y并没有反弹到完全相同的值不同的值。根据您使用的numpy版本,您会收到不同的错误消息。我的与您的相差不大,但相差不大。

Moreover, you're doing this over and over, for the exact same values because x and y have not been rebound to different values. Depending on which version of numpy you're using, you'll get a different error message. Mine differs a bit from yours but not by much.

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

If you really want to "correlate" the two numbers per line (unlikely, because that's the same as pairwise multiplication), you should change your second for-loop to this:

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

如果要关联两个一维数组s1和尽管是s2(这很有可能),但要摆脱第二个for循环(也不需要第一个for循环),然后写:

If you want to correlate the two one-dimensional arrays s1 and s2 though (which is rather likely), just get rid of the 2nd for-loop (and the first one isn't necessary either) and write:

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

是不相等的2个一维数组( squeeze 函数摆脱了不必要的2D性质)的相关性大小(大小为 m m + 1 的字符)使用功能的有效模式。

which is the correlation of 2 one-dimensional arrays (the squeeze function gets rid of the unnecessary 2D nature) of unequal size (sizes m and m+1) using the function's "valid" mode.

这篇关于值错误:对象深度不足,无法容纳所需数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆