看板 Python 關於我們 聯絡資訊
假設有個矩陣 [ 1, 2, 3, 4, 5, 6] [ 4, 5, 6, 7, 8, 9] [ 1, 2, 3, 4, 5, 6] [ 4, 5, 6, 7, 8, 9] 想要區域性2x2平均回填回去 [3, 3, 5, 5, 7, 7] [3, 3, 5, 5, 7, 7] [3, 3, 5, 5, 7, 7] [3, 3, 5, 5, 7, 7] 或是變成 [3, 5, 7] [3, 5, 7] 我只想到用迴圈解決 感覺沒發揮到 numpy 的威力 是否有用聰明的用法 謝謝! import numpy as np matrix1 = np.arange(0,200,1)[np.newaxis,:] matrix1 = np.reshape(matrix1,(20,10)) matrix2 = np.zeros((20,10)) for i in range(0,20,2): for j in range(0,10,2): print(matrix1[i, j]) temp = [matrix1[i,j],matrix1[i+1,j],matrix1[i,j+1],matrix1[i+1,j+1]] average = sum(temp)/len(temp) matrix2[i, j] = average matrix2[i+1, j] = average matrix2[i, j+1] = average matrix2[i+1, j+1] = average print(matrix2) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.94.219 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1514390044.A.C6C.html
KyotoAnime: 這不就是convolution嗎 12/28 00:13
Blankfein: 就是 12/28 05:45
Blankfein: 直接看tensorflow怎麼做的不是比來板上問快嗎XD 12/28 05:46
Blankfein: tf.nn.Convolution, 找method pool, pooling_type=“AV 12/28 05:50
Blankfein: G” 12/28 05:50
CaptainH: 就做個convolution也要TF? 12/28 12:40
elements: https://stackoverflow.com/q/42463172/2721007 換成 n 12/28 14:02
elements: p.mean 12/28 14:02
abc95007: 感謝樓上提供 這比較簡單XD 12/28 16:34