Machine Learning Basics with Python
Machine Learning Basics with Python
Plan
I. Introduction Machine Learning .....................................................2
II. Learning supervised or unsupervised..................................2
[Link] learning.....................................................................................................................................3
[Link]-learning-supervised or clustering
[Link]-learning-superviseé ............................................................................................................................4
Page | 1
I. Introduction Machine Learning
Machine Learning is therefore a scientific discipline focused on the development, analysis and
the implementation of automatable methods, which offer the possibility for the machine to evolve thanks to a
learning process. It proves particularly effective when it comes to analyzing large
diverse and evolving data sets, commonly referred to as Big Data.
Today, all companies collect and store large amounts of data. These databases
data, which continue to increase day by day, are poorly exploited, even though they hide
decisive knowledge in the face of the market and competition. To meet this need, a new
industry is being born Data Science. There is a whole plethora of fields in which the
machine learning intervenes, namely in finance, security, medicine, the automotive industry and
technology as a whole as well as different tools for developing these various
algorithms.
Over the years, Python has become a daily tool for engineers and researchers in all fields.
scientific disciplines. Thanks to numerous high-quality libraries, it allows today
to equal or even surpass the most efficient proprietary solutions on the market. It has become a
essential tools for Data Scientists! In this article, we propose to discover the
vast expanse of this ecosystem.
There are two main types of learning: supervised and unsupervised. The main difference
the difference between the two types lies in the fact that we have prior knowledge of what should
be the output values of our samples. The learning algorithm is the method with
the statistical model will be parameterized based on the example data. There are many
different algorithms!
We will choose a particular type of algorithm based on the type of task we wish to accomplish and
the type of data we have. What is the input of the algorithm and what is the output. The objective
this is not to go into the details of the models but rather to provide the reader with elements of
understanding on each of them.
Page | 2
A.L ’apprentissage supervisé
The goal is to understand the mapping function so well that when you have new data
From the input (x), you can predict the output variables (Y) for this data.
Learning is said to be supervised when the data that enters the process is already categorized.
and that algorithms must use it to predict a result in order to be able to do it later
when the data will no longer be categorized.
Classification or Regression
• Decision trees
• K Nearest Neighbours (k nearest neighbors)
• SVM (support vector machine)
• Logistic regression
• Naïve Bayes
• Neural networks
• Linear regression
Page | 3
B. Unsupervised learning or clustering
The input data is not annotated. To do this, the system will cross-reference the information it has.
are submitted, in order to be able to gather in the same class the elements presenting certain
similarities. The training algorithm applies in this case to find the similarities on its own and
distinctions within these data, and to group together those that share characteristics
communes.
Algorithms are left to their own mechanisms to discover and present the structure.
interesting data. Unsupervised learning includes two categories of algorithms,
the clustering algorithm or clustering which consists of separating or dividing a set of
data into a certain number of groups, so that the datasets belonging to the
The same groups resemble each other more than those of other groups, and the association consists of discovering.
interesting relationships between variables in large databases. For example, the
people who watched the movie The Beggar also watched the series Plus Belle la Vie.
• K-means clustering
• Dimensionality Reduction
• Neural networks / Deep Learning
• Principal Component Analysis
• Singular Value Decomposition
• Independent Component Analysis
• Distribution models
• Hierarchical clustering
Semi-supervised learning
Page | 4
III. The Python environment
Python has become a language of choice for scientists, due to its ease of implementation.
and the richness of its ecosystem, particularly thanks to its numerous and efficient libraries of
Numerical calculations often developed by the scientists themselves.
In this section, we will work step by step on a small machine learning project.
classification of iris flowers from the Iris database of the scikit-learn library using
the anaconda environment with the IDE spyder.
The dataset includes 150 samples of each of the three species of iris (Iris setosa, Iris
virginica andIrisversicolor).Four characteristics were measured from each sample: the
length and width ofsepalset des petals,in centimeters. Based on the combination of these
four variables, Fisher developed a linear discriminant analysis model to distinguish the
species from each other. Our project consists of teaching the computer to classify
automatically the species of a new iris flower based on the length and width of the sepals
and petals based on the results already obtained by Mr. Fisher.
Each instance (row) of our data is composed of four attributes to describe an Iris flower.
the dataset is labeled by the type of flower. Thus, for four attributes describing an Iris flower, we
we will know which variant it is. As we already know what the values should be
output of our sample in the case of a categorical variable. We have supervised learning of
type classification, therefore we can use one of the classification algorithms seen
previously.
In our case, we will use a very commonly used algorithm in learning called K-NN (K Nearest Neighbors).
Neighbors) in French the K nearest neighbors. The principle of this model consists in choosing the K
data closest to the studied point in order to predict its class. Our objective will therefore be to train
a model that will be able to recognize the three species of iris.
Page | 5
First, we import the necessary libraries:
We can directly obtain the data via a function call in the scikit-Learn library.
The iris object contains two entries: data and target, let's create two variables X and y for easier access.
with the labels to our data.
Data visualization:
The separation of the groups based on the lengths and widths of the petals seems very clear and decisive.
Once our dataset is loaded, we will split the dataset into two groups: training set.
(the training) and testing set (the test).
It is reminded that we will only use the training set to train our model and we will keep the
testing set to test the performance of our model. We set a distribution of 75% and 25% between
training and testing set.
We create a 5-NN classifier, which takes into account the 5 nearest neighbors for classification.
Page | 6
The k-NN algorithm does not perform any optimization, but will simply memorize all the data.
memory, it's their way of learning to classify new data.
And to predict the species of a new flower, the algorithm will simply look for the 5 closest neighbors.
close to this point and find the species that constitutes the majority of these points, in order to deduce the class
new point.
Let's now measure the error of our classifier, the accuracy_score method determines the proportion of
correctly predicted points.
In our case, the accuracy is 97%, meaning that our algorithm was able to correctly classify 97%.
from our testing set data which is an error of 3%. We can also represent the performances of
our model in a confusion matrix.
It is noted that our algorithm was able to classify 37 flowers out of 38 based on their
characteristics.
Page | 7
IV. Conclusion
The chosen K-NN algorithm is quite simple from a conceptual point of view but perfectly illustrates the
classic issues that arise from it. However, in practice, this algorithm is used quite rarely
in classification because it is costly in computational power. Indeed, a model being
an approximation of reality, it rests on a number of initial assumptions to exist. These
hypotheses are dependent on the context (i.e. the problem at hand). The hypotheses being different
For each type of problem, we must consider different models for different issues.
We could have used several algorithms in this article for our dataset because in reality it
there is no 'ultimate' algorithm and model applicable to all problems. You must therefore
approach each new problem with a fresh perspective and ensure to test multiple algorithms to solve it
solve, by formulating specific hypotheses related to your problem.
Whatever algorithm is chosen, the steps no longer change: we instantiate the algorithm class, we
provides the training data to the fit method for supervised learning and we
make our predictions with the method predict by providing it with the test data.
On the other hand, the difficulties in this discipline are: data acquisition, data distribution
in the training set and in the testing set, the understanding of the mathematical concepts that are behind each
algorithm and finally the choice of hyperparameters.
V. Resources
[Link]
[Link]
[Link]
[Link]
Page | 8