Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两侧的Numpy遮罩阵列_Python_Arrays_Numpy - Fatal编程技术网

Python 两侧的Numpy遮罩阵列

Python 两侧的Numpy遮罩阵列,python,arrays,numpy,Python,Arrays,Numpy,假设一个numpy数组x。我如何做x[x>value]=max(value1,x[x>value]) 换句话说,如果元素x[i]>value,那么x[i]=max(value1,x[i]) 谢谢 编辑 使用numpy.max而不是numpy.max解决了问题。当我有numpy.max时,我收到了错误消息TypeError:只有整数标量数组可以转换为标量索引创建数组: In [361]: x = np.random.randint(0,10,10) In [362]: x Out[362]: ar

假设一个numpy数组x。我如何做
x[x>value]=max(value1,x[x>value])

换句话说,如果元素
x[i]>value
,那么
x[i]=max(value1,x[i])

谢谢

编辑 使用
numpy.max
而不是
numpy.max
解决了问题。当我有
numpy.max
时,我收到了错误消息
TypeError:只有整数标量数组可以转换为标量索引

创建数组:

In [361]: x = np.random.randint(0,10,10)
In [362]: x
Out[362]: array([7, 8, 4, 8, 1, 2, 6, 6, 3, 9])
确定要替换的值:

In [363]: mask = x>5
In [364]: mask
Out[364]: 
array([ True,  True, False,  True, False, False,  True,  True, False,
        True])
In [365]: x[mask]
Out[365]: array([7, 8, 8, 6, 6, 9])
替换值:

In [368]: np.maximum(7, x[mask])
Out[368]: array([7, 8, 8, 7, 7, 9])
只要双方的术语数量相同(
shape
实际上):

由于这实际上只是在5和7之间更改值,我们可以使用:

In [378]: x = np.array([7, 8, 4, 8, 1, 2, 6, 6, 3, 9])
In [379]: mask = (x>5) & (x<7)
In [380]: mask
Out[380]: 
array([False, False, False, False, False, False,  True,  True, False,
       False])
In [381]: x[mask]
Out[381]: array([6, 6])
In [382]: x[mask] = 7
[378]中的
:x=np.数组([7,8,4,8,1,2,6,6,3,9])

在[379]:mask=(x>5)和(x中,可以使用
np。其中
和单个组合条件:

np.where((x>value)&(x<value1), value1, x)
np.where((x>value)&(x<value1), value1, x)
x[(x>value) & (x<value1)] = value1
x = np.arange(20).reshape(4,5)
value=5
value1=10
#output:
array([[ 0,  1,  2,  3,  4],
       [ 5, 10, 10, 10, 10],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])