值错误:目标数组太小的对象。

时间:2021-09-08 20:22:38

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

我想在zip_list中关联s1和s2变量。然而,我有一个错误:

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

1 个解决方案

#1


2  

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. Convert them first:

关联字符串不是您可能使用numpy库所做的事情(有其他库做单词分析),所以您很可能在使用这些数据之后。先将它们转换为:

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. That means, at the line where you're trying to correlate, you're actually executing this piece of code:

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

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. 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.

而且,你做的是一遍又一遍,因为x和y的值没有反弹到不同的值。根据您使用的numpy版本的不同,您将得到一个不同的错误消息。我的和你的有点不同,但不是很多。

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循环(第一个也不是必需的)去掉,然后写:

>>> 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.

这就是利用函数的“有效”模式,2个一维数组(压缩函数去掉不必要的2D性质)的相关性(大小为m和m+1)。

#1


2  

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. Convert them first:

关联字符串不是您可能使用numpy库所做的事情(有其他库做单词分析),所以您很可能在使用这些数据之后。先将它们转换为:

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. That means, at the line where you're trying to correlate, you're actually executing this piece of code:

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

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. 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.

而且,你做的是一遍又一遍,因为x和y的值没有反弹到不同的值。根据您使用的numpy版本的不同,您将得到一个不同的错误消息。我的和你的有点不同,但不是很多。

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循环(第一个也不是必需的)去掉,然后写:

>>> 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.

这就是利用函数的“有效”模式,2个一维数组(压缩函数去掉不必要的2D性质)的相关性(大小为m和m+1)。