Description and The First Use of Numpy Library
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
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])
—————————————–
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])
—————————————–
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.]]
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)
—————————————-
random_float=np.random.rand(5)
print(random_float)
—————————————-
random_gaussian=np.random.randn(5)
print(random_gaussian)
—————————————-
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())
—————————————-
Similarly, we can get the index of the biggest element with the “argmax” function.
print(random_array.argmax())
—————————————-
We can calculate the standard deviation of the Numpy array with the “std” function.
print(random_array.std())
—————————————-
16.419500601419035
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]
print(array_2 - array_1)
—————————————
[5 5 5 5 5]
print(array_1 * array_2)
—————————————
[ 6 14 24 36 50]
print(array_2 / array_1)
—————————————
print(array_1 ** array_2)
—————————————