使用argmax numpy Python进行数组格式化 [英] Array formatting with argmax numpy Python
问题描述
我在使用 Numbers [(Numbers< = 0).argmax():] = 0
函数时遇到麻烦,如果满足条件,应该将其后面的所有元素都设为零,但是如果不满足条件,它将把所有数组元素都归零.我该如何解决此问题.如果不满足 Numbers< = 0
条件,则数组不应更改.
I am having trouble with the Numbers[(Numbers<=0).argmax():] = 0
function it is supposed to turn all the elements behind it into zeroes if the condition is met, however if the condition is not met it turns all the array elements into zeroes. How can i fix this issue. If the Numbers<=0
condition is not met the array should not change.
满足条件为-35.15610151的数组:
Array with satisfying condition at -35.15610151:
Numbers = np.array([123.6, 123.6 , 123.6, 110.3748, 111.6992976,
102.3165566, 97.81462811 , 89.50038472 , 96.48141473 , 90.49956702,
88.59907611 , 77.96718698, 61.51611052, 56.84088612, 55.36302309,
54.69866681, 56.44902415 , 59.49727145, 42.12406819, 27.42276839,
33.86711896, 32.10602877, -35.15610151, 32.34361339 , 29.20628289])
Numbers[(Numbers<=0).argmax():] = 0
输出:
[123.6 123.6 123.6 110.3748 111.6992976
102.3165566 97.81462811 89.50038472 96.48141473 90.49956702
88.59907611 77.96718698 61.51611052 56.84088612 55.36302309
54.69866681 56.44902415 59.49727145 42.12406819 27.42276839
33.86711896 32.10602877 0. 0. 0. ]
不满足条件的数组将-35.15610151转换为+35.15610151:
Array with no satisfying condition, turned -35.15610151 into +35.15610151:
Numbers = np.array([123.6, 123.6 , 123.6, 110.3748, 111.6992976,
102.3165566, 97.81462811 , 89.50038472 , 96.48141473 , 90.49956702,
88.59907611 , 77.96718698, 61.51611052, 56.84088612, 55.36302309,
54.69866681, 56.44902415 , 59.49727145, 42.12406819, 27.42276839,
33.86711896, 32.10602877, 35.15610151, 32.34361339 , 29.20628289])
Numbers[(Numbers<=0).argmax():] = 0
输出:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0.]
推荐答案
尝试这2种方法,一种方法已分配给NumPy视图,另一种方法创建了一个要分配给另一个变量的新数组-
Try these 2 methods, one is in place assignment to the NumPy view and the other creates a fresh array to be assigned to another variable -
#Method 1 (Inplace assignment)
Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0
或者
#Method 2 (Not inplace)
np.where(~(Numbers<=0).cumsum(dtype=bool), Numbers, 0)
或者
#As an excellent suggestion by Mad Physicist!
Numbers[np.logical_or.accumulate(Numbers >= 0)] = 0
说明-
-
返回
[F,F,F,T,F,F,F]
的布尔数组可以看作是1和0的数组.进行总和最终会使第一个T传播到后续元素.
The bool array that returns
[F, F, F, T, F, F, F]
can be seen as an array of 1s and 0s. Doing a cumsum ends up propogating the first T to the subsequent elements.
因此,这会将数组转换为 [F,F,F,T,T,T,T]
,现在可以将其与布尔索引一起使用代码>,然后将视图设置为0或
np.where
以获取原始元素,或者通过使用〜
This, therefore, turns the array as [F, F, F, T, T, T, T]
which can now be used with just boolean indexing
and set the view to 0 OR np.where
to fetch original elements or 0 based on reversing the boolean with ~
这里的优点是,如果您的数组仅由False组成,则意味着没有元素满足条件,它只会返回原始的 Numbers
本身,而不是将它们设置为0.>
Advantage here is that if your array is just composed of False, meaning no element meets the condition, it just returns the original Numbers
itself, instead of setting them to 0.
运行测试-
- 具有符合条件的值
Numbers = np.array([123.6 , 123.6 , -123.6, 110.3748 , 111.6992976, 102.3165566, 97.81462811])
Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0
#array([123.6, 123.6, 0. , 0. , 0. , 0. , 0. ])
- 没有符合条件的值
Numbers = np.array([123.6 , 123.6 , 123.6, 110.3748 , 111.6992976, 102.3165566, 97.81462811])
Numbers[(Numbers<=0).cumsum(dtype=bool)] = 0
#array([123.6 , 123.6 , 123.6 , 110.3748 ,111.6992976 , 102.3165566 , 97.81462811])
编辑:根据要求提供新方案
Numbers1 = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
Numbers2 = np.array([1,2,-3,4,5])
Numbers2 = np.where(~(Numbers2<=0).cumsum().astype(bool), Numbers1, 0)
Numbers2
array([1.1, 2.2, 0. , 0. , 0. ])
这篇关于使用argmax numpy Python进行数组格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!