Basic array manipulations

Let's see some basic array manipulations around multiplication tables.

In [1]: import numpy as np

We first create an array of integers between 1 and 10, as shown here:

In [2]: x = np.arange(1, 11)
In [3]: x
Out[3]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Note that in np.arange(start, end), start is included while end is excluded.

To create our multiplication table, we first need to transform x into a row and column vector. Our vector x is a 1D array, whereas row and column vectors are 2D arrays (also known as matrices). There are many ways to transform a 1D array to a 2D array. We will see the two most common methods here.

The first method is to use reshape():

In [4]: x_row = x.reshape((1, -1))
        x_row
Out[4]: array([[ 1, ...

Get Learning IPython for Interactive Computing and Data Visualization - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.