【发布时间】:2018-10-09 19:57:44
【问题描述】:
使用时:
import numpy as np
A = np.array([1,2,-3,-1, 0,3,-1])
print [max(A[j], 0) for j in range(len(A))]
我们会根据需要得到[1, 2, 0, 0, 0, 3, 0]。
如何直接用一个numpy函数得到一样的,比如np.max?
print max(A, 0) # ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
print np.max(A, 0) # 3
print np.max(A, 0, axis=0) # argument axis not working
print np.amax(A, 0) # 3
【问题讨论】:
-
..
np.maximum? -
如果你只是在
A和0之间得到最大值(比如relu),那么你可以做A*(A>0) -
@Divakar 哦,没错!我一直认为 np.max 是 np.maximum 的别名,因为我找不到 np.max 的任何文档...此链接不起作用:docs.scipy.org/doc/numpy/reference/generated/numpy.max.html
-
@Divakar 请原谅我,但我认为这里的问题是关于 np.max 的文档不存在/np.max 和 np.maximum 之间的混淆,而不是关于“有条件地结合两个相同形状的numpy数组”。我的标题是相反的:有条件地比较一个数组和一个数字(显然不是相同的形状)。
-
@Basj Well
np.maximum如链接的 dup Q&A 中所述适用于所有可广播的形状,不一定是相同的形状,因此也适用于此处。我在这里读到的问题是 -How to get the same directly with a numpy function, such as np.max?和np.maximum与那里再次说明的一样完美。