Numpy 从最大值中减去标准偏差

Numpy 从最大值中减去标准偏差,numpy,Numpy,我有一个数组a(形状(5,4)) 我想从最大值(15)中减去标准偏差,这样最终结果将是 [[ 8 9 10 11] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 12.23413666] [ 8 9 10 11]] 我如何才能做到这一点?将numpy作为np导入 a=np.数组([[8,9,10,11], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [8,9,10,11]],dtype=f

我有一个数组
a
(形状(5,4))

我想从最大值(15)中减去标准偏差,这样最终结果将是

[[ 8  9 10 11]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 12.23413666]
 [ 8  9 10 11]]
我如何才能做到这一点?

将numpy作为np导入
a=np.数组([[8,9,10,11],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15],
[8,9,10,11]],dtype=float)
#获取最大值的索引
索引=np.其中(a==a.max())
#从值中减去std
a[index]=a.std()
印刷品(a)
这应该可以:

import numpy as np
a = np.array([[ 8.,  9., 10., 11.,], [ 4.,  5.,  6.,  7.], [ 8.,  9., 10., 11.], [12., 13., 14., 15.], [ 8.,  9., 10., 11.]])
max_index = np.unravel_index(a.argmax(), a.shape)
a[max_index] -= a.std()
print(a)

对于max_index,必须使用unave_index函数,因为numpy的argmax从展平数组返回索引。在此之后,您只需从max_索引的值中减去标准偏差。

到目前为止您尝试了什么?告诉我们你的处境。
import numpy as np
a = np.array([[ 8.,  9., 10., 11.,], [ 4.,  5.,  6.,  7.], [ 8.,  9., 10., 11.], [12., 13., 14., 15.], [ 8.,  9., 10., 11.]])
max_index = np.unravel_index(a.argmax(), a.shape)
a[max_index] -= a.std()
print(a)