簡體   English   中英

如何找到兩個相鄰值的平均值?

[英]How to find the average values of the two adjacent values?

我想計算兩個相鄰值的平均值。 這意味着其形狀下方的數組是(6,6)。 我希望它的形狀會是(6,3)。 比如我想得到[0,0] term(1)和[0,1] term(2)的平均值,然后得到[0,2] term(45)和[0,3 ] 項 (32) 使用 for 循環...

a = np.array([[1, 2, 3, 45, 32, 9],
              [5, 23, 5, 23, 42, 5],
              [7, 34, 3, 53, 60, 12],
              [324, 135, 323, 76, 1, 51],
              [89, 573, 65, 213, 23, 8],
              [231, 53, 84, 17, 31, 1]])

你的意思是這樣的嗎?

averages = [[((row[i] + row[i+1]) / 2) for i in range(0, 5, 2)] for row in a]

Output:

[[1.5, 24.0, 20.5], 
[14.0, 14.0, 23.5], 
[20.5, 28.0, 36.0], 
[229.5, 199.5, 26.0], 
[331.0, 139.0, 15.5], 
[142.0, 50.5, 16.0]]

正如評論和 j1-lee 所建議的那樣,有更快的方法,但使用 for 循環的一種方法是:

import numpy as np

ll= [[1, 2, 3, 45, 32, 9],
    [5, 23, 5, 23, 42, 5],
    [7, 34, 3, 53, 60, 12],
    [324, 135, 323, 76, 1, 51],
    [89, 573, 65, 213, 23, 8],
    [231, 53, 84, 17, 31, 1]]

def get_avg(l):
    ret = []
    for i in range(0, len(l),2):
        ret.append((l[i]+ l[i+1])/2)
    return ret

avg_ll = [get_avg(l) for l in ll]

final = np.array(avg_ll)

print(final)

我不是 100% 確定我知道你在問什么。 這是產生三行的代碼,其中每一行是一對行的平均值:

>>> b = (a[0::2,:] + a[1::2]) / 2
>>> print(b)
[[  3.   12.5   4.   34.   37.    7. ]
 [165.5  84.5 163.   64.5  30.5  31.5]
 [160.  313.   74.5 115.   27.    4.5]]

這應該有效。

  1. 創建一個空數組,其行數與a相同但列數的一半。
  2. 循環遍歷這個新數組的列
  3. 新數組中的每一列是數組a的 2 個相鄰列的平均值。
import numpy as np

a = np.array([[1, 2, 3, 45, 32, 9],
              [5, 23, 5, 23, 42, 5],
              [7, 34, 3, 53, 60, 12],
              [324, 135, 323, 76, 1, 51],
              [89, 573, 65, 213, 23, 8],
              [231, 53, 84, 17, 31, 1]])


def avg_of_adj(a):
    avg_array = np.empty(shape=(a.shape[0],int(a.shape[1]/2)))
    for i in range(avg_array.shape[1]):
        avg_array[:,i] = (a[:,2*i] + a[:,2*i+1])/2
    return avg_array

print(avg_of_adj(a))

Output:

[[  1.5  24.   20.5]
 [ 14.   14.   23.5]
 [ 20.5  28.   36. ]
 [229.5 199.5  26. ]
 [331.  139.   15.5]
 [142.   50.5  16. ]]

您不需要for循環,只需重塑數組,並使用標准 NumPy 函數:

n_rows = a.shape[0]
print(a.reshape(-1, 2).mean(axis=1).reshape(n_rows, -1))

// [[  1.5  24.   20.5]
// [ 14.   14.   23.5]
// [ 20.5  28.   36. ]
// [229.5 199.5  26. ]
// [331.  139.   15.5]
// [142.   50.5  16. ]]

要了解程序背后的直覺,請觀察每個單獨的步驟:

  1. 按 2 分組值,(它依賴於數組中的列數是even的事實)
print(a.reshape(-1, 2))

[[  1   2]
 [  3  45]
 [ 32   9]
 [  5  23]
 [  5  23]
 [ 42   5]
 [  7  34]
 [  3  53]
 [ 60  12]
 [324 135]
 [323  76]
 [  1  51]
 [ 89 573]
 [ 65 213]
 [ 23   8]
 [231  53]
 [ 84  17]
 [ 31   1]]
  1. 計算重整數組列的平均值(即數字對)
print(a.reshape(-1, 2).mean(axis=1))

[  1.5  24.   20.5  14.   14.   23.5  20.5  28.   36.  229.5 199.5  26.
 331.  139.   15.5 142.   50.5  16. ]
  1. 我們有一個一維數組 - 將其重新整形為相同的行數
print(a.reshape(-1, 2).mean(axis=1).reshape(n_rows, -1))

[[  1.5  24.   20.5]
 [ 14.   14.   23.5]
 [ 20.5  28.   36. ]
 [229.5 199.5  26. ]
 [331.  139.   15.5]
 [142.   50.5  16. ]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM