0% found this document useful (0 votes)
39 views4 pages

19mid0034 (Chandru) - ML Lab Fat - Jupyter Notebook

This document details the steps taken to analyze the iris dataset using a decision tree classifier in Python. It loads and explores the dataset, splits it into training and test sets, builds a decision tree model, and evaluates the model's performance using various metrics.

Uploaded by

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

19mid0034 (Chandru) - ML Lab Fat - Jupyter Notebook

This document details the steps taken to analyze the iris dataset using a decision tree classifier in Python. It loads and explores the dataset, splits it into training and test sets, builds a decision tree model, and evaluates the model's performance using various metrics.

Uploaded by

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

12/9/21, 1:22 PM 19MID0034(CHANDRU)-ML LAB FAT - Jupyter Notebook

In [83]: # importing libraries


print("CHANDRU\n19MID0034")
import numpy as nm
import matplotlib.pyplot as mtp
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

CHANDRU

19MID0034

In [104]: #importing datasets


data = pd.read_csv('iris.csv')

In [106]: data.head()

Out[106]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) species

0 5.1 3.5 1.4 0.2 0

1 4.9 3.0 1.4 0.2 0

2 4.7 3.2 1.3 0.2 0

3 4.6 3.1 1.5 0.2 0

4 5.0 3.6 1.4 0.2 0

In [107]: data.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 150 entries, 0 to 149

Data columns (total 5 columns):

sepal length (cm) 150 non-null float64

sepal width (cm) 150 non-null float64

petal length (cm) 150 non-null float64

petal width (cm) 150 non-null float64

species 150 non-null int64

dtypes: float64(4), int64(1)

memory usage: 5.9 KB

In [108]: data.shape

Out[108]: (150, 5)

In [109]: data.columns

Out[109]: Index(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',

'petal width (cm)', 'species'],

dtype='object')

localhost:8888/notebooks/19MID0034(CHANDRU)-ML LAB FAT.ipynb 1/4


12/9/21, 1:22 PM 19MID0034(CHANDRU)-ML LAB FAT - Jupyter Notebook

In [110]: #SPLITTING DATASETS IN FEAUTURES AND TARGET VARIABLES


cols = ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal widt
X = data[cols]
y = data.species

In [111]: #TRAINING AND TESTING THE DATA


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_s

In [112]: #BUILDING THE DECISION TREE MODEL


from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)

In [113]: #EVALUATING THE MODEL


accuracy_clf = clf.score(X_test, y_test)
print("THE ACCURACY IS :",accuracy_clf)

THE ACCURACY IS : 0.9777777777777777

In [114]: #PRECESION, RECALL, ACCURACY, F1 SCORE, SUPPORT


from sklearn.metrics import classification_report
classification_report(y_test, y_pred)

Out[114]: ' precision recall f1-score support\n\n 0 1.


00 1.00 1.00 18\n 1 0.90 1.00 0.95
9\n 2 1.00 0.94 0.97 18\n\n accuracy
0.98 45\n macro avg 0.97 0.98 0.97 45\nweighted
avg 0.98 0.98 0.98 45\n'

In [115]: #CONFUSION MATRIX


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

Out[115]: array([[18, 0, 0],

[ 0, 9, 0],

[ 0, 1, 17]], dtype=int64)

In [116]: #ERROR RATE


print("THE ERROR RATE FOR DECISION TREE IS : ",1-accuracy_clf)

THE ERROR RATE FOR DECISION TREE IS : 0.022222222222222254

localhost:8888/notebooks/19MID0034(CHANDRU)-ML LAB FAT.ipynb 2/4


12/9/21, 1:22 PM 19MID0034(CHANDRU)-ML LAB FAT - Jupyter Notebook

In [117]: sns.pairplot(data)

Out[117]: <seaborn.axisgrid.PairGrid at 0x246e4997a58>

localhost:8888/notebooks/19MID0034(CHANDRU)-ML LAB FAT.ipynb 3/4


12/9/21, 1:22 PM 19MID0034(CHANDRU)-ML LAB FAT - Jupyter Notebook

In [118]: sns.heatmap(data.corr(),annot=True)

Out[118]: <matplotlib.axes._subplots.AxesSubplot at 0x246e6518c18>

In [ ]: ​

localhost:8888/notebooks/19MID0034(CHANDRU)-ML LAB FAT.ipynb 4/4

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