python - change the format of a numpy array with no loops -
i have numpy array shape a.shape = (1,k*d)
and want transform numpy array shape b.shape = (k*d,k)
in each column
b[i,j] = a[i] if j<i+1
b[i,j] = 0 if not
for example:
k = 3 d= 2 ********** = |a| => b = |a 0 0| |b| |b 0 0| |c| |0 c 0| |d| |0 d 0| |e| |0 0 e| |f| |0 0 f|
mainly, no loops!
what looking sequence of numpy-matrix operations lead desired result.
this reproduces example. can generalized other k
, d
in [12]: a=np.arange(6) in [13]: b=np.zeros((6,3)) in [14]: b[np.arange(6),np.arange(3).repeat(2)]=a in [15]: b out[15]: array([[ 0., 0., 0.], [ 1., 0., 0.], [ 0., 2., 0.], [ 0., 3., 0.], [ 0., 0., 4.], [ 0., 0., 5.]])
the key column indexing repeats necessary number of times
in [16]: np.arange(3).repeat(2) out[16]: array([0, 0, 1, 1, 2, 2])
Comments
Post a Comment