Accessing and changing the shape

The number of dimensions is what distinguishes a vector from a matrix. The shape is what distinguishes vectors of different sizes, or matrices of different sizes. In this section, we examine how to obtain and change the shape of an array.

The shape function

The shape of a matrix is the tuple of its dimensions. The shape of an n × m matrix is the tuple (n, m). It can be obtained by the shape function:

M = identity(3)
shape(M) # (3, 3)

For a vector, the shape is a singleton containing the length of that vector:

v = array([1., 2., 1., 4.])
shape(v) # (4,) <- singleton (1-tuple)

An alternative is to use the array attribute shape, which gives  the same result:

M = array([[1.,2.]])
shape(M) # (1,2)
M.shape # (1,2)

However, the advantage of using  shape as a function is that this function may be used on scalars and lists as well. This may come in handy when code is supposed to work with both scalars and arrays:

shape(1.) # ()
shape([1,2]) # (2,)
shape([[1,2]]) # (1,2)

Number of dimensions

The number of dimensions of an array is obtained with the function numpy.ndim  or using the array attribute ndarray.ndim :

ndim(A) # 2
A.ndim # 2

Note that the number of dimensions, given by the function ndim, of a tensor T (a vector, matrix, or higher order tensor) is always equal to the length of its shape:

T = zeros((2,2,3)) # tensor of shape (2,2,3); three dimensions
ndim(T) # 3
len(shape(T)) # 3

Reshape

The method reshape gives a new view of the array, with a new shape, without copying the data:

v = array([0,1,2,3,4,5])
M = v.reshape(2,3)
shape(M) # returns (2,3)
M[0,0] = 10 # now v[0] is 10

Tip

Reshape does not copy

Reshape does not create a new array. It rather gives a new view on the existing array. In the preceding example, changing one element of M would automatically result in a change in the corresponding element in v. When this behavior is not acceptable, you need to copy the data.

The various effects of the reshape method on an array defined by arange(6) are given in the following figure :

Reshape

Figure 4.2: The various effects of the reshape method on an array defined by arange(6)

If one tries to reshape an array with a shape that does not multiply to the original shape, an error is raised:

 ValueError: total size of new array must be unchanged.

Sometimes, it is convenient to specify only one shape parameter and let Python determine the other in such a way that it multiplies to the original shape. This is done by setting the free shape parameter -1:

v = array([1, 2, 3, 4, 5, 6, 7, 8])
M = v.reshape(2, -1)
shape(M) # returns (2, 4)
M = v.reshape(-1, 2)
shape(M) # returns (4,2 )
M = v.reshape(3,- 1) # returns error

Transpose

A special form of reshaping is transposing. It just switches the two shape elements of the matrix. The transpose of a matrix A is a matrix B such that:

Transpose

Which is resolved in the following way:

A = ...
shape(A) # 3,4

B = A.T # A transpose
shape(B) # 4,3

Tip

Transpose does not copy

Transposition is very similar to reshaping. In particular, it does not copy the data either and just returns a view on the same array:

A= array([[ 1., 2.],[ 3., 4.]])
B=A.T
A[1,1]=5.
B[1,1] # 5

Transposing a vector makes no sense since vectors are tensors of one dimension, that is, functions of one variable. NumPy will, however, comply and return exactly the same object:

v = array([1., 2., 3.])
v.T # exactly the same vector!

What you have in mind when you want to transpose a vector is probably to create a row or column matrix. This is done using reshape:

v.reshape(-1, 1) # column matrix containing v
v.reshape(1, -1) # row matrix containing v
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45