100% found this document useful (3 votes)
480 views

8 Machine Learning Algorithms in Python

The document discusses 8 machine learning algorithms that are important to learn in Python: linear regression, logistic regression, decision trees, support vector machines, Naive Bayes, k-nearest neighbors, clustering, and neural networks. For each algorithm, it provides a brief overview of what the algorithm is used for and an example Python code implementation using sample datasets to demonstrate how it works. The goal is to help readers understand and be able to implement these fundamental machine learning techniques in Python.

Uploaded by

uzair12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
480 views

8 Machine Learning Algorithms in Python

The document discusses 8 machine learning algorithms that are important to learn in Python: linear regression, logistic regression, decision trees, support vector machines, Naive Bayes, k-nearest neighbors, clustering, and neural networks. For each algorithm, it provides a brief overview of what the algorithm is used for and an example Python code implementation using sample datasets to demonstrate how it works. The goal is to help readers understand and be able to implement these fundamental machine learning techniques in Python.

Uploaded by

uzair12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair

8 Machine Learning Algorithms in


Python – You Must Learn
BY DATAFLAIR TEAM· PUBLISHED AUGUST 9, 2018· UPDATED SEPTEMBER 28, 2018

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.

Let’s begin the journey of Machine Learning Algorithms in Python Programming.

Machine Learning Algorithms in Python – You Must LEARN

2. Machine Learning Algorithms in Python


Followings are the Algorithms of Python Machine Learning:

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.

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

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

<matplotlib.collections.PathCollection object at 0x0584FF70>

1.

[<matplotlib.lines.Line2D object at 0x0584FF30>]


1.

([], <a list of 0 Text xticklabel objects>)

1.

([], <a list of 0 Text yticklabel objects>)

1.

Machine Learning Algorithms in Python – Linear Regression

Python Machine Learning – Data Preprocessing, Analysis & Visualization

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

Logistic regression is a supervised classification is unique Machine Learning algorithms


in Python that finds its use in estimating discrete values like 0/1, yes/no, and true/false.
This is based on a given set of independent variables. We use a logistic function to
predict the probability of an event and this gives us an output between 0 and 1.
Although it says ‘regression’, this is actually a classification algorithm. Logistic
regression fits data into a logit function and is also called logit regression. Let’s plot this.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

<matplotlib.collections.PathCollection object at 0x057B0E10>

1.
2.
3.
4.
5.

[<matplotlib.lines.Line2D object at 0x057BA090>]


1.
2.

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

1.

[<matplotlib.lines.Line2D object at 0x057BA0B0>]


1.

<matplotlib.lines.Line2D object at 0x05860E70>

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.

<matplotlib.legend.Legend object at 0x057C89F0>


Do you know How to Split Train and Test Set in Python Machine Learning

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

Machine Learning Algorithms in Python -


Logistic

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.

Machine Learning Algorithms in Python – Decision Tree

1.

[[ 0 6 7] [ 0 67 18] [ 0 19 71]] 73.40425531914893

Machine Learning Algorithms in Python – Decision Tree

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

Machine Learning Algorithms in Python – Decision Tree

1.

[[ 0 6 7] [ 0 63 22] [ 0 20 70]] 70.74468085106383

Machine Learning Algorithms in Python – Decision Tree Algorithm


i

Let’s explore 4 Machine Learning Techniques with Python

d. Support Vector Machines (SVM)


SVM is a supervised classification is one of the most important Machines Learning
algorithms in Python, that plots a line that divides different categories of your data. In
this ML algorithm, we calculate the vector to optimize the line. This is to ensure that the
closest point in each group lies farthest from each other. While you will almost always
find this to be a linear vector, it can be other than that.
In this Python Machine Learning tutorial, we plot each data item as a point in an n-
dimensional space. We have n features and each feature has the value of a certain
coordinate.
First, let’s plot a dataset.

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

<matplotlib.collections.PathCollection object at 0x04E1BBF0>

1.

Machine Learning Algorithms in Python – SVM

1.
2.
3.

<matplotlib.collections.PathCollection object at 0x07318C90>

1.
2.
3.
4.
5.

[<matplotlib.lines.Line2D object at 0x07318FF0>]


<matplotlib.collections.PolyCollection object at 0x073242D0>
[<matplotlib.lines.Line2D object at 0x07318B70>]
<matplotlib.collections.PolyCollection object at 0x073246F0>
[<matplotlib.lines.Line2D object at 0x07324370>]
<matplotlib.collections.PolyCollection object at 0x07324B30>

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.

Machine Learning Algorithms in Python – Support Vector Machine

Follow this link to know about Python PyQt5 Tutorial

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)

f. kNN (k-Nearest Neighbors)


This is a Python Machine Learning algorithms for classification and regression- mostly
for classification. This is a supervised learning algorithm that considers different
centroids and uses a usually Euclidean function to compare distance. Then, it analyzes
the results and classifies each point to the group to optimize it to place with all closest
points to it. It classifies new cases using a majority vote of k of its neighbors. The case it
assigns to a class is the one most common among its K nearest neighbors. For this, it
uses a distance function.

i. Training and testing on the entire dataset


1.
2.
3.
4.
5.
6.
7.

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

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,


intercept_scaling=1, max_iter=100, multi_class=’ovr’, n_jobs=1,
penalty=’l2′, random_state=None, solver=’liblinear’, tol=0.0001,
verbose=0, warm_start=False)

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.

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights=’uniform’)

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

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=1, p=2,
weights=’uniform’)

1.
2.

1.0

ii. Splitting into train/test


1.

(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.

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights=’uniform’)

1.
2.

0.9666666666666667

1.
2.
3.
4.
5.
6.
7.
8.

[0.95, 0.95, 0.9666666666666667, 0.9666666666666667, 0.9666666666666667,


0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9666666666666667, 0.9833333333333333, 0.9666666666666667,
0.9666666666666667, 0.9666666666666667, 0.9666666666666667, 0.95, 0.95]
1.
2.

[<matplotlib.lines.Line2D object at 0x05FDECD0>]


https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 14/16
6/25/2019 8 Machine Learning Algorithms in Python - You Must Learn - DataFlair

1.

Text(0.5,0,’k for kNN’)

1.

Text(0,0.5,’Testing Accuracy’)

1.

Machine Learning Algorithms in Python – k-Nearest Neighbors

Read about Python Statistics – p-Value, Correlation, T-test, KS Test

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.

<matplotlib.collections.PathCollection object at 0x0642AF30>

1.
2.
3.

KMeans(algorithm=’auto’, copy_x=True, init=’k-means++’, max_iter=300,


n_clusters=2, n_init=10, n_jobs=1, precompute_distances=’auto’,
random_state=None, tol=0.0001, verbose=0)

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.

<matplotlib.collections.PathCollection object at 0x043B14D0>

1.

https://data-flair.training/blogs/machine-learning-algorithms-in-python/ 16/16

You might also like