The Role of Python in Artificial Intelligence
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 high-level programming language. This means that you don't have to worry about memory allocation, pointers, or machine code in general. You can write code in a convenient fashion and rely on Python's robustness. Python is also cross-platform compatible.
- The strong emphasis on developer experience makes Python a very popular choice among software developers. In fact, according to a 2018 developer survey by https://www.hackerrank.com, across all ages, Python ranks as the number one preferred language of software developers. This is because Python is easily readable and simple. Therefore, Python is great for rapid application development.
- Despite being an interpreted language, Python is comparable to other languages used in data science such as R. Its main advantage is memory efficiency, as Python can handle large, in-memory databases.
Note
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.
Why is Python Dominant in Machine Learning, Data Science, and AI?
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.
Anaconda in Python
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.
Note
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.
Python Libraries for Artificial Intelligence
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:
- NumPy: NumPy is a computing library for Python. As Python does not come with a built-in array data structure, we have to use a library to model vectors and matrices efficiently. In data science, we need these data structures to perform simple mathematical operations. We will extensively use NumPy in future modules.
- SciPy: SciPy is an advanced library containing algorithms that are used for data science. It is a great complementary library to NumPy, because it gives you all the advanced algorithms you need, whether it be a linear algebra algorithm, image processing tool, or a matrix operation.
- pandas: pandas provides fast, flexible, and expressive data structures such as one-dimensional series and two-dimensional DataFrames. It efficiently loads, formats, and handles complex tables of different types.
- scikit-learn: scikit-learn is Python's main machine learning library. It is based on the NumPy and SciPy libraries. scikit-learn provides you with the functionality required to perform both classification and regression, data preprocessing, as well as supervised and unsupervised learning.
- NLTK: We will not deal with natural language processing in this book but NLTK is still worth mentioning, because this library is the main natural language toolkit of Python. You can perform classification, tokenization, stemming, tagging, parsing, semantic reasoning, and many other services using this library.
- TensorFlow: TensorFlow is Google's neural network library, and it is perfect for implementing deep learning artificial intelligence. The flexible core of TensorFlow can be used to solve a vast variety of numerical computation problems. Some real-world applications of TensorFlow include Google voice recognition and object identification.
A Brief Introduction to the NumPy Library
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:
Figure 1.1: Multiplication calculation with two matrices
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.
Exercise 1: Matrix Operations Using NumPy
We will be using IPython and the following matrix to solve this exercise. We will start by understanding the NumPy syntax:
Figure 1.2: Simple Matrix
Using NumPy, calculate the following:
- The square of the matrix
- The determinant of the matrix
- The transpose of the matrix
Let's begin with NumPy matrix operations:
- Import the NumPy library.
import numpy as np
- Create a two-dimensional array storing the matrix:
A = np.mat([[1,2,3],[4,5,6],[7,8,9]])
Notice the
np.mat
construct. If you have created annp.array
instead ofnp.mat
, the solution for the array multiplication will be incorrect. - NumPy supports matrix multiplication by using the asterisk:
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
. - Use
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.
- Use
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, thenT[j][i]
is equal toA[i][j]
.NumPy comes with many useful features for vectors, matrices, and other mathematical structures.