Array Shape Manipulation

import numpy as np

1. Flattening

a = np.array([("Germany","France", "Hungary","Austria"),
              ("Berlin","Paris", "Budapest","Vienna" )]) 
a
array([['Germany', 'France', 'Hungary', 'Austria'],
       ['Berlin', 'Paris', 'Budapest', 'Vienna']], dtype='<U8')
a.shape
(2, 4)

The ravel() function

The primary functional difference is that flatten is a method of an ndarray object and hence can only be called for true numpy arrays. In contrast ravel() is a library-level function and hence can be called on any object that can successfully be parsed. For example ravel() will work on a list of ndarrays, while flatten will not.

a.ravel()
array(['Germany', 'France', 'Hungary', 'Austria', 'Berlin', 'Paris',
       'Budapest', 'Vienna'], dtype='<U8')

T gives transpose of an array

a.T   
array([['Germany', 'Berlin'],
       ['France', 'Paris'],
       ['Hungary', 'Budapest'],
       ['Austria', 'Vienna']], dtype='<U8')
a.T.ravel()
array(['Germany', 'Berlin', 'France', 'Paris', 'Hungary', 'Budapest',
       'Austria', 'Vienna'], dtype='<U8')

2. Reshaping

reshape() gives a new shape to an array without changing its data.

a.shape
(2, 4)
a.reshape(4,2)
array([['Germany', 'France'],
       ['Hungary', 'Austria'],
       ['Berlin', 'Paris'],
       ['Budapest', 'Vienna']], dtype='<U8')
np.arange(15).reshape(3,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
np.arange(15).reshape(5,3)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

The reshape() dimensions needs to match the number of values in the array

Reshaping a 15-element array to an 18-element one will throw an error

np.arange(15).reshape(3,6)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-5fb62e91be13> in <module>()
----> 1 np.arange(15).reshape(3,6)

ValueError: cannot reshape array of size 15 into shape (3,6)

Specify only one dimension (and infer the others) when reshaping

Another way we can reshape is by metioning only one dimension, and -1. -1 means that the length in that dimension is inferred

countries = np.array(["Germany","France", "Hungary","Austria","Italy","Denmark"])
countries
array(['Germany', 'France', 'Hungary', 'Austria', 'Italy', 'Denmark'],
      dtype='<U7')

Here the unspecified value is inferred to be 2

countries.reshape(-1,3) 
array([['Germany', 'France', 'Hungary'],
       ['Austria', 'Italy', 'Denmark']], dtype='<U7')

Here the unspecified value is inferred to be 3

countries.reshape(3,-1) 
array([['Germany', 'France'],
       ['Hungary', 'Austria'],
       ['Italy', 'Denmark']], dtype='<U7')

If the values of the dimensions are not factors of the number of elements, there will be an error

countries.reshape(4,-1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-a4451166d337> in <module>()
----> 1 countries.reshape(4,-1)

ValueError: cannot reshape array of size 6 into shape (4,newaxis)