Introduction

In this tutorial, we will introduce NumPy which is a very important library when we use Python for Machine Learning.

Table of Contents:

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.

Numpy in Python

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.

first_array=np.array([1,2,3,4,5])
print(first_array)

—————————————–

[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.

second_array=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(second_array)

—————————————–

[[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.

print(first_array[3])

—————————————–

4

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.

print(second_array[1,1])

—————————————–

5

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.

zeros_array=np.zeros(5)
print(zeros_array)

—————————————–

[0. 0. 0. 0. 0.]

 

Similarly, we can define a two-dimensional 2×2 zeros array with the following line.

zeros_matrix=np.zeros((2,2))
print(zeros_matrix)

—————————————–

[[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.

ones_matrix=np.ones((3,3))
print(ones_matrix)

—————————————–

[[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.

unit_matrix=np.eye(3)
print(unit_matrix)

—————————————–

[[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.

range_array=np.arange(0,10)
print(range_array)

—————————————–

[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.

step_array=np.arange(0,15,3)
print(step_array)

—————————————-

[ 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.

linear_array=np.linspace(0,100,5)
print(linear_array)

—————————————-

[  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.

random_float=np.random.rand(5)
print(random_float)

—————————————-

[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.

random_gaussian=np.random.randn(5)
print(random_gaussian)

—————————————-

[ 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.

random_number=np.random.randint(0,100)
print(random_number)

—————————————-

25

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.

random_array=np.random.randint(0,100,5)
print(random_array)

—————————————-

[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.

random_array=np.random.randint(0,100,5)
print(random_array)

—————————————-

[64 32 62 27 30]

We calculate the sum of the elements in the array with the “sum” function in the following code line.

print(random_array.sum())

—————————————-

215

We can get the smallest element of the array with the “min” function.

print(random_array.min())

—————————————-

27

We can access the biggest element of the array with the “max” function in the line below.

print(random_array.max())

—————————————-

64

We can access the index of the smallest element with the “argmin” function.

print(random_array.argmin())

—————————————-

3

Similarly, we can get the index of the biggest element with the “argmax” function.

print(random_array.argmax())

—————————————-

0

We can calculate the standard deviation of the Numpy array with the “std” function.

print(random_array.std())

—————————————-

16.419500601419035

“var” function calculates the variance of the array.

print(random_array.var())

—————————————-

269.6

 

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.

print(random_array[:3])

—————————————-

[64 32 62]

We can take the elements between indexes 1 and 3 in the code line below.

print(random_array[1:3])

—————————————-

[32 62]

We can also take the elements after the third index with the slicing operation given in the below code.

print(random_array[3:])

—————————————-

[27 30]

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]

Summary

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.