Font size
  • A-
  • A
  • A+
Site color
  • R
  • A
  • A
  • A
Skip to main content
AI4VET AI4VET
  • Home
  • Calendar
  • More
You are currently using guest access
Log in
AI4VET
Home Calendar
Expand all Collapse all
  1. AI/ML Fundamentals
  2. AIML-EN
  3. 1. Artificial Intelligence (EN)
  4. Intro Exercise: NumPy, Matplotlib and Pandas libraries

Intro Exercise: NumPy, Matplotlib and Pandas libraries

To get an idea of how machine learning algorithms work and to get a better idea of some important concepts, we will use the libraries of the Python programming language, namely NumPy, Matplotlib and Pandas. The goal here is certainly not to explore them in detail (all three libraries are extensive and offer a lot of possibilities), but to get to know some basic objects and functions that can help us.

NumPy Library

 colab

 All the data we have should be presented using numbers and some schemes that combine them. So, for example, the outside temperature can be represented by a real number (also called a scalar), while a series of numbers can be represented by the external temperature for the whole week (the length of that sequence is seven). With a block of numbers, the so-called matrices, we can represent the pixels of a black and white image. If the size of the image is 200x300 pixels, this block has 200 rows and 300 columns, and at the intersection of each type and column there is one number that indicates the value of the pixel. That is why we say that such structures are two-dimensional. If the image is in color and uses RGB (eng. Red Green Blue) color system (we will talk about this later in the section on convolutional neural networks), we actually have three blocks of numbers measuring 200x300 (one for each of the colors) which can also be written as 200x300x3. Can you imagine how we present the video? He saw nothing but a series of frames (images) in time. The usual 24 frames per second allow us to experience the video as natural, without interruptions or chopping. This would further mean that one second of video can be described as 24x200x300x3. Such structures are four-dimensional. It is common for structures that have multiple dimensions to be called tensors. In fact, a scalar is a tensor of dimension zero, a string is a tensor of dimension one, and a matrix is a tensor of dimension two. Hence the name of the popular TensorFlow library, which is used in machine learning.

Tensors of different dimensions

The NumPy library is an open source library that allows us to quickly perform many mathematical operations on the data presented in this way. Although the set of functionalities of this library is very rich, we will learn how to:

  • Let's define arrays and matrices of numbers.
  • Let's take a look at their elements, types, and columns.
  • And we will be able to add and multiply them.
  • Let's find a mathematical function, and
  • We're going to get some random values.

 This section is paired with the Jupyter volume B-numpy.ipynb. To be able to follow the content further, click on the button colabto open the content in the Google Colab environment.If you're viewing the notebooks on your local machine, find the notebook with the same name in the table of contents and run it. For more detailed instructions, see the Hands-on Zone section and the Jupyter Exercise Notebook lesson.

 At the very beginning of the notebook, you will be greeted by the import numpy as np command, which we need to execute in order to be able to use the NumPy library.

 As you saw in the introduction, in order to work with real data, we need multidimensional arrays. The basic structure of the NumPy library is a multidimensional array. multidimensional array, ndarray)tag. It is characterized by a shape that indicates the dimensions of a multidimensional array and the elements it contains. A function that creates a multidimensional array is called an array. The next block of code creates a matrix M with dimensions 3x2, i.e. A matrix that has three rows and two columns.

As we have seen, we encounter such blocks when representing images, but also tabular data - individual columns indicate attributes, and instance types indicate sets.

The number of columns and the number of types of a multidimensional array M can be read by the shape property, so the following line of code results in a pair of numbers (3, 2):

M.shape

 Multidimensional arrays must contain values of the same type - they can be integers or real numbers. The library also allows the use of numbers with single and double precision, but we won't go into those details. You can always read the element type of a multidimensional array by using the dtype property. Since our matrix contains only integers, the following command will print int64 :

M.dtipe

Here are some examples of creating multidimensional arrays:

  • A sequence of single-digit numbers: np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  • A 1x3 matrix containing the numbers 10, 11, and 12: np.array([[10, 11, 12]])
  • A 3x1 matrix containing the numbers 10, 11, and 12: np.array([[10], [11], [12]])

The individual elements of the array are approached using the appropriate indices - we use as many indices as we have dimensions and make sure that the indices start from zero. Thus, M[0,0] reads the value in the zero row and the zero column, while M[2,1] reads the value in the second row and the first column.

 Just like with lists, the NumPy library can use the snipping operator. So, for example, in matrix A of dimension 5x5 shown in the figure below:

  • A [4 , :] distinguishes all the elements of the last kind, i.e. The Yellow Block,
  • A [ :, 1::2] is extracted by the elements of every other column, i.e. The red blocks.
  • A [1::2 , 0:3:2] are distinguished by the elements of the blue block.

Addition and subtraction operations on multidimensional arrays are performed element by element. Subtract the elements of arrays that are in the same positions, and as a result, an array of the same dimensions is obtained. The operators of these actions are, as you would expect, + and -, and the add and subtract functions can also be used.

The following block of code sums two matrices: A =[1357911131517] and B =[24681012141618]:

A = np.array([
[1, 3, 5],
[7, 9, 11],
[13, 15, 17]
])
B = np.array([
[2, 4, 6],
[8, 10, 12],
[14, 16, 18]
])
A + B

The result is a matrix [3711151923273135].

When it comes to multiplication, it is possible to multiply arrays by scalars, in which case each element of the array is multiplied by a scalar. This operation is denoted by *. Thus, the bottom block of code for matrix A from the previous example yields the matrix [3915212733394551].

