gpt4 book ai didi

python - 如何根据一行的平均值设置阈值?

转载 作者:太空宇宙 更新时间:2023-11-04 08:30:48 25 4
gpt4 key购买 nike

我有一个二维数组。我想将每行中大于该行平均值的所有值设置为 0。一些天真的代码是:

new_arr = arr.copy()
for i, row in enumerate(arr):
avg = np.mean(row)
for j, pixel in enumerate(row):
if pixel > avg:
new_arr[i,j] = 0
else:
new_arr[i,j] = 1

这很慢,我想知道是否有某种方法可以使用 Numpy 索引来做到这一点?如果它是整个矩阵的平均值,我可以简单地做:

mask = arr > np.mean(arr)
arr[mask] = 0
arr[np.logical_not(mask)] = 1

是否有某种方法可以使用一维平均值数组或类似的东西来对每行平均值执行此操作?

编辑:建议的解决方案:

avg = np.mean(arr, axis=0)
mask = arr > avg
new_arr = np.zeros(arr.shape)
arr[mask] = 1

实际上使用的是列平均,这对某些人也可能有用。它相当于:

new_arr = arr.copy()
for i, row in enumerate(arr.T):
avg = np.mean(row)
for j, pixel in enumerate(row):
if pixel > avg:
new_arr[j,i] = 0
else:
new_arr[j,i] = 1

最佳答案

设置

a = np.arange(25).reshape((5,5))

您可以将 keepdimsmean 一起使用:

a[a > a.mean(1, keepdims=True)] = 0

array([[ 0,  1,  2,  0,  0],
[ 5, 6, 7, 0, 0],
[10, 11, 12, 0, 0],
[15, 16, 17, 0, 0],
[20, 21, 22, 0, 0]])

使用 keepdims=True,为 mean 提供以下结果:

array([[ 2.],
[ 7.],
[12.],
[17.],
[22.]])

这样做的好处是in the docs :

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

关于python - 如何根据一行的平均值设置阈值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com