-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Artificial Intelligence and Machine Learning Fundamentals
By :
In order to put the basic AI concepts into practice, we need a programming language that supports artificial intelligence. In this book, we have chosen Python. There are a few reasons why Python is such a good choice for AI:
Python is a multi-purpose language. It can be used to create desktop applications, database applications, mobile applications, as well as games. The network programming features of Python are also worth mentioning. Furthermore, Python is an excellent prototyping tool.
To understand the dominant nature of Python in machine learning, data science, and AI, we have to compare Python to other languages also used in these fields.
One of the main alternatives is R. The advantage of Python compared to R is that Python is more general purpose and more practical.
Compared to Java and C++, writing programs in Python is significantly faster. Python also provides a high degree of flexibility.
There are some languages that are similar in nature when it comes to flexibility and convenience: Ruby and JavaScript. Python has an advantage over these languages because of the AI ecosystem available for Python. In any field, open source, third-party library support vastly determines the success of that language. Python's third-party AI library support is excellent.
We already installed Anaconda in the preface. Anaconda will be our number one tool when it comes to experimenting with artificial intelligence.
This list is by far incomplete, as there are more than 700 libraries available in Anaconda. However, if you know these libraries, then you're off to a good start because you will be able to implement fundamental AI algorithms in Python.
Anaconda comes with packages, IDEs, data visualization libraries, and high-performance tools for parallel computing in one place. Anaconda hides configuration problems and the complexity of maintaining a stack for data science, machine learning, and artificial intelligence. This feature is especially useful in Windows, where version mismatches and configuration problems tend to arise the most.
Anaconda comes with the IPython console, where you can write code and comments in documentation style. When you experiment with AI features, the flow of your ideas resembles an interactive tutorial where you run each step of your code.
IDE stands for Integrated Development Environment. While a text editor provides some functionalities to highlight and format code, an IDE goes beyond the features of text editors by providing tools to automatically refactor, test, debug, package, run, and deploy code.
The list of libraries presented here is not complete as there are more than 700 available in Anaconda. However, these specific ones will get you off to a good start, because they will give you a good foundation to be able to implement fundamental AI algorithms in Python:
The NumPy library will play a major role in this book, so it is worth exploring it further.
After launching your IPython console, you can simply import NumPy as follows:
import numpy as np
Once NumPy has been imported, you can access it using its alias, np. NumPy contains the efficient implementation of some data structures such as vectors and matrices. Python does not come with a built-in array structure, so NumPy's array comes in handy. Let's see how we can define vectors and matrices:
np.array([1,3,5,7])
The output is as follows:
array([1, 3, 5, 7])
We can declare a matrix using the following syntax:
A = np.mat([[1,2],[3,3]]) A
The output is as follows:
matrix([[1, 2], [3, 3]])
The array method creates an array data structure, while mat creates a matrix.
We can perform many operations with matrices. These include addition, subtraction, and multiplication:
Addition in matrices:
A + A
The output is as follows:
matrix([[2, 4], [6, 6]])
Subtraction in matrices:
A - A
The output is as follows:
matrix([[0, 0], [0, 0]])
Multiplication in matrices:
A * A
The output is as follows:
matrix([[ 7, 8], [12, 15]])
Matrix addition and subtraction works cell by cell.
Matrix multiplication works according to linear algebra rules. To calculate matrix multiplication manually, you have to align the two matrices, as follows:
To get the (i,j)th element of the matrix, you compute the dot (scalar) product on the ith row of the matrix with the jth column. The scalar product of two vectors is the sum of the product of their corresponding coordinates.
Another frequent matrix operation is the determinant of the matrix. The determinant is a number associated with square matrices. Calculating the determinant using NumPy is easy:
np.linalg.det( A )
The output is -3.0000000000000004.
Technically, the determinant can be calculated as 1*3 – 2*3 = -3. Notice that NumPy calculates the determinant using floating-point arithmetic, and therefore, the accuracy of the result is not perfect. The error is due to the way floating-points are represented in most programming languages.
We can also transpose a matrix, like so:
np.matrix.transpose(A)
The output is as follows:
matrix([[1, 3], [2, 3]])
When calculating the transpose of a matrix, we flip its values over its main diagonal.
NumPy has many other important features, and therefore, we will use it in most of the chapters in this book.
We will be using IPython and the following matrix to solve this exercise. We will start by understanding the NumPy syntax:
Using NumPy, calculate the following:
Let's begin with NumPy matrix operations:
import numpy as np
A = np.mat([[1,2,3],[4,5,6],[7,8,9]])
Notice the np.mat construct. If you have created an np.array instead of np.mat, the solution for the array multiplication will be incorrect.
A * A
The output is as follows:
matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]])
As you can see from the following code, the square of A has been calculated by performing matrix multiplication. For instance, the top-left element of the matrix is calculated as follows:
1 * 1 + 2 * 4 + 3 * 7
The output is 30.
np.linalg.det to calculate the determinant of the matrix:np.linalg.det( A )
The output is -9.51619735392994e-16.
The determinant is almost zero according to the preceding calculations. This inefficiency is due to floating-point arithmetic. The actual determinant is zero.
You can conclude this by calculating the determinant manually:
1*5*9 + 2*6*7 + 3*4*8 - 1*6*8 - 2*4*9 - 3*5*7
The output is 0.
Whenever you work with NumPy, make sure that you factor in the possibility of floating-point arithmetic rounding errors, even if you appear to be working with integers.
np.matrix.transpose to get the transpose of the matrix:np.matrix.transpose(A)
The output is as follows:
matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
If T is the transpose of matrix A, then T[j][i] is equal to A[i][j].
NumPy comes with many useful features for vectors, matrices, and other mathematical structures.
Change the font size
Change margin width
Change background colour