简体   繁体   English

值错误:对象深度不足,无法容纳所需数组

[英]Value Error: object of too small depth for desired array

I want to correlate s1 and s2 variables in my zip_list. 我想在zip_list中关联s1和s2变量。 However, I have this error: 但是,我有此错误:

"return multiarray.correlate2(a, v, mode) ValueError: object of too small depth for desired array" “返回multiarray.correlate2(a,v,mode)ValueError:深度太深的对象对于所需数组而言”

Is there anyone who could help me? 有谁可以帮助我吗?


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

Those are two numpy arrays of 3-character-long strings: 这是两个3个字符长的字符串的numpy数组:

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

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. 使用numpy库(可能存在其他进行单词分析的库)可能不会使字符串相关联,因此很可能在将它们用作实际数字之后使用。 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

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. 如我添加的注释中所示, xy将引用这两个标识符的最后一个设置,这是在它们最后一次运行第一个for循环期间。 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

Moreover, you're doing this over and over, for the exact same values because x and y have not been rebound to different values. 而且,您要一遍又一遍地执行这些操作,因为它们的值完全相同,因为x和y尚未反弹到不同的值。 Depending on which version of numpy you're using, you'll get a different error message. 根据您使用的numpy版本,您会收到不同的错误消息。 Mine differs a bit from yours but not by much. 我的与您的相差不大,但相差不大。

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循环更改为:

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

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: 如果您想使两个一维数组s1和s2相关联(这很可能),只需摆脱第二个for循环(也不需要第一个for循环)并编写:

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

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. 这是使用函数的“有效”模式不等大小(大小为mm+1 )的2个一维数组( squeeze函数摆脱了不必要的2D性质)的相关性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 numpy:ValueError:对象深度太小,无法获得所需的数组 - Numpy : ValueError: object of too small depth for desired array 使用 scipy.signal.lfilter 时,实现巴特沃斯带通滤波器遇到:“ValueError: object of too small depth for desired array”错误 - Implementing Butterworth bandpass filter running into: "ValueError: object of too small depth for desired array" error when using scipy.signal.lfilter np.interp 在脚本中失败 - 但在独立测试中没有? (ValueError:object 所需数组的深度太小) - np.interp fails in script - but not in standalone test? (ValueError: object of too small depth for desired array) 插值问题 - ValueError:所需数组的深度太小(Python,numpy) - Interpolation Issue - ValueError: object of too small depth for desired array (Python, numpy) scipy.signal.lfilter: *** ValueError: 所需数组的深度太小 - scipy.signal.lfilter: *** ValueError: object of too small depth for desired array Matlab 过滤器与深度太小的 Python lfilter ValueError 对象 - Matlab filter with Python lfilter ValueError object of too small depth python lmfit“对象对于所需数组而言太深” - python lmfit “object too deep for desired array” ValueError:&#39;对象太深,不适合所需的数组&#39; - ValueError: 'object too deep for desired array' ValueError:对象对于所需数组而言太深 - ValueError: object too deep for desired array ValueError:对象太深,无法放入所需的数组(netcdf) - ValueError: object too deep for desired array (netcdf)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM