0% found this document useful (0 votes)
13 views3 pages

ML7 - Colab

The document outlines an experiment using the Iris dataset to train a Decision Tree Classifier. It includes steps for importing libraries, loading the dataset, splitting it into training and testing sets, and evaluating the model's performance using accuracy, precision, recall, and F1-score metrics. The Decision Tree Classifier achieved an accuracy of approximately 95.56%.

Uploaded by

kulkarnics97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

ML7 - Colab

The document outlines an experiment using the Iris dataset to train a Decision Tree Classifier. It includes steps for importing libraries, loading the dataset, splitting it into training and testing sets, and evaluating the model's performance using accuracy, precision, recall, and F1-score metrics. The Decision Tree Classifier achieved an accuracy of approximately 95.56%.

Uploaded by

kulkarnics97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

4/1/25, 9:52 ML7 -

AM Colab

 Experiment 7
1. Importing Libraries

import numpy as
np import pandas
as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score,
f1_score from sklearn import datasets
from sklearn import svm
from sklearn.tree import
DecisionTreeClassifier from
sklearn.naive_bayes import GaussianNB

2. Loading Dataset

iris =
datasets.load_iris() x =
iris.data
y = iris.target
print(f"x = {x}")
print (f"y = {y}")

x = [[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
[5.7 3.8 1.7 0.3]
[5.1 3.8 1.5 0.3]
[5.4 3.4 1.7 0.2]
[5.1 3.7 1.5 0.4]
[4.6 3.6 1. 0.2]
[5.1 3.3 1.7 0.5]
[4.8 3.4 1.9 0.2]
[5. 3. 1.6 0.2]
[5. 3.4 1.6 0.4]
[5.2 3.5 1.5 0.2]
[5.2 3.4 1.4 0.2]
[4.7 3.2 1.6 0.2]
[4.8 3.1 1.6 0.2]
[5.4 3.4 1.5 0.4]
[5.2 4.1 1.5 0.1]
[5.5 4.2 1.4 0.2]
[4.9 3.1 1.5 0.2]
[5. 3.2 1.2 0.2]
[5.5 3.5 1.3 0.2]
[4.9 3.6 1.4 0.1]
[4.4 3. 1.3 0.2]
[5.1 3.4 1.5 0.2]
[5. 3.5 1.3 0.3]
[4.5 2.3 1.3 0.3]
[4.4 3.2 1.3 0.2]
[5. 3.5 1.6 0.6]
[5.1 3.8 1.9 0.4]
[4.8 3. 1.4 0.3]
[5.1 3.8 1.6 0.2]
[4.6 3.2 1.4 0.2]
[5.3 3.7 1.5 0.2]
[5. 3.3 1.4 0.2]
[7. 3.2 4.7 1.4]
[6.4 3.2 4.5 1.5]
[6.9 3.1 4.9 1.5]
https://colab.research.google.com/drive/ 1/
1G7MMxGlH5P_Sbh2Gfv_EzVPfrjx6xzzg#scrollTo=3dsBYzcWlGXX&printMode=true 3
4/1/25, 9:52 ML7 -
AM [5.5 2.3 4. 1.3] Colab
[6.5 2.8 4.6 1.5]

https://colab.research.google.com/drive/ 2/
1G7MMxGlH5P_Sbh2Gfv_EzVPfrjx6xzzg#scrollTo=3dsBYzcWlGXX&printMode=true 3
4/1/25, 9:52 ML7 -
AM [5.7 2.8 4.5 1.3] Colab 
[6.3 3.3 4.7 1.6]
[4.9 2.4 3.3 1. ]

3. Splitting the dataset

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1)

4. Decision Tree Classifier

dt = DecisionTreeClassifier(random_state=0) #train the model


dt.fit(x_train, y_train)
# make predictions
dt_pred = dt.predict(x_test)
#print the accuracy
print("---Decision Tree Classifier---")
print(f"Accuracy of Decision Tree Classifier: {accuracy_score (y_test,
dt_pred)}") #print other performance metrics
print(f"Precision of Decision Tree Classifier: {precision_score(y_test, dt_pred,
average='weighted')}") print (f"Recall of Decision Tree Classifier: {recall_score(y_test,
dt_pred, average='weighted')}")
print (f"F1-Score of Decision Tree Classifier: {f1_score(y_test, dt_pred, average='weighted')}")

---Decision Tree Classifier---


Accuracy of Decision Tree Classifier: 0.9555555555555556
Precision of Decision Tree Classifier:
0.9555555555555556 Recall of Decision Tree Classifier:
0.9555555555555556
F1-Score of Decision Tree Classifier: 0.9555555555555556

https://colab.research.google.com/drive/ 3/
1G7MMxGlH5P_Sbh2Gfv_EzVPfrjx6xzzg#scrollTo=3dsBYzcWlGXX&printMode=true 3

You might also like