Machine Learning with Python Cookbook, 2nd Edition (First Early Release) Kyle Gallatin instant download
Machine Learning with Python Cookbook, 2nd Edition (First Early Release) Kyle Gallatin instant download
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/machine-learning-with-python-
cookbook-2nd-edition-chris-albon/
https://ebookmeta.com/product/machine-learning-with-python-
cookbook-practical-solutions-from-preprocessing-to-deep-
learning-2nd-ed-release-5-2nd-edition-chris-albon/
https://ebookmeta.com/product/the-white-educators-guide-to-
equity-jeramy-wallace/
Lawyer Games After Midnight in the Garden of Good and
Evil 2nd Edition Dep Kirkland
https://ebookmeta.com/product/lawyer-games-after-midnight-in-the-
garden-of-good-and-evil-2nd-edition-dep-kirkland/
https://ebookmeta.com/product/artificial-intelligence-a-modern-
approach-3rd-edition-stuart-russell-peter-norvig/
https://ebookmeta.com/product/body-and-soul-in-hellenistic-
philosophy-1st-edition-brad-inwood/
https://ebookmeta.com/product/gravity-falls-don-t-color-this-
book-1st-edition-emmy-cicierega-alex-hirsch/
https://ebookmeta.com/product/folk-tales-of-bengal-1st-edition-
lal-behari-day/
Annual Review of Gerontology and Geriatrics Volume 39
2019 154th Edition Roland J Thorpe Jr Phd
https://ebookmeta.com/product/annual-review-of-gerontology-and-
geriatrics-volume-39-2019-154th-edition-roland-j-thorpe-jr-phd/
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]])
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
(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 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, 4, 5, 6])
array([1, 2, 3])
array([4, 5, 6])
# Select the last element
vector[-1]
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]])
(3, 4)
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:
Packing
Foods
Food for growing children. Jessie P. Rich. (Texas. Univ. Bul. 1707)
20p pa ‘17 Austin, Tex.
Suggestions for infant feeding. Anna E. Richardson. (Texas. Univ.
Bul. 1706) 11p pa ‘17 Austin, Tex.
What to feed the children. Dorothy Reed Mendenhall and A. L.
Daniels. (Wis. Univ. Agric. College extension service. Circular 69)
10p pa ‘17 Madison, Wis.
Children’s library
Child in the library. (Riverside public library. Bul. 146) 10p pa ‘17
Riverside, Cal.
Citizenship
Proceedings of the first citizenship convention held at
Washington, D.C., July 10-15, 1916. Raymond F. Crist. (U.S.
Labor) 86p pa 10c ‘17 Supt. of doc.
Work of the public schools with the Bureau of naturalization in the
preparation for citizenship responsibilities of the candidate for
naturalization. (U.S. Bur. of naturalization. Extract from report of
Comr., 1916) 50p pa ‘17 Supt. of doc.
City and county consolidation
Report of the Los Angeles realty board on city and county
consolidation. 28p pa ‘17 Los Angeles, Cal.
City planning
City planning. Frank G. Bates. (Indiana. Bur. of legislative
information. Bul. no. 8) 31p pa Dec. ‘16 Fort Wayne, Ind.
Coal
Economical purchase and use of coal for heating homes with
special reference to conditions in Illinois; a non-technical
manual for the householder and operator of small house-
heating plants. (Illinois. Univ. Eng. Extension station. Circular
no. 4) 58p pa 10c ‘17 Urbana, Ill.
Commerce
Act to regulate commerce (as amended) including text or related
sections; rev. to Jan. 1, 1917 (U.S. Interstate commerce comm.)
157p pa 10c ‘17 Supt. of doc.
Concluding chapter of the Federal trade commission report on
cooperation in American export trade. (U.S. Federal trade
comm.) 14p pa 5c ‘16 Supt. of doc.
Report on cooperation in American export trade. (U.S. Federal
trade comm.) 2v pa $1.15 ‘16 Supt. of doc.
Commercial organization, German
German foreign-trade organization with supplementary statistical
material and extracts from official reports on German methods.
Chauncey Depew Snow. (U.S. For. & dom. comm. Miscellaneous
ser. no. 57) 182p pa 20c ‘17 Supt. of doc.
Community and national life
Lessons in community and national life. (U.S. Educ. Community
leaflets)
No. 1. Sec. A. Designed for use in the upper classes of the high
school. 32p pa ‘17 Supt. of doc.
No. 2. Sec. B. Designed for use in the upper grades of elementary
schools and the first year of high school. 32p pa ‘17 Supt. of
doc.
No. 3. Sec. C. Designed for use in the intermediate grades. 32p
pa ‘17 Supt. of doc.
These lessons will be issued each month during the school year,
1917-18. They will be edited by Charles H. Judd, director of the
school of education of the University of Chicago, and Leon C.
Marshall, dean of the school of commerce and administration of
the University of Chicago. The lessons will be prepared in three
sections, namely, Section A, for the upper classes of high
schools; Section B, for the upper grades of elementary schools
and the first class of high schools; Section C, for intermediate
grades of elementary schools. Each section will contain three or
four lessons. Eight numbers of each section will be issued, one
number appearing on the first of each calendar month. Copies
may be purchased from the U.S. Food administration,
Washington, D.C., at the following prices: One copy, 5 cents;
each additional copy in same order, 3 cents; 100 copies, $2; 500
copies, $5; 1,000 copies, $9.50; subscriptions for the series of
any section, eight times the prices named. Such subscriptions
are urged in order to assure prompt and regular delivery.
Conscription
Conscription in the Confederate states of America, 1862-1865. R.
P. Brooks. (Ga. univ. Bul. vol. 17, no. 4) 442p pa ‘17 Athens, Ga.
Cooperation
Business practice and accounts for cooperative stores. J. A.
Bexell. (U.S. Agric. Bul. 381) 56p 5c ‘16 Supt. of doc.
Co operate! (Texas. Markets and warehouse commission. Bul. no.
37) 36p pa ‘17 Austin, Tex.
Cooperative associations, organizing. James E. Boyle. (N.D. Agric.
Experiment station. Circular 16) 24p pa ‘17 Fargo, N.D.
Cooperative retail delivery; the organization and methods of
central delivery systems. Walton S. Bittner. (Indiana. Univ.
Extension division. Bul. vol. 3, no. 1) 30p pa ‘17 Bloomington,
Ind.
How farmers may improve their personal credit through
cooperation. C. W. Thompson. (U.S. Farmers’ Bul. 654) 16p pa
5c ‘17 Supt. of doc.
Survey of typical cooperative stores in the U.S. J. A. Bexell. (U.S.
Agric. Bul. 394) 32p 5c ‘16 Supt. of doc.
Copyright
Copyright law of the United States of America; being the act of
March 4, 1909, (in force July 1, 1909) as amended by the acts
of August 24, 1912, March 2, 1913 and March 28, 1914,
together with rules for practice and procedure under section 25
by the Supreme Court of the United States. (Copyright office.
Bul. no. 14) 66p pa ‘16 Supt. of doc.
Corn
Corn. (N.J. Agric. Experiment station. Circular 69) 7p pa ‘17 New
Brunswick, N.J.
Corporations
Service instruction
Negro
Public service