python - Reshape a numpy array -
although have tried np.reshape , transpose, not getting desired output.
i have numpy array looks this:
>>> = np.array([[1, 1, 2, 2],[1, 1, 2, 2],[1, 1, 2,2]]) >>> array([[1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]]) i want following:
>>> b = np.array([[1, 2],[1,2],[1,2],[1, 2],[1,2],[1,2]]) >>> b array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]) the best can in 4 lines of code:
e, f = np.hsplit(a,2) e = e.reshape(np.size(e)) f = f.reshape(np.size(f)) b = np.vstack((e,f)).t can provide me pythonic way reshape array in manner?
it not clear looking using 1s , 2s in example. please use letters or unique numbers mapping clearer.
i able jiggling make work i'm not 100% if mapping want. see steps below:
>>> = np.array([[1, 1, 2, 2],[1, 1, 2, 2],[1, 1, 2,2]]) array([[1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]]) >>> a.reshape(6,2) array([[1, 1], [2, 2], [1, 1], [2, 2], [1, 1], [2, 2]]) >>> a.reshape(6,2).transpose() array([[1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2]]) >>> a.reshape(6,2).transpose().reshape(6,2) array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]])
Comments
Post a Comment