追加到多维数组Python [英] Appending to a multidimensional array Python

查看:56
本文介绍了追加到多维数组Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在筛选数组ab以查找相同的值,然后我想将它们附加到一个新数组difference,但是我收到错误:ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 2。我如何能够修复此问题?

import numpy as np
a = np.array([[0,12],[1,40],[0,55],[1,23],[0,123.5],[1,4]])
b = np.array([[0,3],[1,10],[0,55],[1,34],[1,122],[0,123]])
difference= np.array([[]])


for i in a:
    for j in b:
        if np.allclose(i, j, atol=0.5):
            difference = np.concatenate((difference,[i]))

预期输出:

[[ 0. 55.],[  0.  123.5]]

推荐答案

In [22]: a = np.array([[0,12],[1,40],[0,55],[1,23],[0,123.5],[1,4]])
    ...: b = np.array([[0,3],[1,10],[0,55],[1,34],[1,122],[0,123]])

使用直截了当的列表理解:

In [23]: [i for i in a for j in b if np.allclose(i,j,atol=0.5)]
Out[23]: [array([ 0., 55.]), array([  0. , 123.5])]

但至于你方的连结。查看数组的形状:

In [24]: np.array([[]]).shape
Out[24]: (1, 0)
In [25]: np.array([i]).shape
Out[25]: (1, 1)
这些只能在轴1上联接;默认值为0,这会给您带来错误。就像评论中写的那样,您必须了解数组形状才能使用concatenate

In [26]: difference= np.array([[]])
    ...: for i in a:
    ...:     for j in b:
    ...:         if np.allclose(i, j, atol=0.5):
    ...:             difference = np.concatenate((difference,[i]), axis=1)
    ...: 
In [27]: difference
Out[27]: array([[  0. ,  55. ,   0. , 123.5]])

矢量化

全数组方法:

Broadcaseab,生成(5,5,2)接近数组:

In [37]: np.isclose(a[:,None,:],b[None,:,:], atol=0.5)
Out[37]: 
array([[[ True, False],
        [False, False],
        [ True, False],
        [False, False],
        [False, False],
        [ True, False]],

       [[False, False],
        [ True, False],
        [False, False],
        [ True, False],
        [ True, False],
        [False, False]],

       [[ True, False],
        [False, False],
        [ True,  True],
        [False, False],
        [False, False],
        [ True, False]],

       [[False, False],
        [ True, False],
        [False, False],
        [ True, False],
        [ True, False],
        [False, False]],

       [[ True, False],
        [False, False],
        [ True, False],
        [False, False],
        [False, False],
        [ True,  True]],

       [[False, False],
        [ True, False],
        [False, False],
        [ True, False],
        [ True, False],
        [False, False]]])

查找两列均为TRUE的位置,以及至少有一行&Quot;为:

的位置
In [38]: _.all(axis=2)
Out[38]: 
array([[False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False,  True, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False,  True],
       [False, False, False, False, False, False]])
In [39]: _.any(axis=1)
Out[39]: array([False, False,  True, False,  True, False])
In [40]: a[_]
Out[40]: 
array([[  0. ,  55. ],
       [  0. , 123.5]])

这篇关于追加到多维数组Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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