yusuf yusuf - 1 month ago 16
Python Question

Reshaping a python numpy array in a custom way

I have such a python numpy array;

[[-0.17433028 -0.20116786 -0.17599097 -0.1907735 0.27599955 -0.16071874]
[-0.21809219 -0.20256139 -0.15900832 -0.18323743 -0.26910328 0.78731642]]


How can I reshape the array as the following?

[[-0.17433028, -0.21809219], [-0.20116786, -0.20256139], [-0.17599097, -0.15900832], [-0.1907735, -0.18323743], [0.27599955, -0.26910328], [-0.16071874, 0.78731642]]

Answer

You want to use the transpose method:

>>> arr = np.array([[-0.17433028, -0.20116786, -0.17599097, -0.1907735, 0.27599955, -0.16071874], [-0.21809219, -0.20256139, -0.15900832, -0.18323743, -0.26910328,  0.78731642]])

>>> arr.transpose()
array([[-0.17433028, -0.21809219],
       [-0.20116786, -0.20256139],
       [-0.17599097, -0.15900832],
       [-0.1907735 , -0.18323743],
       [ 0.27599955, -0.26910328],
       [-0.16071874,  0.78731642]])