8 Machine Learning Algorithms in Python
8 Machine Learning Algorithms in Python
1. Objective
Previously, we discussed the techniques of machine learning with Python. Going
deeper, today, we will learn and implement 8 top Machine Learning Algorithms in
Python.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 1/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
a. Linear Regression
Linear regressionis one of the supervised Machine learning algorithms in Python
that observes continuous features and predicts an outcome. Depending on whether it
runs on a single variable or on many features, we can call it simple linear regression or
multiple linear regression.
This is one of the most popular Python ML algorithms and often under-appreciated. It
assigns optimal weights to variables to create a line ax+b to predict the output. We often
use linear regression to estimate real values like a number of calls and costs of houses
based on continuous variables. The regression line is the best line that fits Y=a*X+b to
denote a relationship between independent and dependent variables.
Do you know about Python Machine Learning Environment Setup
Let’s plot this for the diabetes dataset.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
1.
2.
array([941.43097333])
1.
3035.0601152912695
1.
0.410920728135835
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 2/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
1.
1.
b. Logistic Regression
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 3/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
1.
2.
3.
4.
5.
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 4/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
Text(0,0.5,’y’)
1.
Text(0.5,0,’x’)
1.
2.
3.
(-0.25, 1.25)
1.
(-4, 10)
1.
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 5/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
c. Decision Tree
A decision tree falls under supervised Machine Learning Algorithms in Python and
comes of use for both classification and regression- although mostly for classification.
This model takes an instance, traverses the tree, and compares important features with
a determined conditional statement. Whether it descends to the left child branch or the
right depends on the result. Usually, more important features are closer to the root.
Decision Tree, a Machine Learning algorithms in Python can work on both categorical
and continuous dependent variables. Here, we split a population into two or more
homogeneous sets. Let’s see the algorithm for this-
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 6/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
37.
38.
625
(625, 5)
01234
0B1111
1R1112
2R1113
3R1114
4R1115
1.
2.
3.
4.
1.
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 7/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
1.
2.
3.
4.
5.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 8/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
1.
2.
3.
1.
2.
3.
4.
5.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 9/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
(-1, 3.5)
1.
e. Naive Bayes
Naive Bayes is a classification method which is based on Bayes’ theorem. This assumes
independence between predictors. A Naive Bayes classifier will assume that a feature in
a class is unrelated to any other. Consider a fruit. This is an apple if it is round, red, and
2.5 inches in diameter. A Naive Bayes classifier will say these characteristics
independently contribute to the probability of the fruit being an apple. This is even if
features depend on each other.
For very large data sets, it is easy to build a Naive Bayesian model. Not only is this
model very simple, it performs better than many highly sophisticated classification
methods. Let’s build this.
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 10/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
array([[16, 0, 0],
[ 0, 18, 0],
[ 0, 0, 11]], dtype=int64)
1.
2.
3.
array([[16, 0, 0],
[ 0, 0, 18],
[ 0, 0, 11]], dtype=int64)
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 11/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
1.
2.
150
1.
2.
0.96
1.
2.
3.
1.
2.
0.9666666666666667
1.
2.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 12/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
2.
1.0
(150, 4)
1.
(150,)
1.
2.
(150, 4)
1.
(150,)
1.
2.
3.
(90, 4)
1.
(60, 4)
1.
(90,)
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 13/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
1.
(60,)
1.
2.
3.
4.
0.9666666666666667
1.
2.
1.
2.
0.9666666666666667
1.
2.
3.
4.
5.
6.
7.
8.
1.
1.
Text(0,0.5,’Testing Accuracy’)
1.
g. k-Means
k-Means is an unsupervised algorithm that solves the problem of clustering. It classifies
data using a number of clusters. The data points inside a class are homogeneous and
heterogeneous to peer groups.
1.
2.
3.
4.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 15/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair
5.
6.
7.
8.
1.
2.
3.
1.
2.
3.
array([[1.16666667, 1.46666667],
[7.33333333, 9. ]])
1.
array([0, 1, 0, 1, 0, 1])
1.
2.
3.
4.
[1. 2.] 0
[<matplotlib.lines.Line2D object at 0x0642AE10>] [5. 8.] 1
[<matplotlib.lines.Line2D object at 0x06438930>] [1.5 1.8] 0
[<matplotlib.lines.Line2D object at 0x06438BF0>] [8. 8.] 1
[<matplotlib.lines.Line2D object at 0x06438EB0>] [1. 0.6] 0
[<matplotlib.lines.Line2D object at 0x06438FB0>] [ 9. 11.] 1
[<matplotlib.lines.Line2D object at 0x043B1410>]
1.
1.
https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 16/16