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

4 Decision Tree - Jupyter Notebook

This document loads and prepares iris data from a CSV file. It splits the data into training and test sets, extracts the feature and target variables, and fits a decision tree classifier to make predictions on the test set. It calculates accuracy on the test set, which is about 94.67%.

Uploaded by

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

4 Decision Tree - Jupyter Notebook

This document loads and prepares iris data from a CSV file. It splits the data into training and test sets, extracts the feature and target variables, and fits a decision tree classifier to make predictions on the test set. It calculates accuracy on the test set, which is about 94.67%.

Uploaded by

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

In 

[18]: import numpy as np


import matplotlib.pyplot as plt
import pandas as pd

In [19]: iris = pd.read_csv("D:\\Course\\Python\\Datasets\\iris.csv")


iris

...

In [20]: iris_setosa = iris.iloc[0:50,0:5]


iris_versicolor = iris.iloc[50:100,0:5]
iris_vergenica = iris.iloc[100:150,0:5]

In [21]: df1 = pd.DataFrame(iris_setosa.iloc[0:25,0:5])


df2 = pd.DataFrame(iris_versicolor.iloc[0:25,0:5])
df3 = pd.DataFrame(iris_vergenica.iloc[0:25,0:5])

In [22]: iris_train = pd.concat([df1,df2,df3])


iris_train

...

In [23]: # Dividing the input and output variable for Training Dataset

X_train = iris_train.iloc[:,0:4]
X_train
y_train = iris_train.iloc[:,4]
y_train

...

In [24]: df1 = pd.DataFrame(iris_setosa.iloc[25:50,0:5])


df2 = pd.DataFrame(iris_versicolor.iloc[25:50,0:5])
df3 = pd.DataFrame(iris_vergenica.iloc[25:50,0:5])

In [25]: iris_test= pd.concat([df1,df2,df3])


iris_test

...

In [26]: X_test = iris_test.iloc[:,0:4]


X_test
y_test = iris_test.iloc[:,4]
y_test
...

In [9]: # X_train and X_test are the dataset for training and Testing
#y_train ,y_test are target predictor for Training and Testing Dataset
In [27]: # Fitting Decision Tree Classification to the Training set
# step 1 :import libraries and methods
from sklearn.tree import DecisionTreeClassifier
# step 2 : Create an alogrithm using imported methods
classifier = DecisionTreeClassifier(criterion = 'entropy')
# Step3 : applying algorithm on training dataset ( Xtrain , ytrain)
classifier.fit(X_train, y_train)
...

In [11]: # Predicting the Test set results by applying the algorithm only in input
#variables of testing dataset (xtest)
y_pred = classifier.predict(X_test)
#y_pred

In [12]: # Making the Confusion Matrix


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm

...

In [13]: from sklearn.metrics import accuracy_score


Accuracy_Score = accuracy_score(y_test, y_pred)

In [14]: Accuracy_Score

Out[14]: 0.9466666666666667

In [ ]: ​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy