import numpy as np
temp_array = np.array(
[[33.5, 35.3, 33.6, 33.6, 33.5, 33.9, 32.3, 33.2, 53.8, 54.6, 53.4, 54.2],
[33.1, 34.2, 34.1, 34.3, 34.7, 31.3, 32.3, 33.4, 57.5, 55. , 53.5, 56.1],
[35.3, 35.4, 35.6, 32.6, 33.2, 34.3, 32.8, 33.1, 54.7, 55.4, 54.6, 55.1],
[34.2, 36.1, 33.5, 32.4, 32.1, 33.5, 34.5, 35. , 53.8, 56.9, 54.5, 54.7],
[33.4, 33.8, 36.2, 33. , 35. , 34.2, 33.8, 33.8, 55.7, 55.2, 56. , 54.5],
[34.3, 35.9, 34.4, 34.2, 53.5, 54.2, 55.7, 54. , 56.3, 54.4, 55.5, 53.8],
[34.7, 35.4, 34.7, 33.1, 53.6, 54.5, 54.4, 55.5, 54.7, 55.4, 55.1, 55.6],
[33.3, 34.3, 33.6, 33.1, 55.4, 55.7, 55.4, 55.4, 55.8, 55. , 55.3, 54.1],
[33.7, 33.5, 37. , 34.9, 57.6, 54.2, 54.9, 54.6, 56. , 55.7, 55.1, 55.9],
[34. , 35.1, 33.6, 34.5, 56.2, 55.3, 55.2, 54. , 54.1, 54.5, 54.4, 56. ]])
cell_shape = (5,4)
我想找到該部分的平均值,例如第一部分:
[33.5, 35.3, 33.6, 33.6]
[33.1, 34.2, 34.1, 34.3]
[35.3, 35.4, 35.6, 32.6]
[34.2, 36.1, 33.5, 32.4]
[33.4, 33.8, 36.2, 33. ]
第二節:
[33.5, 33.9, 32.3, 33.2]
[34.7, 31.3, 32.3, 33.4]
[33.2, 34.3, 32.8, 33.1]
[32.1, 33.5, 34.5, 35. ]
[35. , 34.2, 33.8, 33.8]
等等。
uj5u.com熱心網友回復:
sh = temp_array.shape
cell_shape = (5, 4)
for y in range(0, sh[0], cell_shape[0]):
for x in range(0, sh[1], cell_shape[1]):
print(np.mean(temp_array[y:y 5, x:x 4]))
印刷:
34.16
33.49499999999999
54.96
34.365
54.964999999999996
55.135000000000005
uj5u.com熱心網友回復:
for startx, endx in [ ( n * cell_shape[0], (n 1) * cell_shape[0] ) for n in range( temp_array.shape[0] // cell_shape[0] ) ]:
for starty, endy in [ ( n * cell_shape[1], (n 1) * cell_shape[1] ) for n in range( temp_array.shape[1] // cell_shape[1] ) ]:
np.average(temp_array[startx:endx, starty:endy])
使用格式為 [startx:endx, starty:endy] 的 np 切片
這僅列印陣列,但使其成為平均值只是將其更改為 np.mean 的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/355688.html
