0% found this document useful (0 votes)
0 views

456 ML Lab

The document provides Python code examples demonstrating the Decision Tree Algorithm for classification and regression using the Iris dataset and a synthetic regression dataset, respectively. It includes parameter tuning with Grid Search for improved accuracy in classification, achieving 100% accuracy with the best parameters identified. Additionally, it showcases the Naïve Bayes Classification algorithm applied to the Iris dataset, also achieving 100% accuracy.

Uploaded by

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

456 ML Lab

The document provides Python code examples demonstrating the Decision Tree Algorithm for classification and regression using the Iris dataset and a synthetic regression dataset, respectively. It includes parameter tuning with Grid Search for improved accuracy in classification, achieving 100% accuracy with the best parameters identified. Additionally, it showcases the Naïve Bayes Classification algorithm applied to the Iris dataset, also achieving 100% accuracy.

Uploaded by

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

Here’s a Python program to demonstrate the Decision Tree Algorithm for a

classification problem using the Iris dataset. The program also includes parameter
tuning using Grid Search for better results.

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.tree import DecisionTreeClassifier,plot_tree
from sklearn.metrics import accuracy_score,classification_report
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data
y = iris.target

X_train,X_test,y_train,y_test=train_test_split(
X,y,test_size=0.3,random_state=42
)
dt_classifier = DecisionTreeClassifier(random_state=42)

dt_classifier.fit(X_train,y_train)

y_pred=dt_classifier.predict(X_test)

accuracy = accuracy_score(y_test,y_pred)
print("Decision Tree Classification Results(Default Parameters):")
print(f"Accuracy:{accuracy * 100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test,y_pred))

plt.figure(figsize=(15,10))
plot_tree(dt_classifier,filled=
True,feature_names=iris.feature_names,class_names=iris.target_names)
plt.title("Decision Tree Visualization")
plt.show()

param_grid={
"criterion":["gini","entropy"],
"max_depth":[None,3,5,10],
"min_samples_split":[2,5,10],
"min_samples_leaf":[1,2,4],
}

grid_search = GridSearchCV(estimator=dt_classifier, param_grid=param_grid, cv=5,


scoring='accuracy')

Footer 1
grid_search.fit(X_train, y_train)

best_params = grid_search.best_params_
best_model = grid_search.best_estimator_
y_pred_tuned = best_model.predict(X_test)

accuracy_tuned = accuracy_score(y_test,y_pred_tuned)
print("\nDecision Tree Classification Results(Tuned Parameters):")
print(f"Accuracy:{accuracy_tuned * 100:.2f}%")
print(f"Best Parameters: {best_params}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred_tuned))
plt.figure(figsize=(15, 10))
plot_tree(best_model, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names) # Used best_model here
plt.title("Tuned Decision Tree Visualization")
plt.show()

OUTPUT:
Decision Tree Classification Results(Default Parameters):
Accuracy:100.00%

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

Footer 2
Decision Tree Classification Results(Tuned Parameters):
Accuracy:100.00%
Best Parameters: {'criterion': 'gini', 'max_depth': None, 'min_samples_leaf': 1,
'min_samples_split': 10}

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

Footer 3
Footer 4
5.Here’s an example of using the Decision Tree algorithm for regression in Python. We'll
use a synthetic regression dataset and evaluate the model's performance based on
metrics such as Mean Squared Error (MSE) and R² score.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor, plot_tree
from sklearn.metrics import mean_squared_error, r2_score
X, y = make_regression(n_samples=200, n_features=1, noise=15, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
dt_regressor = DecisionTreeRegressor(random_state=42)
dt_regressor.fit(X_train, y_train)
y_pred = dt_regressor.predict(X_test)\
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("Decision Tree Regression Results:")
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R² Score: {r2:.2f}")
plt.figure(figsize=(12, 8))
plot_tree(dt_regressor, filled=True, feature_names=["Feature"], rounded=True)
plt.title("Decision Tree Visualization")
plt.show()
plt.figure(figsize=(8, 6))
plt.scatter(X_test, y_test, color="blue", label="Actual Values")
plt.scatter(X_test, y_pred, color="red", label="Predicted Values")
plt.title("Decision Tree Regression: Predictions vs Actual Values")
plt.xlabel("Feature")
plt.ylabel("Target")
plt.legend()
plt.show()

OUTPUT:

Decision Tree Regression Results:


Mean Squared Error (MSE): 527.88
R² Score: 0.94

Footer 5
Footer 6
6. Here’s a demonstration of the Naïve Bayes Classification algorithm using Python.
We'll
use the Gaussian Naïve Bayes model from sklearn and apply it to the Iris dataset to
classify different species of flowers.

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)
y_pred = nb_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=iris.target_names)
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:\n", report)

OUTPUT:
Accuracy: 1.00
Classification Report:
precision recall f1-score
support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

Footer 7

You might also like