Convert a 1D array to a 2D array in numpy

442,112

Solution 1

You want to reshape the array.

B = np.reshape(A, (-1, 2))

where -1 infers the size of the new dimension from the size of the input array.

Solution 2

You have two options:

  • If you no longer want the original shape, the easiest is just to assign a new shape to the array

    a.shape = (a.size//ncols, ncols)
    

    You can switch the a.size//ncols by -1 to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size, else you'll run into some problem.

  • You can get a new array with the np.reshape function, that works mostly like the version presented above

    new = np.reshape(a, (-1, ncols))
    

    When it's possible, new will be just a view of the initial array a, meaning that the data are shared. In some cases, though, new array will be acopy instead. Note that np.reshape also accepts an optional keyword order that lets you switch from row-major C order to column-major Fortran order. np.reshape is the function version of the a.reshape method.

If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as

>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)

Solution 3

Try something like:

B = np.reshape(A,(-1,ncols))

You'll need to make sure that you can divide the number of elements in your array by ncols though. You can also play with the order in which the numbers are pulled into B using the order keyword.

Solution 4

If your sole purpose is to convert a 1d array X to a 2d array just do:

X = np.reshape(X,(1, X.size))

Solution 5

convert a 1-dimensional array into a 2-dimensional array by adding new axis.

a=np.array([10,20,30,40,50,60])

b=a[:,np.newaxis]--it will convert it to two dimension.
Share:
442,112

Related videos on Youtube

ahwillia
Author by

ahwillia

Updated on July 08, 2022

Comments

  • ahwillia
    ahwillia almost 2 years

    I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this:

    > import numpy as np
    > A = np.array([1,2,3,4,5,6])
    > B = vec2matrix(A,ncol=2)
    > B
    array([[1, 2],
           [3, 4],
           [5, 6]])
    

    Does numpy have a function that works like my made-up function "vec2matrix"? (I understand that you can index a 1D array like a 2D array, but that isn't an option in the code I have - I need to make this conversion.)

  • Jamie F
    Jamie F over 4 years
    AttributeError: module 'numpy' has no attribute 'flatten'
  • Sid
    Sid over 4 years
    It's better that you include some explanation along with code.
  • StupidWolf
    StupidWolf over 4 years
    Can you explain how your answer now is different from the previous and also the other answers above, that also uses np.reshape?
  • Rafi
    Rafi over 4 years
    Could you share your code? Because the numpy sure does have flatten method: docs.scipy.org/doc/numpy/reference/generated/…
  • Kermit
    Kermit over 3 years
    A = A.reshape(A.shape[0], 1)