In this tutorial, we will introduce NumPy which is a very important library when we use Python for Machine Learning.
Table of Contents:
- Description and the first use of Numpy library
- Defining special arrays with Numpy
- Defining arrays with special methods
- Special functions of the Numpy arrays
- Mathematical operations with Numpy arrays
We start by importing the Numpy library and defining an array with Numpy. Secondly, we define special arrays with Numpy and learn to define arrays with special functions. We mention some functions which are used very often when we work with Numpy arrays. After that, we refer to some mathematical functions used with Numpy arrays. Finally, we summarize the tutorial in the last section.
Description and the first use of Numpy library
NumPy is a library for the Python programming language. It supports fast computation for large multidimensional arrays and offers a wide collection of high-level mathematical functions to work on them. It is essential for Data science and used very often in Machine and Deep learning projects.
We first import the Numpy library with its very often used abbreviation “np” in the following line.
import numpy as np
Then we define a Numpy array: This is a one-dimensional array containing elements 1, 2, 3, 4, 5. We can see our array with the embedded “print” function in Python.
[1 2 3 4 5]
We define another array. This one is a 3×3, 2-dimensional array(matrix) that contains numbers from 1 to 9.
[[1 2 3] [4 5 6] [7 8 9]]
We can access specified elements of the Numpy arrays with indexes given in square brackets. The first element of the array has the index 0. For example, the element in the 3rd index of the “first_array” is printed below.
To reach the element in the second row and the second column of the “second_array” could be printed with the following line. We give [1, 1] indexes here because the first row and the first column have the index 0.
Defining special arrays with Numpy
In this section, we mention the zeros, ones, and eye functions in Numpy.
We can call the “zeros” function to define an array including 0 for all elements. For example, we can define a one-dimensional 5 element zeros array with the line below.
[0. 0. 0. 0. 0.]
Similarly, we can define a two-dimensional 2×2 zeros array with the following line.
[[0. 0.] [0. 0.]]
One’s function is similar to the zeros function. In the following line, we define a 3×3 array containing all elements 1.
[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]
Eye function creates an identity matrix (unit matrix) which involves 1’s in the diagonal and 0’s in other elements. We define a 3×3 unit matrix in the line below.
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Defining arrays with special methods
In this section, we mention arange, linspace, and random functions in Numpy.
We can create an array including the numbers in a range with the “arange” function. We give the lower bound and upper bound of the range for the two-parameter passing versions of the function. We define an array with the numbers between 0 and 10 in the following code line.
[0 1 2 3 4 5 6 7 8 9]
In the three-parameter passing version, we give the step size from one element to the next. We define an array between 0 and 15 with step size 3 below.
[ 0 3 6 9 12]
We define a linear spaced array with the “linspace” function. We give 3 parameters to this function: The first one is the lower bound of the array and the second is the upper bound. The third parameter is how many elements we want for the output array when we divide the range with equal intervals. For example, we define a 5-element linear spaced array between 0 and 100 in the following code line.
[ 0. 25. 50. 75. 100.]
“Random” is a special module in the Numpy library and has many different functions inside. First, we will show the “rand” function. We create a random array with the parameter which determines the number of elements. The array contains floating-point numbers between 0 and 1. In the following code, a 5 element array is created.
[0.20279302 0.59716957 0.94310218 0.03177052 0.88601822]
We can generate a Gaussian distributed(standard normal distribution) random array with the “random” function. For example, we can see the output of this function for a 5-element array in the following code.
[ 1.07442191 1.10276759 1.1536396 1.56249421 -0.14813009]
We can generate a random integer number between the parameters we give with the “randint” function. In the sample use, we generate a random number between 0 and 100 in the code line below.
We can pass the third parameter to generate a random array with the number of elements determined with this parameter. For example, we create a 5 element random array between 0 and 100 in the following line.
[64 32 62 27 30]
Special functions of the Numpy arrays
In this section, we will explain “sum”, “min”, “max”, “argmin”, “argmax”, “std”, “var” functions and the slicing operation of the Numpy arrays. These functions could be used with any type of Numpy arrays. First of all, we will create a random numpy array with 5 integer numbers between 0 and 100 as we mention in the previous section. We will use this array to show the special functions.
[64 32 62 27 30]
We calculate the sum of the elements in the array with the “sum” function in the following code line.
We can get the smallest element of the array with the “min” function.
We can access the biggest element of the array with the “max” function in the line below.
We can access the index of the smallest element with the “argmin” function.
Similarly, we can get the index of the biggest element with the “argmax” function.
We can calculate the standard deviation of the Numpy array with the “std” function.
“var” function calculates the variance of the array.
We can get a determined part of the array with a slicing operation. For example, we can take the first 3 elements of the random array in the following line.
[64 32 62]
We can take the elements between indexes 1 and 3 in the code line below.
We can also take the elements after the third index with the slicing operation given in the below code.
Mathematical operations with Numpy arrays
In this section, we will show addition, subtraction, multiplication, division, and some other mathematical functions with the Numpy arrays. First of all, we will create two Numpy arrays to show the functions.
array_1 = np.array([1,2,3,4,5]) array_2 = np.array([6,7,8,9,10])
The addition operation of the two arrays is shown in the following line.
print(array_1 + array_2)
[ 7 9 11 13 15]
Subtraction of the array_1 from array_2 is shown in the line below.
print(array_2 - array_1)
[5 5 5 5 5]
Multiplication of the two arrays could be calculated like the below.
print(array_1 * array_2)
[ 6 14 24 36 50]
We can divide the array_2 to array_1 like the following.
print(array_2 / array_1)
[6. 3.5 2.66666667 2.25 2. ]
We can calculate the element-wise exponentiation with two stars.
print(array_1 ** array_2)
[ 1 128 6561 262144 9765625]
Numpy is an essential library in Python for Machine Learning. In this tutorial, we explain some base functions of this library. We give information about creating Numpy arrays and special functions/operations with them.