Python Machine Learning 3rd editionI was finally able to read the third edition of Sebastian Raschka’s Python Machine Learning, which focuses on machine learning and in-depth learning with Python, Scikit-learn and TensorFlow 2.
It is an excellent book and at some point in the ML/DL chronology. I’ve always wanted to try. Thanks to an electronic copy of the book to be reviewed, I was finally able to verify it. The book consists of 18 chapters, in which all aspects of machine learning are discussed in the first 12 chapters and the basic principles of neural networks and advanced learning in the last 6 chapters.
So far I can go through most of the first chapters (the first six) about learning by machine. And here are some quick thoughts about the book after reading a few chapters.
What do I like about learning python techniques?
TLDR: If you are interested in the short summary, buy it if you are interested in ML and deep learning. I strongly advise you to obtain a physical copy. I have an electronic copy and that made it difficult for me.
One of the first things I noticed while reading the book is that it stands out from the rest of the books on machine learning (most). It is a very practical book for machine learning, but the book does not hesitate to show some of the mathematics behind the methods.
And most of the chapters explaining the new technique start by showing you how to make it from scratch, and then how to use it based on scientific learning. I think one of the reasons why the emphasis is on a solid foundation and practical learning is that the book comes from the courses that Sebastian gives almost every year.
What can you learn from Python Machine Learning?
If you want to know what you can learn in this book, here is a brief overview of some of the chapters covered in the book. In the second chapter of the book you will learn simple machine learning algorithms for classification. It starts by implementing one of the most fundamental ML classification algorithms, the sceptron, and implements the sceptron from scratch. This is an extremely useful chapter for both classical machine learning and in-depth learning.
Chapter 3 is devoted to the journey through self-learning class members with a scientific background. Here you can explore the most commonly used algorithms and learn how to implement linear classification algorithms in Python. The fourth chapter is about data mining to build good training datasets for building good machine learning models. This is one of the most important chapters that really teaches you the practical side – it’s called machine learning. You learn
- To remove and apply missing values in a dataset
- Categorical data processing for use in machine learning algorithms
- How to select the relevant functions for a machine learning model.
And chapter 5 is about reducing dimensionality and how it can be used in the selection of feature films. He starts with vanilla PCA and learns classical linear discriminant analysis and then moves on to nonlinear dimensional reduction techniques using Principal Component Core Analysis (PCA).
And in Chapter 6 you can apply everything you learned in Chapter 5 and learn how to make good machine learning models, refine them and evaluate them.
Python-based Perceptron learning algorithm from zero
The best way to learn from the book is to work in chapters. In all chapters you will not only learn mechanically, but also improve your Python skills, because sebastin uses an excellent mix of NumPy, Pandas and scikit-learn.
I certainly will. Here is a quick example of the use of code fragments from the book, but with a different dataset. Here I am working on the perceptron classification, one of the basic classification algorithms described in detail in chapter 2.
The book illustrates the sceptron learning algorithm using the classic IRIS dataset. Here we use the Palmer-Penguin dataset instead of the Iris dataset. We will see an example of using the Python perceptron learning algorithm code from the book to build a machine learning model and predict penguin species based on two characteristics of penguins.
Let’s start by downloading the necessary packages.
import np
import pandas as pd
import matplotlib.pyplot as plt
import zeepanda as sns
We will use the Palmer penguin dataset available in the Seaborn integrated datasets.
Penguins = sns.load_dataset(Penguins)
For simplicity, we consider only two possibilities in the dataset and remove all observations with missing values. The two characteristics we will use here are the length and the depth of the penguin number. Our class variable is a representation, and we will replace the data so that it contains only two values from the representation.
df = penguin[‘species’,jaw length_mm, jaw depth_mm]]
df = df.query(‘species != chinstrap’)
df = df.dropna()
And that’s what our data looks like.
df.head()
See invoice length in mm invoice depth in mm
0 Adeli 39,1 18,7
1 Adeli 39,5 17,4
2 Adeli 40,3 18,0
4 Adeli 36,7 19,3
5 Adeli 39,3 20,6
First we visualize the data by simply plotting the point cloud and colouring it by species.
sns.set_context(talk, font_scale=1.5)
plt.figure(figsize=(10,8))
sns.scatterplot(x=jaw length_mm,
#y=jaw length_mm,
y=jaw depth_mm,
hue=species,
data=df)
plt.savefig(pinguin_scatterplot_Seaborn.png,
format=’png’,dpi=150)
We can clearly see that there is a clear separation between species and that there will be a large dataset for our perceptron learning algorithm.
Distribution plot of penguin data
We will use the Perceptron algorithm, implemented in Python from scratch. It essentially uses Numpy, so we convert the attributes and class variables in the Pandas data frame to Numpy arrays.
y = df.iloc[:, 0].values
y = np.where(y == ‘Adelie’, -1, 1)
# Extract the length and depth of the data frames in the form of Tables
X = df.iloc[:, [1, 2]].values
Perception algorithm
This is the Python class that implements the Perceptron algorithm from the book. Take a look at the code repository that was created for 3. The book edition is available.
Perceptron class (object) :
The Perceptron classification.
Parameters
————
eta: float
Learning rate (between 0.0 and 1.0)
n_iter: int
Runs through the training data set.
random_state : int
Sets a random number generator to initialize a random weight
.
Attributes
———–
w_: 1d-array
Weight after installation.
Error_ : List
number of erroneous classifications (updates) per era.
def __init__(self, eta=0.01, n_iter=50, random_state=1):
self.eta = eta
self.n_iter = n_iter
self.random_state = random_state
…is not enough:
Modify the data of the corporate event.
Parameters
———-
X: {array-like}, form = [n_examples, n_features]
drive vectors, where n_examples is the number of
examples, and n_features is the number of characteristics.
y: array-like, form = [n_examples]
Target values.
Back
——-
itself : Object
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01, size
=1 + X.shape[1])
self.errors_ = []
for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target – self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self
def net_input(self, X) :
Calculate the network input
return np.dot(X, self.w_[1:]) + self.w_[0].
Tell me, X:
The class label returned after step
returns np.where(self.net_input(X) >= 0.0, 1, -1).
Let’s practice our perceptual algorithm with the penguin dataset. Here we specify the training time and the number of iterations used to adjust the data.
ppn = Perceptron(eta=0.1, n_iter=25)
ppn.fit(X, y)
Now let’s see how the perceptron algorithm worked on our dataset. To do this, we check how the algorithm converges by plotting the classification error for each era. If the algorithm would converge, it would find a clear decision limit that would unambiguously separate the two penguin species.
sns.set_context(talk, font_scale=1.5)
plt.figure(figsize=(10,8))
plt.plot(range(1, len(ppn.errors_) + 1),
ppn.errors_, marker=’o’)
plt.xlabel(‘epochs’)
plt.ylabel(‘number of updates’)
plt.show()
plt.savefig(perceptron_performance.png,
format=’png’,dpi=150)
As can be seen, our algorithm took some time to learn the perceptron, but it came together long after the twelfth era. He is able to classify training examples perfectly.
Defining the platform model
Let’s visualize the decision limit.
Resolution = 0,02
x1_min, x1_max = X[:, 0].min() – 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() – 1, X[:, 1].max() + 1
xx1, xx2 = np.mesh grid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))
Z = ppn.preict(np.array([xx1.ravel(), xx2.ravel()]).T)
Here is a small tool from the book to visualize the limits of the solutions.
from matplotlib.colors import ListedColormap
def plot_decision_regions(X, y, classifier, resolution=0.02) :
# Configure marker generator and color map
markers = (‘s’, ‘x’, ‘o’, ‘^’, ‘v’)
colors = (‘red’, ‘blue’, ‘light green’, ‘grey’, ‘cyan’)
cmap = ListedColormap(colors[:len(np).)unique(y)])
# Decision area construction
x1_min, x1_max = X[:, 0].min() – 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() – 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))
Z = classifier.preict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.form)
plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
# examples of plot classes
for idx, cl in enumerate(np.unique(y))
plt.scatter(x=X[y == cl, 0],
y=X[y == cl, 1],
alpha=0.8,
c=colors [idx],
marker=markers [idx],
label=cl,
edgecolor=’black’)
We can now use the function to create a graph to test the performance of our perceptual algorithm.
sns.set_context(talk, font_scale=1.2)
plt.figure(figsize=(10,8))
plot_decision_regions(X, y, classifier=ppn)
plt.xlabel(‘jaw length (mm)’)
plt.ylabel(‘beak depth (mm)’)
plt.legenda(loc=’top left’)
plt.savefig(perceptron_classifier.png,
format=’png’,dpi=150)
We can clearly see the decision limit, and our sceptron learning algorithm has correctly classified the penguin species.
Perceptron classifier
Find out what happens if you don’t let the sensor algorithm converge. Reduce the number of iterations and save the platform algorithm to see what happens.
Related Tags:
python machine learning book,python machine learning sebastian raschka pdf github,python machine learning book 3rd-edition github,python machine learning sebastian raschka 3rd edition download,python machine learning 3rd edition pdf github,python machine learning table of contents,python machine learning packt pdf,python machine learning 3rd github