NumPy是否可以注意数组(非限制性地)沿一个轴增加? [英] Can NumPy take care that an array is (nonstrictly) increasing along one axis?
问题描述
在numpy中是否存在一个函数来保证或更确切地说确定一个数组,使其沿一个特定轴(非严格地)增加? 例如,我有以下2D数组:
Is there a function in numpy to guarantee or rather fix an array such that it is (nonstrictly) increasing along one particular axis? For example, I have the following 2D array:
X = array([[1, 2, 1, 4, 5],
[0, 3, 1, 5, 4]])
np.foobar(X)
的输出应返回
array([[1, 2, 2, 4, 5],
[0, 3, 3, 5, 5]])
foobar
是否存在,或者我需要使用np.diff
之类的方法和一些智能索引手动进行操作吗?
Does foobar
exist or do I need to do that manually by using something like np.diff
and some smart indexing?
推荐答案
Use np.maximum.accumulate
for a running (accumulated) max value along that axis to ensure the strictly increasing criteria -
np.maximum.accumulate(X,axis=1)
样品运行-
In [233]: X
Out[233]:
array([[1, 2, 1, 4, 5],
[0, 3, 1, 5, 4]])
In [234]: np.maximum.accumulate(X,axis=1)
Out[234]:
array([[1, 2, 2, 4, 5],
[0, 3, 3, 5, 5]])
为了提高内存效率,我们可以使用其out
参数将其分配回输入以进行原位更改.
For memory efficiency, we can assign it back to the input for in-situ changes with its out
argument.
运行时测试
案例1:数组作为输入
In [254]: X = np.random.rand(1000,1000)
In [255]: %timeit np.maximum.accumulate(X,axis=1)
1000 loops, best of 3: 1.69 ms per loop
# @cᴏʟᴅsᴘᴇᴇᴅ's pandas soln using df.cummax
In [256]: %timeit pd.DataFrame(X).cummax(axis=1).values
100 loops, best of 3: 4.81 ms per loop
案例2:数据帧作为输入
Case #2 : Dataframe as input
In [257]: df = pd.DataFrame(np.random.rand(1000,1000))
In [258]: %timeit np.maximum.accumulate(df.values,axis=1)
1000 loops, best of 3: 1.68 ms per loop
# @cᴏʟᴅsᴘᴇᴇᴅ's pandas soln using df.cummax
In [259]: %timeit df.cummax(axis=1)
100 loops, best of 3: 4.68 ms per loop
这篇关于NumPy是否可以注意数组(非限制性地)沿一个轴增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!