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, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
The reshape()
method takes the new shape as parameter. The total number of elements must be unchanged. For example, reshaping a (2, 3)
array to a (5,)
array would raise an error. The number ...