标签: python numpy matrix vectorization reshape
以下代码中是否有for循环的快捷方式?
import numpy as np ff = np.zeros((3,3,4)) gg = np.zeros((36,1)) ff[:,:,0] = 1 ff[:,:,1] = 2 ff[:,:,2] = 3 ff[:,:,3] = 4 for ii in xrange(4): gg[ii*9:(ii+1)*9,0] = ff[:,:,ii].reshape((9,))
答案 0 :(得分:2)
您可以将transpose或swapaxes与reshaping -
transpose
swapaxes
reshaping
ff.transpose([2,1,0]).ravel()[:,None] ff.swapaxes(0,2).ravel()[:,None]
您可以将ravel替换为reshape -
ravel
reshape
ff.transpose([2,1,0]).reshape(-1,1) ff.swapaxes(0,2).reshape(-1,1)