numpy arrays
Steven D'Aprano
steve at pearwood.info
Wed Mar 23 06:32:24 EDT 2016
On Wed, 23 Mar 2016 09:06 pm, Heli wrote:
> Hi,
>
> I have a 2D numpy array like this:
>
> [[1,2,3,4],
> [1,2,3,4],
> [1,2,3,4]
> [1,2,3,4]]
>
> Is there any fast way to convert this array to
>
> [[1,1,1,1],
> [2,2,2,2]
> [3,3,3,3]
> [4,4,4,4]]
Mathematically, this is called the "transpose" of the array, and you can do
that in numpy:
py> import numpy as np
py> arr = np.array(
... [[1,2,3,4],
... [1,2,3,4],
... [1,2,3,4],
... [1,2,3,4]])
py> arr
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
py> arr.T
array([[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]])
--
Steven
More information about the Python-list
mailing list