Numpy is a math library for python. It enables us to do computation (between an array, matrix, tensor etc..) efficiently and effectively. In this article, I’m just going to introduce you to the basics of what is mostly required for Machine Learning and Data science (and Deep Learning !).

This is the part one of numpy tutorial series. For this section, I am going to focus on the creation of an array, the shape of the array and finally the method np.arrange().

Import the library

The first that you want to do before using numpy it is the importation of the package. By convention the importation is done by the following code:

import numpy as np

tips: If you want to check the version of your numpy you can try this line:

np.version.version

Create your first array

Let’s create our first array. For this example we create an array with the list of integer [0,1,2,3,4,5].

b = np.arange(6)
b,b.shape,b[0],type(b[0])
(array([0, 1, 2, 3, 4, 5]), (6,), 0, numpy.int64)

One thing which is vershape!tant when you manipulate an array: Check the shape ! In our case the shape of the array b is (6,) therefore it is an array with just one dimension with five elements. You can see that the first element is 0 in this case. If your goal it is not to create this kind of array it is possible to use the method reshape to change the shape of your array (when it is possible). I advise you to use this method in order to understand the elements that you manipulate.

d = np.arange(6).reshape(1,6)
d,d.shape,d[0],type(d[0])
(array([[0, 1, 2, 3, 4, 5]]), (1, 6), array([0, 1, 2, 3, 4, 5]), numpy.ndarray)

In this case we have an array with one dimension, each with six elements. Each elements are an integer.

Finally we are going to see the creation of an array with six dimensions, each with one element.

c = np.arange(6).reshape(6,1)
c,c.shape,c[0],type(c[0])
(array([[0],
        [1],
        [2],
        [3],
        [4],
        [5]]), (6, 1), array([0]), numpy.ndarray)

Method np.arrange()

To conclude this first introduction to numpy I would like to show few examples with the method np.arrange(). In a previous example I have just used one parameter but it is possible to use four parameters.

np.arange(0,15,3)
array([ 0,  3,  6,  9, 12])

You can personalize the elements created through the method np.arrange(). np.arange([start],stop,[step],dtype=None) does is that it arranges numbers from starting to stop, in steps of step. Here, it means for np.arange(0,15,3): return an np list starting from 0 all the way upto 15 but don’t include 15 and increment numbers by 3 each time.

Another example:

np.arange(2,29,5)
array([ 2,  7, 12, 17, 22, 27])

In the second part, we will focus on three others methods: np.zeros(), np.eye() and on the computation between two matrix.