3 * A

To perform true matrix multiplication, the dot function is used. Matrix A and B are given a matrix [96114132240294348384474564].

A.dot(B)

When the mathematical functions of the NumPy library are applied to multidimensional arrays, they are applied to each of its elements. For example, by executing the following code

npr.exp(M)

applying the exponential function to the elements of the matrix M =[123456], the matrix M =[2.718281837.389056120.0855369254.59815003148.4131591403.42879349].

It is also possible to execute a function only along some dimension of a multidimensional array, for example, only by columns or only by types. Of course, this only makes sense for some functions such as finding maximums, minimums, additions or averages. The following code first adds the elements of the matrix M by type, and then by columns. As a result, [3,7,11] and [9,12] are obtained.

# sum by rows
np.sum(M, axis=1)
# sum by columns
np.sum(M, axis=0)

In our work, it often means that we quickly generate arrays with some random values, or vectors of zeros or ones. The following calls will be generated, in order:

  • 2x3 random array array: np.random.random((2, 3))
  • Matrix Zero Dimension 4k4: np.zeros((4, 4))
  • 4x2 unit matrix: np.ones((4, 2))
  • A one-dimensional array with an equidistant set of 9 points from the interval 0 to 2: np.linspace(0, 2, 9)

 

You can find out more about the contents and capabilities of the NumPy library on the official website of http://www.numpy.org/.

Matplotlib Library

Open In Colab

 This section is paired with the Jupyter volume A-matplotlib.ipynb. To continue following the content, click on the link and then on the button to open the content in the Google Colab environment.If you're viewing the notebooks on your local machine, find the notebook with the same name in the table of contents and run it. For more detailed instructions, see the Hands-on Zone section and the Jupyter Exercise Notebook lesson.

 Matplotlib is a Python library used for both 2D and 3D graphics. Graphical representations are very useful when working with data because they allow us to better understand the data, as well as to better monitor some of the behavior of algorithms. Let's get to know the functionality of this library through two simple examples: plotting a graph of the sin(x) function and displaying a dot graph of a data set.

Typically, codes that use the Matplotlib library start with the import matplotlib.pyplot as plt command, which loads the plt plotting panel and its functions.

An example of plotting the graphs of the sin(x) function on the interval [0, 10] will begin by creating an equidistant grid of points x by calling the NumPy library linspace function. This function expects the ends of the intervals 0 and 10 and the number of milestones as arguments - in our case, it can be 100. We will then calculate the value of the sine function for each of these points by calling the sin(x) function. We will store its values in the variable y - and it will now be a single array of 100 points because the function is applied to each element of the array x.

We set the title of the graph by calling the title function, and the axis markers (the texts that will explain their meaning) by calling the xlabel and ylabel functions. All these functions are defined at the level of the drawing panel plt. The graph itself is plotted by calling the plot function and giving the values of the x and y coordinates for drawing (that's why we named the initial set of points and values of the sine function with these names). The graph is displayed by calling the show function.

# creating a grid of points
x = np.linspace(0, 10, 100)
y = np.sin(x)
# setting the title of the graph and axis labels
plt.title('Graph of the function y=sin(x)')
plt.xlabel('x')
plt.ylabel('y')
# plotting the graph
plt.plot(x, y)
# displaying the graph
plt.show()

Scatter charts are often used to look at the spatial arrangement of data. In the following example, we will create ten pairs of points with integer values of coordinates from the interval [0, 20] and display them in the form of a scatter chart.

 To create a series of pairs of points, we will create an array of single coordinates x and y. We will do this by using the NumPy randomint function , whose low, high, and size arguments allow you to control the lower and upper bounds of the interval, as well as control the number of points. The scatter chart itself is created by calling the scatter panel function to draw plt. When this function is called, the values of the coordinates of the points, in our case x and y, are given. In addition, you can adjust the color of the dots with the color argument, as well as modify the symbol itself for display with the marker argument. Instead of the default black circles, the graphics use green triangles pointing downwards. The graph is displayed, as in the previous example, by calling the show function.

# arrays of length 10 with arbitrary elements from the interval [0, 20]
np.random.seed(7)
k = np.random.randint (nisko = 0, visoko = 20, veličina = 10)
i = np.random.randint (nisko = 0, visoko = 20, veličina = 10)
# generating scatter plot
plt.scatter(x, y, color='green', marker='v')
# displaying the graph
plt.show()

We can see that in this example, at the level of the NumPy library and its random package, we have set the random number generator (the so-called seed property) to a value of 7. This will allow us to get the same arrangement of points every time we run this code. This feature is important to us because of the ability to restart experiments and share codes. This property is called repeatability or reproducibility.

The official website of the Matplotlib library is https://matplotlib.org/ , and in addition to it, there are other Python libraries for visualizations, such as Seaborn and Plotly.

The Pandas library is designed to work with tabular data. It is characterized by functions for loading various file formats, and then numerous functions for manipulating data. The link to the official website of the library is https://pandas.pydata.org/ and we will get acquainted with its capabilities a little later, in the part with exploratory data analysis.

 

Completion requirements:
  • Make a submission
Previous activity Intro Exercise: Google Colab platform
Next activity Exercise 1.1: Application of Artificial Intelligence - Using AI Programs
You are currently using guest access (Log in)
Data retention summary
Get the mobile app
Get the mobile app
Play Store App Store
Powered by Moodle

This theme was proudly developed by

Conecti.me