python - 值错误 : object of too small depth for desired array

标签 python arrays correlation

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

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

最佳答案

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

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

这是两个大小不等(大小 mm)的一维数组(squeeze 函数消除了不必要的二维性质)的相关性+1)使用函数的“有效”模式。

关于python - 值错误 : object of too small depth for desired array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426578/

相关文章:

c# - 为什么 Array.Sort 在使用 Array.ForEach 后工作得更快?

c++ - 对字符串数组进行基数排序?

python - Numpy python 数组切片

python - Scipy:距离相关性高于1

r - R中栅格层之间的成对相关

r - 使用 ggpairs 创建此图

python - Python 在短路 bool 值之前会引发 TypeErrors 吗?

python - 如何将 django 项目集成到云存储

python - 为什么我不能使用 while 循环在 txt 文件中写入内容

python - 使用 MQTT 向 Google Cloud IoT Core 中的多个设备发送命令