How to change array shapes in numpy?

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

[[ 0.31215124]
 [ 0.84270715]
 [ 0.41846041]]

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

[0 1 2]

How to force a figure (3L, 1L)in my array A?

+4
source share
4 answers

numpy already has a "reshape" method, which numpy.ndarray.shape you can use to change the shape of your array.

A.shape = (3,1)
+2
source
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
+2
source

numpy reshape, ndarray reshape, :

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 , , .. prod(old_shape) == prod(new_shape). - -1, numpy :

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

directy i.e.

A.shape = (3L, 1L)

:

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

or during creation with a change of form

A = np.array([0,1,2]).reshape((3L, 1L))
0
source

All Articles