二维和一维数组numpy的交叉口 [英] Intersection of 2d and 1d Numpy array

查看:170
本文介绍了二维和一维数组numpy的交叉口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关在阵列 A的每一个元素[:3:] ,这也是在阵列 B ,我想将值设置为0,并且会创建阵列结果

For every element in array A[:,3:] that is also in array B, I want to set the value to 0, which creates the array result

import numpy as np

A = np.array([[1, 1, 10, 101, 102, 103,   0,   0],
              [2, 2, 10, 102, 108,   0,   0,   0],
              [3, 3, 11, 101, 102, 106, 107, 108]])

B = np.array([101, 106, 108])

result = np.array([[1, 1, 10,   0, 102, 103,   0,   0],
                   [2, 2, 10, 102,   0,   0,   0,   0],
                   [3, 3, 11,   0, 102,   0, 107,   0]])

我知道有一种方法可以做到这一点使用 in1d 和广播 A 作为一维数组,但我不知道如何去了解这一点。

I know there is a way to do this using in1d and broadcasting A as a 1D array, but I have no idea how to go about this.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

如果您在切片二维数组饲料 A [:3:] 来的 np.in1d​​ ,将它压平为一维数组和 B比较,从而创造一个1D面具​​,这可能是重塑和用于布尔检索到该切片数组设置 TRUE 元素。一行程序的执行将是这个样子 -

If you feed in that sliced 2D array A[:,3:] to np.in1d, it would flatten it to a 1D array and compare with B for occurrences and thus create a 1D mask, which could be reshaped and used for boolean indexing into that sliced array to set the TRUE elements to zeros. A one-liner implementation would look something like this -

A[:,3:][np.in1d(A[:,3:],B).reshape(A.shape[0],-1)] = 0

样运行 -

In [37]: A
Out[37]: 
array([[  1,   1,  10, 101, 102, 103,   0,   0],
       [  2,   2,  10, 102, 108,   0,   0,   0],
       [  3,   3,  11, 101, 102, 106, 107, 108]])

In [38]: np.in1d(A[:,3:],B) # Flattened mask
Out[38]: 
array([ True, False, False, False, False, False,  True, False, False,
       False,  True, False,  True, False,  True], dtype=bool)

In [39]: np.in1d(A[:,3:],B).reshape(A.shape[0],-1) # Reshaped mask
Out[39]: 
array([[ True, False, False, False, False],
       [False,  True, False, False, False],
       [ True, False,  True, False,  True]], dtype=bool)

In [40]: A[:,3:][np.in1d(A[:,3:],B).reshape(A.shape[0],-1)] = 0 # Final code

In [41]: A
Out[41]: 
array([[  1,   1,  10,   0, 102, 103,   0,   0],
       [  2,   2,  10, 102,   0,   0,   0,   0],
       [  3,   3,  11,   0, 102,   0, 107,   0]])


为了让事情简单,你可以创建一个视图扁平 A 并使用 np.in1d​​ 有一个更优雅的解决方案。对于仅更改切片 A的解决方案[:3:] ,您可以使用 .flat ,然后指数像这样 -


To make things simpler, you could create a view of the flattened A and use the 1D mask obtained from np.in1d to have a more elegant solution. For a solution that changes only the sliced A[:,3:], you can use .flat and then index like so -

A[:,3:].flat[np.in1d(A[:,3:],B)] = 0

有关的案件时,你想整组匹配的人 A ,您可以使用 .ravel() -

For a case when you would like to set matching ones across entire A, you can use .ravel() -

A.ravel()[np.in1d(A,B)] = 0

我知道 .ravel()是一个视图,然后从文档,似乎 .flat 不无论是创建一个副本,所以这应该是的便宜

I know .ravel() is a view and from the docs, it seems .flat doesn't create a copy either, so these should be cheap.

这篇关于二维和一维数组numpy的交叉口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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