更改Numpy对象中的每个元素
我有一个随机N*M元素的Numpy对象,还有两个数字a和B 现在我想访问这个N*M数组中的每个元素并进行更改,即,如果元素>0,则将此元素替换为a(即元素IIUC: 使用更改Numpy对象中的每个元素,numpy,Numpy,我有一个随机N*M元素的Numpy对象,还有两个数字a和B 现在我想访问这个N*M数组中的每个元素并进行更改,即,如果元素>0,则将此元素替换为a(即元素IIUC: 使用np.where: np.where(narr > 0, A, np.where(narr < 0, B , narr)) IIUC: 使用np.where: np.where(narr > 0, A, np.where(narr < 0, B , narr)) 布尔屏蔽赋值将就地更改值: In [4
np.where
:
np.where(narr > 0, A, np.where(narr < 0, B , narr))
IIUC:
使用np.where
:
np.where(narr > 0, A, np.where(narr < 0, B , narr))
布尔屏蔽赋值将就地更改值:
In [493]: arr = np.random.randint(-10,10,(5,7))
In [494]: arr
Out[494]:
array([[ -5, -6, -7, -1, -8, -8, -10],
[ -9, 1, -3, -9, 3, 8, -1],
[ 6, -7, 4, 0, -4, 4, -2],
[ -3, -10, -2, 7, -4, 2, 2],
[ -5, 5, -1, -7, 7, 5, -7]])
In [495]: arr[arr>0] = 100
In [496]: arr[arr<0] = -50
In [497]: arr
Out[497]:
array([[-50, -50, -50, -50, -50, -50, -50],
[-50, 100, -50, -50, 100, 100, -50],
[100, -50, 100, 0, -50, 100, -50],
[-50, -50, -50, 100, -50, 100, 100],
[-50, 100, -50, -50, 100, 100, -50]])
[493]中的:arr=np.random.randint(-10,10,(5,7))
在[494]中:arr
出[494]:
数组([-5,-6,-7,-1,-8,-8,-10],
[ -9, 1, -3, -9, 3, 8, -1],
[ 6, -7, 4, 0, -4, 4, -2],
[ -3, -10, -2, 7, -4, 2, 2],
[ -5, 5, -1, -7, 7, 5, -7]])
在[495]中:arr[arr>0]=100
在[496]中:arr[arr布尔掩码赋值将就地更改值:
In [493]: arr = np.random.randint(-10,10,(5,7))
In [494]: arr
Out[494]:
array([[ -5, -6, -7, -1, -8, -8, -10],
[ -9, 1, -3, -9, 3, 8, -1],
[ 6, -7, 4, 0, -4, 4, -2],
[ -3, -10, -2, 7, -4, 2, 2],
[ -5, 5, -1, -7, 7, 5, -7]])
In [495]: arr[arr>0] = 100
In [496]: arr[arr<0] = -50
In [497]: arr
Out[497]:
array([[-50, -50, -50, -50, -50, -50, -50],
[-50, 100, -50, -50, 100, 100, -50],
[100, -50, 100, 0, -50, 100, -50],
[-50, -50, -50, 100, -50, 100, 100],
[-50, 100, -50, -50, 100, 100, -50]])
[493]中的:arr=np.random.randint(-10,10,(5,7))
在[494]中:arr
出[494]:
数组([-5,-6,-7,-1,-8,-8,-10],
[ -9, 1, -3, -9, 3, 8, -1],
[ 6, -7, 4, 0, -4, 4, -2],
[ -3, -10, -2, 7, -4, 2, 2],
[ -5, 5, -1, -7, 7, 5, -7]])
在[495]中:arr[arr>0]=100
在[496]:arr[arr中,因为您提到您对计算速度感兴趣,所以我对您的问题的几种不同方法进行了速度比较
test.py:
import numpy as np
A = 100
B = 50
def createArray():
array = np.random.randint(-100,100,(500,500))
return array
def replace(x):
return A if x > 0 else B
def replace_ForLoop():
"""Simple for-loop."""
array = createArray()
for i in range(array.shape[0]):
for j in range(array.shape[1]):
array[i][j] = replace(array[i][j])
def replace_nditer():
"""Use numpy.nditer to iterate over values."""
array = createArray()
for elem in np.nditer(array, op_flags=['readwrite']):
elem[...] = replace(elem)
def replace_masks():
"""Use boolean masks."""
array = createArray()
array[array>0] = A
array[array<0] = B
def replace_vectorize():
"""Use numpy.vectorize"""
array = createArray()
vectorfunc = np.vectorize(replace)
array = vectorfunc(array)
def replace_where():
"""Use numpy.where"""
array = createArray()
array = np.where(array > 0, A, np.where(array < 0, B , array))
使用循环确实非常慢。numpy.nditer
甚至更慢,这让我感到惊讶,因为文档称它为一个.numpy.vectorize
,但它的速度仍然是原始实现的三倍。
根据hpaulj的回答,Scott Boston提出的np.where
变体比使用布尔掩码稍微快一点。但是,它确实需要更多内存,因为它不会在原地修改。因为您提到您对计算速度感兴趣,所以我对几种不同的方法进行了速度比较问题
test.py:
import numpy as np
A = 100
B = 50
def createArray():
array = np.random.randint(-100,100,(500,500))
return array
def replace(x):
return A if x > 0 else B
def replace_ForLoop():
"""Simple for-loop."""
array = createArray()
for i in range(array.shape[0]):
for j in range(array.shape[1]):
array[i][j] = replace(array[i][j])
def replace_nditer():
"""Use numpy.nditer to iterate over values."""
array = createArray()
for elem in np.nditer(array, op_flags=['readwrite']):
elem[...] = replace(elem)
def replace_masks():
"""Use boolean masks."""
array = createArray()
array[array>0] = A
array[array<0] = B
def replace_vectorize():
"""Use numpy.vectorize"""
array = createArray()
vectorfunc = np.vectorize(replace)
array = vectorfunc(array)
def replace_where():
"""Use numpy.where"""
array = createArray()
array = np.where(array > 0, A, np.where(array < 0, B , array))
使用循环确实非常慢。numpy.nditer
甚至更慢,这让我感到惊讶,因为文档称它为一个.numpy.vectorize
,但它的速度仍然是原始实现的三倍。
Scott Boston提出的np.where
变体比hpaulj的答案中使用布尔掩码的速度稍快。但是,它确实需要更多内存,因为它不会在原地修改。试试numpy.clip
。看看np.where
试试numpy.clip
。看看np.where
这很有帮助!但是原版阵列narr尚未更改,因此我需要分配新内存来保存此新阵列。我们可以就地执行此更改吗?这很有帮助!但原始阵列narr尚未更改,因此我需要分配新内存来保存此新阵列。我们可以就地执行此更改吗?