检查集合中的值是否在python中的numpy数组中

检查集合中的值是否在python中的numpy数组中,python,numpy,Python,Numpy,我想检查NumPyArray中是否有一个集合中的值,如果是,则在数组中设置该区域=1。如果未设置,则设置keepRaster=2 numpyArray = #some imported array repeatSet= ([3, 5, 6, 8]) confusedRaster = numpyArray[numpy.where(numpyArray in repeatSet)]= 1 收益率: <type 'exceptions.TypeError'>: unhashable t

我想检查NumPyArray中是否有一个集合中的值,如果是,则在数组中设置该区域=1。如果未设置,则设置keepRaster=2

numpyArray = #some imported array
repeatSet= ([3, 5, 6, 8])

confusedRaster = numpyArray[numpy.where(numpyArray in repeatSet)]= 1
收益率:

<type 'exceptions.TypeError'>: unhashable type: 'numpy.ndarray'
要澄清并寻求进一步的帮助:

我现在正在做的是把光栅输入到一个数组中。我需要读取二维数组中的值,并基于这些值创建另一个数组。如果数组值在集合中,则该值将为1。如果它不在一个集合中,那么该值将从另一个输入中导出,但我现在说77。这就是我目前正在使用的。我的测试输入大约有1500行和3500列。它总是在350排左右结冰

for rowd in range(0, width):
    for cold in range (0, height):
        if numpyarray.item(rowd,cold) in repeatSet:
            confusedArray[rowd][cold] = 1
        else:
            if numpyarray.item(rowd,cold) == 0:
                confusedArray[rowd][cold] = 0
            else:
                confusedArray[rowd][cold] = 2

这里有一种可能的方法来做你想做的事情:

numpyArray = np.array([1, 8, 35, 343, 23, 3, 8]) # could be n-Dimensional array
repeatSet = np.array([3, 5, 6, 8])
mask = (numpyArray[...,None] == repeatSet[None,...]).any(axis=-1) 
print mask
>>> [False  True False False False  True  True]

在1.4及更高版本中,numpy提供了该功能

>>> test = np.array([0, 1, 2, 5, 0])
>>> states = [0, 2]
>>> np.in1d(test, states)
array([ True, False,  True, False,  True], dtype=bool)
您可以将其用作分配的掩码

>>> test[np.in1d(test, states)] = 1
>>> test
array([1, 1, 1, 5, 1])
下面是numpy的索引和赋值语法的一些更复杂的用法,我认为这些用法将适用于您的问题。注意使用位运算符替换基于逻辑的
if

>>> numpy_array = numpy.arange(9).reshape((3, 3))
>>> confused_array = numpy.arange(9).reshape((3, 3)) % 2
>>> mask = numpy.in1d(numpy_array, repeat_set).reshape(numpy_array.shape)
>>> mask
array([[False, False, False],
       [ True, False,  True],
       [ True, False,  True]], dtype=bool)
>>> ~mask
array([[ True,  True,  True],
       [False,  True, False],
       [False,  True, False]], dtype=bool)
>>> numpy_array == 0
array([[ True, False, False],
       [False, False, False],
       [False, False, False]], dtype=bool)
>>> numpy_array != 0
array([[False,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> confused_array[mask] = 1
>>> confused_array[~mask & (numpy_array == 0)] = 0
>>> confused_array[~mask & (numpy_array != 0)] = 2
>>> confused_array
array([[0, 2, 2],
       [1, 2, 1],
       [1, 2, 1]])
另一种方法是使用
numpy.where
,它创建了一个全新的数组,使用
mask
为true的第二个参数中的值,以及
mask
为false的第三个参数中的值。(与赋值一样,参数可以是标量或与
mask
形状相同的数组)这可能比上面的方法更有效,而且肯定更简洁:

>>> numpy.where(mask, 1, numpy.where(numpy_array == 0, 0, 2))
array([[0, 2, 2],
       [1, 2, 1],
       [1, 2, 1]])

在最近的
numpy
中,您可以使用和的组合来实现此结果。第一种方法输出一个布尔numpy数组,该数组的值计算为
True
,其中其值等于类似于指定测试元素的数组(请参见),而第二种方法可以创建一个新数组,该数组可以设置一个值,其中指定的限定值计算为
True
,另一个值设置为
False

例子 我将用随机数组做一个示例,但使用您提供的特定值

import numpy as np

repeatSet = ([2, 5, 6, 8])

arr = np.array([[1,5,1],
                [0,1,0],
                [0,0,0],
                [2,2,2]])

out = np.where(np.isin(arr, repeatSet), 1, 77)

> out
array([[77,  1, 77],
       [77, 77, 77],
       [77, 77, 77],
       [ 1,  1,  1]])

嗯,据我所知。如果测试值在状态列表中,则为True,即=1,否则将等于原来的值。有没有办法让输出数组([1,0,1,0,0])@mkmitchell,是的,你明白了。这有点类似于普通Python列表中的切片赋值,但a)使用numpy更复杂的索引系统,b)遵循numpy惯例,将标量赋值给数组的切片会将切片中的所有值赋值给该标量值。如果是二维数组呢?@mkmitchell,只要布尔掩码的形状与原始数组的形状相同,它的工作原理就应该相同。@mkmitchell,在二维数组上使用
in1d
的结果时,我发现在索引二维数组之前必须
对其进行整形。换句话说,如果
test=np.arange(25)。重塑((5,5))
那么
(test[np.inad(test,states)。重塑(test.shape)]==np.array([0,2])。all()==True
import numpy as np

repeatSet = ([2, 5, 6, 8])

arr = np.array([[1,5,1],
                [0,1,0],
                [0,0,0],
                [2,2,2]])

out = np.where(np.isin(arr, repeatSet), 1, 77)

> out
array([[77,  1, 77],
       [77, 77, 77],
       [77, 77, 77],
       [ 1,  1,  1]])