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
?
If you have an array of shape (2,4) then reshaping it with (-1, 1), then the array will get reshaped in such a way that the resulting array has only 1 column and this is only possible by having 8 rows, hence, (8,1).
Reshape. There are different ways to change the dimension of an array. Reshape function is commonly used to modify the shape and thus the dimension of an array.
Use the correct NumPy syntax to check the shape of an array. arr = np.array([1, 2, 3, 4, 5]) print(arr. )
Use `.reshape()` to make a copy with the desired shape. You can think of reshaping as first raveling the array (using the given index order), then inserting the elements from the raveled array into the new array using the same kind of index ordering as was used for the raveling.
You ou can assign a shape tuple directly to numpy.ndarray.shape.
A.shape = (3,1)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With