我有一个(3,3)numpy数组,想要找出绝对值最大的元素的符号:
X = [[-2.1, 2, 3],
[ 1, -6.1, 5],
[ 0, 1, 1]]
s = numpy.argmax(numpy.abs(X),axis=0)
Run Code Online (Sandbox Code Playgroud)
给了我需要的元素的索引s = [ 0,1,1].
如何使用此数组提取元素[ -2.1, -6.1, 5]以找出其符号?
试试这个:
# You might need to do this to get X as an ndarray (for example if X is a list)
X = numpy.asarray(X)
# Then you can simply do
X[s, [0, 1, 2]]
# Or more generally
X_argmax = X[s, numpy.arange(X.shape[1])]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1171 次 |
| 最近记录: |