The final part of the introduction to Numpy. In this second part, we are going to see a few functions in order to create a specific array. Then we are going to see the computation between two arrays. The first part of Numpy you can find here.

Array with np.zeros()

Firstly with the function np.zeros() it is possible to create an array (like np.arrange()) with all elements at zeros. It is particularly interesting if you want to initialize an array.

a = np.zeros(6)
a,a.shape,a[0],type(a[0])
(array([0., 0., 0., 0., 0., 0.]), (6,), 0.0, numpy.float64)

You can remark that in our example the element of the array are float. If you prefer to manipulate integer there is a parameter to change that.

a = np.zeros(6,dtype=int)
a,a.shape,a[0],type(a[0])
(array([0, 0, 0, 0, 0, 0]), (6,), 0, numpy.int64)

Compared by the functions np.arrange() where we have used the .reshape() to change the shape of the array with the function np.zeros() it is not necessary. If you want to create an array with a specific shape you can put the shape directly through the function. With the aim of understanding what you manipulate I advise you to put either the dimension (like the following code) or to use the function reshape.

np.zeros((1,6),dtype=int)
array([[0, 0, 0, 0, 0, 0]])

Computation between array

The most important aspect of Numpy arrays is that they are optimized for speed. The computation between two or more arrays are very efficient and easy to implement. We are going to implement a few computations and check the results. In the first example we are going to focus on the addition.

a = np.arange(6).reshape(1,6) 
b = np.arange(6).reshape(1,6)
c = a+b
c,c.shape,c[0],type(c[0])
(array([[ 0,  2,  4,  6,  8, 10]]),
 (1, 6),
 array([ 0,  2,  4,  6,  8, 10]),
 numpy.ndarray)

We can see that the result of the addition is an array with one dimension and the elements added. The dimension of the result stay the same that the inputs. It is not the more interesting example but it is the basis. Another example with the symmetric dimension.

a = np.arange(6).reshape(6,1) 
b = np.arange(6).reshape(6,1)
c = a+b
c,c.shape,c[0],type(c[0])
(array([[ 0],
        [ 2],
        [ 4],
        [ 6],
        [ 8],
        [10]]), (6, 1), array([0]), numpy.ndarray)

What’s happening when we multiply an array with the dimension (1,6)*(1,6)?

a = np.arange(6).reshape(1,6) 
b = np.arange(6).reshape(1,6)
c = a*b
c,c.shape,c[0],type(c[0])
(array([[ 0,  1,  4,  9, 16, 25]]),
 (1, 6),
 array([ 0,  1,  4,  9, 16, 25]),
 numpy.ndarray)

The result of our first multiplication is another array with the same dimensions that the inputs. But with each elements multiply by itself. We can start the more interesting example with the multiplication between an array with a dimension (6,1) and an array with a dimension (1,6).

a = np.arange(6).reshape(6,1) 
b = np.arange(6).reshape(1,6)
c = a*b
c,c.shape,c[0],type(c[0])
(array([[ 0,  0,  0,  0,  0,  0],
        [ 0,  1,  2,  3,  4,  5],
        [ 0,  2,  4,  6,  8, 10],
        [ 0,  3,  6,  9, 12, 15],
        [ 0,  4,  8, 12, 16, 20],
        [ 0,  5, 10, 15, 20, 25]]),
 (6, 6),
 array([0, 0, 0, 0, 0, 0]),
 numpy.ndarray)

The output of a multiplication is a new array with the following dimension (6,6). It is interesting to see the value of each elements of the new array. We can see that the first element of the new array correspond to the element of the array b multiply by the first element of the array a and so on… (ie: It is possible to have the same result with np.dot(a,b) or np.matmul(a,b)).

Matrix with np.eye()

Another function interesting for the creation of a specific kind of array. Sometimes, it is useful to create an identity matrix. The function np.eye() can help us to do that.

np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

With this line of code we have created an identity array with the dimension (3,3). There is a parameter which allows you to move the beginning of the diagonal of one. Another example with this parameter.

m = np.eye(3,k=1)
m
array([[0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])

Function np.sum()

Finally I would like to introduce a function which allows you to sum by different ways the elements of an array. In several cases you must return the sum along columns or along rows. Fortunately there is a function to do that easily. We are going to see how do that with a few examples. The first example show the sum of all elements.

np.sum(m,axis = None)
3.0

The axis at “None” involves the addition of all elements. The parameter axis at “0” involves the sum along columns whereas the value “1” involves the sum along rows.

np.sum(m,axis = 0)
array([1., 1., 1.])

There are several functions that you have to know through the library Numpy. This tutorial is not a comprehensive list of the functions that you have to have in your toolbox. I encourage you to try others functions that you can find throughout the following link: Numpy