Machine Learning with Python Cookbook Practical Solutions from Preprocessing to Deep Learning 2nd Ed Release 5 2nd Edition Chris Albon download
Machine Learning with Python Cookbook Practical Solutions from Preprocessing to Deep Learning 2nd Ed Release 5 2nd Edition Chris Albon download
https://ebookmeta.com/product/machine-learning-with-python-
cookbook-2nd-edition-chris-albon/
https://ebookmeta.com/product/machine-learning-with-python-
cookbook-2nd-edition-first-early-release-kyle-gallatin/
https://ebookmeta.com/product/machine-learning-with-python-
cookbook-2nd-edition-kyle-gallatin/
https://ebookmeta.com/product/jim-stoppani-s-encyclopedia-of-
muscle-strength-3rd-edition-jim-stoppani/
Knot Alone 1st Edition Ivy Baile
https://ebookmeta.com/product/knot-alone-1st-edition-ivy-baile/
https://ebookmeta.com/product/musings-on-internal-quality-audits-
having-a-greater-impact-1st-edition-duke-okes/
https://ebookmeta.com/product/scientific-american-mind-
scientific-american-2/
https://ebookmeta.com/product/virtual-leadership-practical-
strategies-for-success-with-remote-or-hybrid-work-and-teams-2nd-
edition-pullan/
https://ebookmeta.com/product/fat-loss-supplement-guide-1st-
edition-examine/
Java EE to Jakarta EE 10 Recipes: A Problem-Solution
Approach for Enterprise Java 3rd Edition Josh Juneau
https://ebookmeta.com/product/java-ee-to-jakarta-ee-10-recipes-a-
problem-solution-approach-for-enterprise-java-3rd-edition-josh-
juneau/
Machine Learning with Python Cookbook
SECOND EDITION
Practical Solutions from Preprocessing to Deep Learning
With Early Release ebooks, you get books in their earliest form—the author’s raw and unedited content as they write—so you can
take advantage of these technologies long before the official release of these titles.
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 is not the focus of this book, it will show up frequently throughout the following chapters. This
chapter covers the most common NumPy operations we are likely to run into while working on machine
learning workflows.
Problem
You need to create a vector.
Solution
Use NumPy to create a one-dimensional array:
# Load library
import numpy as np
Discussion
NumPy’s main data structure is the multidimensional array. A vector is just an array with a single
dimension. In order 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
Problem
You need to create a matrix.
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([[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.
See Also
Matrix, Wikipedia
Matrix, Wolfram MathWorld
1.3 Creating a Sparse Matrix
Problem
Given data with very few nonzero values, you want to efficiently represent it.
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 only store 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:
(1, 1) 1
(2, 0) 3
There are a number of types of sparse matrices. However, in compressed sparse row (CSR) matrices,
(1, 1) and (2, 0) represent the (zero-indexed) indices of the non-zero values 1 and 3, respectively.
For example, the element 1 is in the second row and second column. We can see the advantage of sparse
matrices if we create a much larger matrix with many more zero elements and then compare this larger
matrix with our original sparse matrix:
(1, 1) 1
(2, 0) 3
(1, 1) 1
(2, 0) 3
As we can see, despite the fact that we added many more zero elements in the larger matrix, its sparse
representation is exactly the same as our original sparse matrix. That is, the addition of zero elements
did not change the size of the sparse matrix.
As mentioned, there are many different types of sparse matrices, such as compressed sparse column, list
of lists, and dictionary of keys. While an explanation of the different types and their implications is
outside the scope of this book, it is worth noting that while there is no “best” sparse matrix type, there
are meaningful differences between them and we should be conscious about why we are choosing one
type over another.
See Also
Sparse matrices, SciPy documentation
101 Ways to Store a Sparse Matrix
Problem
You need to pre-allocate arrays of a given size with some value.
Solution
NumPy has functions for generating vectors and matrices of any size using 0s, 1s, or values of your
choice.
# Load library
import numpy as np
Discussion
Generating arrays prefilled with data is useful for a number of purposes, such as making code more
performant or having synthetic data to test algorithms with. In many programming languages, pre-
allocating an array of default values (such as 0s) is considered common practice.
Problem
You need to select one or more elements in a vector or matrix.
Solution
NumPy’s arrays make it easy to select elements in vectors or matrices:
# Load library
import numpy as np
# Create row vector
vector = np.array([1, 2, 3, 4, 5, 6])
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Discussion
Like most things in Python, NumPy arrays are zero-indexed, meaning that the index of the first element
is 0, not 1. With that caveat, NumPy offers a wide variety of methods for selecting (i.e., indexing and
slicing) elements or groups of elements in arrays:
array([1, 2, 3])
array([4, 5, 6])
array([6, 5, 4, 3, 2, 1])
array([[1, 2, 3],
[4, 5, 6]])
array([[2],
[5],
[8]])
Problem
You want to describe the shape, size, and dimensions of the matrix.
Solution
Use the shape, size, and ndim attributes of a NumPy object:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
12
Discussion
This might seem basic (and it is); however, time and again it will be valuable to check the shape and
size of an array both for further calculations and simply as a gut check after some operation.
Problem
You want to apply some function to all elements in an array.
Solution
Use NumPy’s vectorize method:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Discussion
NumPy’s vectorize class converts a function into a function that can apply to all elements in an array
or slice of an array. It’s worth noting that vectorize is essentially a for loop over the elements and
does not increase performance. Furthermore, NumPy arrays allow us to perform operations between
arrays even if their dimensions are not the same (a process called broadcasting). For example, we can
create a much simpler version of our solution using broadcasting:
Broadcasting does not work for all shapes and situations, but a common way of applying simple
operations over all elements of a numpy array.
Problem
You need to find the maximum or minimum value in an array.
Solution
Use NumPy’s max and min methods:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Return maximum element
np.max(matrix)
Discussion
Often we want to know the maximum and minimum value in an array or subset of an array. This can be
accomplished with the max and min methods. Using the axis parameter we can also apply the operation
along a certain axis:
array([7, 8, 9])
Problem
You want to calculate some descriptive statistics about an array.
Solution
Use NumPy’s mean, var, and std:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Return mean
np.mean(matrix)
5.0
# Return variance
np.var(matrix)
6.666666666666667
2.5819888974716112
Discussion
Just like with max and min, we can easily get descriptive statistics about the whole matrix or do
calculations along a single axis:
Problem
You want to change the shape (number of rows and columns) of an array without changing the element
values.
Solution
Use NumPy’s reshape:
# Load library
import numpy as np
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
Discussion
reshape allows us to restructure an array so that we maintain the same data but it is organized as a
different number of rows and columns. The only requirement is that the shape of the original and new
matrix contain the same number of elements (i.e., the same size). We can see the size of a matrix using
size:
matrix.size
12
One useful argument in reshape is -1, which effectively means “as many as needed,” so reshape(1,
-1) means one row and as many columns as needed:
matrix.reshape(1, -1)
Finally, if we provide one integer, reshape will return a 1D array of that length:
matrix.reshape(12)
Problem
You need to transpose a vector or matrix.
Solution
Use the T method:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Transpose matrix
matrix.T
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
Discussion
Transposing is a common operation in linear algebra where the column and row indices of each element
are swapped. One nuanced point that is typically overlooked outside of a linear algebra class is that,
technically, a vector cannot be transposed because it is just a collection of values:
# Transpose vector
np.array([1, 2, 3, 4, 5, 6]).T
array([1, 2, 3, 4, 5, 6])
However, it is common to refer to transposing a vector as converting a row vector to a column vector
(notice the second pair of brackets) or vice versa:
array([[1],
[2],
[3],
[4],
[5],
[6]])
Problem
You need to transform a matrix into a one-dimensional array.
Solution
Use flatten:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Flatten matrix
matrix.flatten()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Discussion
flatten is a simple method to transform a matrix into a one-dimensional array. Alternatively, we can
use reshape to create a row vector:
matrix.reshape(1, -1)
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
One more common method to flatten arrays is the ravel method. Unlike flatten which returns a copy
of the original array, ravel operates on the original object itself and is therefore slightly faster. It also
lets us flatten lists of arrays, which we can’t do with the flatten method. This operation is useful for
flattening very large arrays and speeding up code.
array([1, 2, 3, 4, 5, 6, 7, 8])
Problem
You need to know the rank of a matrix.
Solution
Use NumPy’s linear algebra method matrix_rank:
# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 1, 1],
[1, 1, 10],
[1, 1, 15]])
Another Random Scribd Document
with Unrelated Content
qu’il aurait la plus grande peine à reconstituer. Il ne voulait plus
risquer pareille avanie. La Truphot, séduite, sollicita la bête.
—Elle serait très bien soignée; il pouvait en être sûr; elle adorait
les animaux, et Aphrodite ferait, sans nul doute, le meilleur ménage
avec Nénette, Spot et Sapho qui, d’ailleurs, lui témoignaient déjà de
l’amitié, car ils donnaient l’assaut aux jupes de la vieille femme pour
flairer, de plus près et avec des frétillements, la fragrance sexuelle
de leur nouvelle camarade.
—Je n’osais point vous l’offrir, madame, acquiesça Cyrille
Esghourde, mais je ne peux vraiment souhaiter meilleur destin pour
la pauvre compagne de ma solitude. Puis, dans un besoin d’informer
l’assistance de sa nature «artiste», il ajouta:
—Jusqu’ici j’adorais les chats, le sonnet de Baudelaire m’avait
emballé, car j’aime à me conformer aux opinions littéraires les plus
en faveur, je le confesse. J’en possédais toujours deux ou trois chez
moi, mais depuis quelque temps je trouve que ces animaux de
perversion sont un peu surfaits! Ils copulent avec platitude, odorent
désagréablement, et n’ont rien des adorables complications
humaines. Or la complication est la condition une, essentielle, de
l’amour des raffinés. A l’heure présente, je me demande comment le
poète des divines névroses a pu s’éprendre de ces félins sans
détraquement, qui aiment et caressent à la façon des portefaix ou
des chefs de bureau. Comment a-t-il osé son fameux sonnet, lui,
l’immortel satanique, comment n’a-t-il pas rougi de ces vers,
d’ailleurs insanes? Souvenez-vous:
Les amoureux fervents et les savants austères
Aiment également en leur mûre saison
Les chats puissants et doux, orgueil de la maison...
· · · · · · · · · · · · · · · · · · ·
Chose surprenante, la Morale éternelle, la Loi inflexible qui
ordonnance la conscience des justes, l’infrangible apanage de la
dignité humaine, que cet homme avait bafoués, tiraient de lui une
implacable vengeance. Dans ses œuvres, les lamentations du
supplicié qu’il était, malgré sa fortune, s’orchestraient sourdement.
Le Rut dont il s’était enrichi lui avait déclaré une vendetta farouche,
et on en pouvait démêler à travers ses livres toutes les cérébrales
péripéties. La lancination continuelle des souvenirs infâmes,
l’impuissante furie contre le passé, avaient implanté dans son esprit
d’analyste l’aiguillon rougi et barbelé d’une endémique
constupration. Il avait dédié trois cents pages à la folie furieuse de la
chair, entonné pendant tout un volume le Magnificat du Sadisme et,
dans chacun de ses romans, il y avait un viol, le déchirement
lamentable d’une enfant par un vieillard forcené. Sa littérature
traduisait sa perpétuelle hypnose: il avait, par contraste, la hantise
de la virginité, des vierges sur lesquelles, par rage, sans doute, il
faisait s’acharner les démoniaques lubriques sortis de son
imagination. Et une douleur terrible issait de ces pages, ruisselait de
ces immondes amours: tout le martyre d’un être qui ne peut pas
effacer ce qui est, toute l’angoisse d’un homme, à qui le Sexe,
monstrueusement symbolisé, dans le tête-à-tête coutumier, offre et
dérobe en même temps son mystère, la torture d’un malheureux
usant ses ongles sur le sphynx de granit, ayant besoin de tout savoir
pour se racheter devant soi-même, pour se trouver une excuse ou
une joie peut-être, et qui, avec des désespoirs et des râles,
s’acharne à faire l’impossible clinique de la Volupté!
· · · · · · · · · · · · · · · · · · ·
On aurait pu l’ignorer et laisser à la carapace d’opprobre qui
l’étreint et l’étouffe lentement le soin d’en faire justice si l’on ne
s’était rappelé à temps qu’il souillait des idées nobles et leur faisait
perdre peu à peu, en combattant pour elles, le pouvoir qu’elles
gardent encore sur quelques âmes ingénues. Qu’il détaille les
prouesses immuablement imbéciles et nous initie aux délectations
cutanées des antropopithèques, des amants contemporains, en mal
d’amour; qu’il fasse panteler l’adultère sur le divan d’analyse de Paul
Bourget; qu’il nous conte, s’il le veut, les sursauts, et nous montre
les écumes de la viande bourgeoise travaillée par la fringale du
Pouvoir ou des fornications; mais qu’il ne touche pas aux saintes
formules de Pitié et de Réparation sociale. Nul n’a le droit de parler à
la foule d’Équité et de Justice, si son pelage n’est pas d’une hermine
impolluée. Le Chabanais n’a pas le droit de nous dire qu’il sait où se
trouve la Vérité. Il faut trancher net, au bord de l’autel, d’un glaive
sans miséricorde, la main breneuse de l’officiant putride qui prétend
s’emparer du hanap, du calice miraculeux, où gît la liqueur de
miséricorde, capable de délier, peut-être, le cœur des hommes du
mensonge, de la férocité et de l’égoïsme. La première œuvre qui
s’impose, avant la mise en route vers la Civilisation supérieure,
consiste à arracher aux épaules des drôles, des rufians, des fourbes
et des ophidiens à face humaine, les paillons fallacieux sous lesquels