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

DecisionTree COM

This document shows how to build a decision tree classifier to predict whether someone will play tennis based on weather conditions. It loads and preprocesses the data, splits it into training and test sets, trains decision tree models using different criteria, and evaluates their performance on the test set, achieving an accuracy of 80%.

Uploaded by

Mansi Varshney
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)
7 views4 pages

DecisionTree COM

This document shows how to build a decision tree classifier to predict whether someone will play tennis based on weather conditions. It loads and preprocesses the data, splits it into training and test sets, trains decision tree models using different criteria, and evaluates their performance on the test set, achieving an accuracy of 80%.

Uploaded by

Mansi Varshney
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

5/2/24, 7:38 PM DecisionTree (3)

In [1]: import pandas as pd


from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder

In [2]: data=pd.read_csv("PlayTennis.csv")
print("-----------Input data-----------",data)

-----------Input data----------- Outlook Temperature Humidity Wind Play Te


nnis
0 Sunny Hot High Weak No
1 Sunny Hot High Strong No
2 Overcast Hot High Weak Yes
3 Rain Mild High Weak Yes
4 Rain Cool Normal Weak Yes
5 Rain Cool Normal Strong No
6 Overcast Cool Normal Strong Yes
7 Sunny Mild High Weak No
8 Sunny Cool Normal Weak Yes
9 Rain Mild Normal Weak Yes
10 Sunny Mild Normal Strong Yes
11 Overcast Mild High Strong Yes
12 Overcast Hot Normal Weak Yes
13 Rain Mild High Strong No

In [3]: x=data.iloc[:,:-1]
y=data.iloc[:,-1]

In [4]: le_outlook=LabelEncoder()
x.Outlook=le_outlook.fit_transform(x.Outlook)

le_Temperature=LabelEncoder()
x.Temperature=le_Temperature.fit_transform(x.Temperature)

le_Humidity=LabelEncoder()
x.Humidity=le_Humidity.fit_transform(x.Humidity)

le_Wind = LabelEncoder()
x.Wind = le_Wind.fit_transform(x.Wind)
print("\nNow the Train data is----\n\n",x.head())

Now the Train data is----

Outlook Temperature Humidity Wind


0 2 1 0 1
1 2 1 0 0
2 0 1 0 1
3 1 2 0 1
4 1 0 1 1

localhost:8888/nbconvert/html/DecisionTree (3).ipynb?download=false 1/4


5/2/24, 7:38 PM DecisionTree (3)

C:\Users\hp\AppData\Local\Temp\ipykernel_10104\2199725814.py:2: SettingWithCopyWar
ning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stabl


e/user_guide/indexing.html#returning-a-view-versus-a-copy
x.Outlook=le_outlook.fit_transform(x.Outlook)
C:\Users\hp\AppData\Local\Temp\ipykernel_10104\2199725814.py:5: SettingWithCopyWar
ning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stabl


e/user_guide/indexing.html#returning-a-view-versus-a-copy
x.Temperature=le_Temperature.fit_transform(x.Temperature)
C:\Users\hp\AppData\Local\Temp\ipykernel_10104\2199725814.py:8: SettingWithCopyWar
ning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stabl


e/user_guide/indexing.html#returning-a-view-versus-a-copy
x.Humidity=le_Humidity.fit_transform(x.Humidity)
C:\Users\hp\AppData\Local\Temp\ipykernel_10104\2199725814.py:11: SettingWithCopyWa
rning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stabl


e/user_guide/indexing.html#returning-a-view-versus-a-copy
x.Wind = le_Wind.fit_transform(x.Wind)

In [5]: x_train,x_test,y_train,y_test=train_test_split(x,y, test_size=0.3,random_state=142)

In [10]: # Ginni method


model=DecisionTreeClassifier()
model=model.fit(x_train,y_train)
y_train_pred= model.predict(x_train)
y_test_pred= model.predict(x_test)

In [11]: print("Train Accuracy", metrics.accuracy_score(y_train,y_train_pred))


print("Test Accuracy", metrics.accuracy_score(y_test,y_test_pred))

Train Accuracy 1.0


Test Accuracy 0.8

In [12]: from sklearn.tree import plot_tree


import matplotlib.pyplot as plt

plt.figure(figsize=(20,10))
plot_tree(model, filled=True, rounded=True, feature_names=['Outlook', 'Temperature'
plt.savefig('PlayTennis.png')
plt.show()

localhost:8888/nbconvert/html/DecisionTree (3).ipynb?download=false 2/4


5/2/24, 7:38 PM DecisionTree (3)

In [14]: # Enrtropy Method


model=DecisionTreeClassifier(criterion='entropy')
model=model.fit(x_train,y_train)
y_train_pred= model.predict(x_train)
y_test_pred= model.predict(x_test)

In [15]: print("Train Accuracy", metrics.accuracy_score(y_train,y_train_pred))


print("Test Accuracy", metrics.accuracy_score(y_test,y_test_pred))

Train Accuracy 1.0


Test Accuracy 0.8

In [16]: from sklearn.tree import plot_tree


import matplotlib.pyplot as plt

plt.figure(figsize=(20,10))
plot_tree(model, filled=True, rounded=True, feature_names=['Outlook', 'Temperature'
plt.savefig('PlayTennis.png')
plt.show()

In [17]: #Test Accuracy


from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB

classifier = GaussianNB()
localhost:8888/nbconvert/html/DecisionTree (3).ipynb?download=false 3/4
5/2/24, 7:38 PM DecisionTree (3)
classifier.fit(x_train,y_train)
print("Accuracy is:", accuracy_score(classifier.predict(x_test),y_test))

Accuracy is: 1.0

localhost:8888/nbconvert/html/DecisionTree (3).ipynb?download=false 4/4

You might also like