0% found this document useful (0 votes)
4 views1 page

Classification - DecisionTreeClassifier

The document outlines a data analysis process using the Iris dataset, including importing libraries, reading the dataset, and performing exploratory data analysis. It details the steps of label encoding, splitting the data into training and testing sets, and training a Decision Tree Classifier model. The model achieved perfect accuracy on the test set, and visualizations of the confusion matrix and decision tree are included.

Uploaded by

backup11424
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)
4 views1 page

Classification - DecisionTreeClassifier

The document outlines a data analysis process using the Iris dataset, including importing libraries, reading the dataset, and performing exploratory data analysis. It details the steps of label encoding, splitting the data into training and testing sets, and training a Decision Tree Classifier model. The model achieved perfect accuracy on the test set, and visualizations of the confusion matrix and decision tree are included.

Uploaded by

backup11424
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

Import Libraries

In [1]: import numpy as np


import pandas as pd
import [Link] as plt
import seaborn as sns

Read Dataset
In [2]: df=pd.read_csv("E:\Tanisha Bangar\Documents\Data Science\[Link]")

Analysis of the Dataset


In [3]: [Link]()

Out[3]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

In [4]: [Link]()

Out[4]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

In [5]: [Link](20)

Out[5]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

149 150 5.9 3.0 5.1 1.8 Iris-virginica

126 127 6.2 2.8 4.8 1.8 Iris-virginica

58 59 6.6 2.9 4.6 1.3 Iris-versicolor

42 43 4.4 3.2 1.3 0.2 Iris-setosa

6 7 4.6 3.4 1.4 0.3 Iris-setosa

122 123 7.7 2.8 6.7 2.0 Iris-virginica

18 19 5.7 3.8 1.7 0.3 Iris-setosa

103 104 6.3 2.9 5.6 1.8 Iris-virginica

41 42 4.5 2.3 1.3 0.3 Iris-setosa

72 73 6.3 2.5 4.9 1.5 Iris-versicolor

137 138 6.4 3.1 5.5 1.8 Iris-virginica

55 56 5.7 2.8 4.5 1.3 Iris-versicolor

1 2 4.9 3.0 1.4 0.2 Iris-setosa

135 136 7.7 3.0 6.1 2.3 Iris-virginica

133 134 6.3 2.8 5.1 1.5 Iris-virginica

144 145 6.7 3.3 5.7 2.5 Iris-virginica

128 129 6.4 2.8 5.6 2.1 Iris-virginica

39 40 5.1 3.4 1.5 0.2 Iris-setosa

107 108 7.3 2.9 6.3 1.8 Iris-virginica

97 98 6.2 2.9 4.3 1.3 Iris-versicolor

In [6]: [Link]()

<class '[Link]'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Id 150 non-null int64
1 SepalLengthCm 150 non-null float64
2 SepalWidthCm 150 non-null float64
3 PetalLengthCm 150 non-null float64
4 PetalWidthCm 150 non-null float64
5 Species 150 non-null object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB

In [7]: [Link]()

Out[7]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

count 150.000000 150.000000 150.000000 150.000000 150.000000

mean 75.500000 5.843333 3.054000 3.758667 1.198667

std 43.445368 0.828066 0.433594 1.764420 0.763161

min 1.000000 4.300000 2.000000 1.000000 0.100000

25% 38.250000 5.100000 2.800000 1.600000 0.300000

50% 75.500000 5.800000 3.000000 4.350000 1.300000

75% 112.750000 6.400000 3.300000 5.100000 1.800000

max 150.000000 7.900000 4.400000 6.900000 2.500000

In [8]: [Link]().sum()

Id 0
Out[8]:
SepalLengthCm 0
SepalWidthCm 0
PetalLengthCm 0
PetalWidthCm 0
Species 0
dtype: int64

In [9]: [Link]

Index(['Id', 'SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm',


Out[9]:
'Species'],
dtype='object')

In [10]: [Link]()

Id 150
Out[10]:
SepalLengthCm 35
SepalWidthCm 23
PetalLengthCm 43
PetalWidthCm 22
Species 3
dtype: int64

Label Encoding of Categorical Column


In [15]: from [Link] import LabelEncoder
le=LabelEncoder()

In [16]: df['Species'].value_counts()

Iris-setosa 50
Out[16]:
Iris-versicolor 50
Iris-virginica 50
Name: Species, dtype: int64

In [18]: df['Species']=le.fit_transform(df['Species'])

In [19]: df['Species'].value_counts()

0 50
Out[19]:
1 50
2 50
Name: Species, dtype: int64

In [20]: df1=df[df["Species"]!=2]

In [21]: df1['Species'].value_counts()

0 50
Out[21]:
1 50
Name: Species, dtype: int64

Defining Dependent and Independent variable


In [22]: [Link](2)

Out[22]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 0

1 2 4.9 3.0 1.4 0.2 0

In [24]: x=[Link](['Id','Species'],axis=1)
y=df1['Species']

In [25]: [Link],[Link],type(x),type(y)

((100, 4), (100,), [Link], [Link])


Out[25]:

Split x and y into train and test Dataset


In [26]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.25)
x_train.shape,x_test.shape,y_train.shape,y_test.shape

((75, 4), (25, 4), (75,), (25,))


Out[26]:

Import Model / Algorithm


In [29]: from [Link] import DecisionTreeClassifier
dtc=DecisionTreeClassifier()
dtc

DecisionTreeClassifier()
Out[29]:

Train Model
In [30]: [Link](x_train,y_train)

DecisionTreeClassifier()
Out[30]:

Prediction
In [31]: y_pred=[Link](x_test)
y_pred

array([0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1,
Out[31]:
1, 1, 0])

In [32]: print(f'y_predict{y_pred}y_test{y_test.values}')

y_predict[0 0 1 1 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 1 0]y_test[0 0 1 1 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 1 0]

Evalution
In [33]: [Link](x_test,y_test)

1.0
Out[33]:

In [34]: from [Link] import confusion_matrix,accuracy_score,classification_report

In [35]: cm=confusion_matrix(y_test,y_pred)
cm

array([[12, 0],
Out[35]:
[ 0, 13]], dtype=int64)

In [36]: print(classification_report(y_pred,y_test))

precision recall f1-score support

0 1.00 1.00 1.00 12


1 1.00 1.00 1.00 13

accuracy 1.00 25
macro avg 1.00 1.00 1.00 25
weighted avg 1.00 1.00 1.00 25

In [37]: print(accuracy_score(y_pred,y_test))

1.0

In [38]: [Link](x_test,y_test)

1.0
Out[38]:

In [40]: [Link](figsize=(10,2))
[Link](cm,annot=True)

<AxesSubplot:>
Out[40]:

In [42]: from sklearn import tree


dft=pd.read_csv("E:\Tanisha Bangar\Documents\Data Science\[Link]")
tree.plot_tree(dtc, feature_names=[Link],class_names=dft['Species'],filled=True)
[Link]()

In [43]: tree.plot_tree(dtc,feature_names=[Link])
[Link]()

In [ ]:

You might also like