Practice+for+Lesson+5 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Practices for Lesson 5:

Python for Machine Learning

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
1 Practices for Lesson 5: Python for Machine Learning
Practices for Lesson 5
Overview
In these practices for this lesson, you will learn to implement color quantization using k-means,
develop ML application using linear regression mechanism, predict hand written single digit
number and to create a decision tree classifier.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
2 Practices for Lesson 5: Python for Machine Learning
Practice of 5-4: Create a Decision Surface and Decision Tree
Overview
In this practice, you will learn to create a decision surface and decision tree using iris dataset
(Iris is the name of a flower) with respect to sepal and petal size.

Tasks:
1. Open your Jupyter Notebook and save the file as decision_tree.
2. Copy the below code and paste it in Jupyter Notebook cell to import necessary libraries
as shown in the below screenshot. Click Run icon.

%matplotlib inline
# import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree

3. Copy the below code and paste it in Jupyter Notebook cell to load the iris flower dataset
as shown in the below screenshot. Click Run icon.

# Load datasets
n_classes = 3
plot_colors = "ryb"
plot_step = 0.02
iris_dataset = load_iris()

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
3 Practices for Lesson 5: Python for Machine Learning
4. Copy the below code and paste it in Jupyter Notebook cell to plot the decision surface as
shown in the below screenshot. Click Run icon.

for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],


[1, 2], [1, 3], [2, 3]]):
# We only take the two corresponding features
X = iris_dataset.data[:, pair]
y = iris_dataset.target

# Train
Train = DecisionTreeClassifier().fit(X, y)

# Plot the decision boundary


plt.subplot(2, 3, pairidx + 1)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1


y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)

Z = Train.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)

plt.xlabel(iris_dataset.feature_names[pair[0]])
plt.ylabel(iris_dataset.feature_names[pair[1]])

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
4 Practices for Lesson 5: Python for Machine Learning
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color,
label=iris_dataset.target_names[i],
cmap=plt.cm.RdYlBu, edgecolor='black', s=15)
plt.suptitle("Decision surface of a decision tree using paired
features")
plt.legend(loc='lower right', borderpad=0, handletextpad=0)
plt.axis("tight")

5. On successful execution, view the decision surface displayed below your code cell as
shown below.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
5 Practices for Lesson 5: Python for Machine Learning
6. Copy the below code and paste it in Jupyter Notebook cell to plot the decision tree
classifier as shown in the below screenshot. Click Run icon.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
6 Practices for Lesson 5: Python for Machine Learning
plt.figure()
clf = DecisionTreeClassifier().fit(iris_dataset.data,
iris_dataset.target)
plot_tree(clf, filled=True)
plt.show()

7. On successful execution, view the decision tree displayed below your code cell as
shown below.

8. Close the Jupyter Notebook.

Copyright © 2020, Proton Expert Systems & Solutions. All rights reserved
7 Practices for Lesson 5: Python for Machine Learning

You might also like