[問題] numpy 區域合併?

看板Python作者 (別理我)時間8年前 (2017/12/27 23:54), 編輯推噓4(405)
留言9則, 5人參與, 8年前最新討論串1/1
假設有個矩陣 [ 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

12/28 00:13, 8年前 , 1F
這不就是convolution嗎
12/28 00:13, 1F

12/28 05:45, 8年前 , 2F
就是
12/28 05:45, 2F

12/28 05:46, 8年前 , 3F
直接看tensorflow怎麼做的不是比來板上問快嗎XD
12/28 05:46, 3F

12/28 05:50, 8年前 , 4F
tf.nn.Convolution, 找method pool, pooling_type=“AV
12/28 05:50, 4F

12/28 05:50, 8年前 , 5F
G”
12/28 05:50, 5F

12/28 12:40, 8年前 , 6F
就做個convolution也要TF?
12/28 12:40, 6F

12/28 14:02, 8年前 , 7F

12/28 14:02, 8年前 , 8F
p.mean
12/28 14:02, 8F

12/28 16:34, 8年前 , 9F
感謝樓上提供 這比較簡單XD
12/28 16:34, 9F
文章代碼(AID): #1QGy8Sni (Python)