Machine Learning With Python Cookbook 2e Preview
Machine Learning With Python Cookbook 2e Preview
1.0 Introduction
NumPy is a foundational tool of the Python machine learning stack. NumPy allows
for efficient operations on the data structures often used in machine learning: vectors,
matrices, and tensors. While NumPy isn’t the focus of this book, it will show up
frequently in the following chapters. This chapter covers the most common NumPy
operations we’re likely to run into while working on machine learning workflows.
Solution
Use NumPy to create a one-dimensional array:
# Load library
import numpy as np
1
Discussion
NumPy’s main data structure is the multidimensional array. A vector is just an array
with a single dimension. To create a vector, we simply create a one-dimensional array.
Just like vectors, these arrays can be represented horizontally (i.e., rows) or vertically
(i.e., columns).
See Also
• Vectors, Math Is Fun
• Euclidean vector, Wikipedia
Solution
Use NumPy to create a two-dimensional array:
# Load library
import numpy as np
# Create a matrix
matrix = np.array([[1, 2],
[1, 2],
[1, 2]])
Discussion
To create a matrix we can use a NumPy two-dimensional array. In our solution, the
matrix contains three rows and two columns (a column of 1s and a column of 2s).
NumPy actually has a dedicated matrix data structure:
matrix_object = np.mat([[1, 2],
[1, 2],
[1, 2]])
matrix([[1, 2],
[1, 2],
[1, 2]])
However, the matrix data structure is not recommended for two reasons. First, arrays
are the de facto standard data structure of NumPy. Second, the vast majority of
NumPy operations return arrays, not matrix objects.
Solution
Create a sparse matrix:
# Load libraries
import numpy as np
from scipy import sparse
# Create a matrix
matrix = np.array([[0, 0],
[0, 1],
[3, 0]])
Discussion
A frequent situation in machine learning is having a huge amount of data; however,
most of the elements in the data are zeros. For example, imagine a matrix where the
columns are every movie on Netflix, the rows are every Netflix user, and the values
are how many times a user has watched that particular movie. This matrix would
have tens of thousands of columns and millions of rows! However, since most users
do not watch most movies, the vast majority of elements would be zero.
A sparse matrix is a matrix in which most elements are 0. Sparse matrices store only
nonzero elements and assume all other values will be zero, leading to significant
computational savings. In our solution, we created a NumPy array with two nonzero
values, then converted it into a sparse matrix. If we view the sparse matrix we can see
that only the nonzero values are stored:
# View sparse matrix
print(matrix_sparse)
(1, 1) 1
(2, 0) 3
See Also
• SciPy documentation: Sparse Matrices
• 101 Ways to Store a Sparse Matrix
Discussion
Generating arrays prefilled with data is useful for a number of purposes, such as
making code more performant or using synthetic data to test algorithms. In many
programming languages, preallocating an array of default values (such as 0s) is
considered common practice.
Solution
NumPy arrays make it easy to select elements in vectors or matrices:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],