12

If I create an array X = np.random.rand(D, 1) it has shape (3,1):

[[ 0.31215124]
 [ 0.84270715]
 [ 0.41846041]]

If I create my own array A = np.array([0,1,2]) then it has shape (1,3) and looks like

[0 1 2]

How can I force the shape (3, 1) on my array A?

1
  • Sorry are you looking for A.reshape([3,1])?
    – EdChum
    Jun 5, 2015 at 13:51

5 Answers 5

7

You can assign a shape tuple directly to numpy.ndarray.shape.

A.shape = (3,1)

As of 2022, the docs state:

Setting arr.shape is discouraged and may be deprecated in the future. Using ndarray.reshape is the preferred approach.

The current best solution would be

A = np.reshape(A, (3,1))
4
  • 1
    Maybe someone is being picky about your use of 'function'. np.reshape is a function, A.reshape a method, and A.shape= a functionality? They all do the same job.
    – hpaulj
    Jun 5, 2015 at 15:35
  • Setting shape will sometimes raise an error, it's not guaranteed to work in all cases. Just adding info, I'm not the down vote.
    – Bi Rico
    Jun 5, 2015 at 21:51
  • The np.reshape docs says: If you want an error to be raise if the data is copied, you should assign the new shape to the shape attribute of the array. The fact that a.shape= does not always work may be a good thing. Do you have any other cases in mind?
    – hpaulj
    Jun 6, 2015 at 2:01
  • docs.scipy.org/doc/numpy/reference/generated/… does not give any warnings about when .shape= would be wrong.
    – hpaulj
    Jun 6, 2015 at 2:07
6
A=np.array([0,1,2])
A.shape=(3,1)

or

A=np.array([0,1,2]).reshape((3,1))  #reshape takes the tuple shape as input
1

The numpy module has a reshape function and the ndarray has a reshape method, either of these should work to create an array with the shape you want:

import numpy as np
A = np.reshape([1, 2, 3, 4], (4, 1))
# Now change the shape to (2, 2)
A = A.reshape(2, 2)

Numpy will check that the size of the array does not change, ie prod(old_shape) == prod(new_shape). Because of this relation, you're allowed to replace one of the values in shape with -1 and numpy will figure it out for you:

A = A.reshape([1, 2, 3, 4], (-1, 1))
0

You can set the shape directy i.e.

A.shape = (3L, 1L)

or you can use the resize function:

A.resize((3L, 1L))

or during creation with reshape

A = np.array([0,1,2]).reshape((3L, 1L))
1
  • Both assignment to shape and resize should be avoided unless you know what you're doing because they both have behaviour that will likely surprise new users.
    – Bi Rico
    Jun 5, 2015 at 22:03
0

Your 1-D array has the shape (3,):

>>>A = np.array([0,1,2]) # create 1-D array
>>>print(A.shape) # print array shape
(3,)

If you create an array with shape (1,3), you can use the numpy.reshape mentioned in other answers or numpy.swapaxes:

>>>A = np.array([[0,1,2]]) # create 2-D array
>>>print(A.shape) # print array shape
>>>A = np.swapaxes(A,0,1) # swap 0th and 1st axes
>>>A # display array with swapped axes
(1, 3)
array([[0],
       [1],
       [2]])

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.