You are on page 1of 4

from sklearn.

tree import DecisionTreeClassifier # Import Decision Tree


Classifier
from sklearn.model_selection import train_test_split # Import
train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for
accuracy calculation

import pandas as pd
df=pd.read_csv("play_tennis.csv")
df

day outlook temp humidity wind play


0 D1 Sunny Hot High Weak No
1 D2 Sunny Hot High Strong No
2 D3 Overcast Hot High Weak Yes
3 D4 Rain Mild High Weak Yes
4 D5 Rain Cool Normal Weak Yes
5 D6 Rain Cool Normal Strong No
6 D7 Overcast Cool Normal Strong Yes
7 D8 Sunny Mild High Weak No
8 D9 Sunny Cool Normal Weak Yes
9 D10 Rain Mild Normal Weak Yes
10 D11 Sunny Mild Normal Strong Yes
11 D12 Overcast Mild High Strong Yes
12 D13 Overcast Hot Normal Weak Yes
13 D14 Rain Mild High Strong No

df.describe()

day outlook temp humidity wind play


count 14 14 14 14 14 14
unique 14 3 3 2 2 2
top D1 Sunny Mild High Weak Yes
freq 1 5 6 7 8 9

from sklearn import preprocessing


str_int=preprocessing.LabelEncoder()
df=df.apply(str_int.fit_transform)
df

day outlook temp humidity wind play


0 0 2 1 0 1 0
1 6 2 1 0 0 0
2 7 0 1 0 1 1
3 8 1 2 0 1 1
4 9 1 0 1 1 1
5 10 1 0 1 0 0
6 11 0 0 1 0 1
7 12 2 2 0 1 0
8 13 2 0 1 1 1
9 1 1 2 1 1 1
10 2 2 2 1 0 1
11 3 0 2 0 0 1
12 4 0 1 1 1 1
13 5 1 2 0 0 0

del df['day']
df

----------------------------------------------------------------------
-----
KeyError Traceback (most recent call
last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in
get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:

~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in
pandas._libs.index.IndexEngine.get_loc()

~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in
pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in
pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in
pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'day'

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call


last)
~\AppData\Local\Temp/ipykernel_1992/1236143950.py in <module>
----> 1 del df['day']
2 df

~\anaconda3\lib\site-packages\pandas\core\generic.py in
__delitem__(self, key)
3961 # there was no match, this call should raise the
appropriate
3962 # exception:
-> 3963 loc = self.axes[-1].get_loc(key)
3964 self._mgr = self._mgr.idelete(loc)
3965

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in
get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'day'

feature_cols=df.columns[1:-1]
feature_cols

Index(['temp', 'humidity', 'wind'], dtype='object')

df.shape

(14, 6)

b=df.columns[:-1]

df.head()

outlook temp humidity wind play


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

a=df.columns[-1:]
a

Index(['play'], dtype='object')

x = df.drop(a,axis=1)
y = df[a]

from sklearn.model_selection import train_test_split


x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.20)

from sklearn.tree import DecisionTreeClassifier


classifier = DecisionTreeClassifier()
classifier.fit(x_train, y_train)

DecisionTreeClassifier()

y_pred = classifier.predict(x_test)

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
[[1 0]
[1 1]]
precision recall f1-score support

0 0.50 1.00 0.67 1


1 1.00 0.50 0.67 2

accuracy 0.67 3
macro avg 0.75 0.75 0.67 3
weighted avg 0.83 0.67 0.67 3

# Create Decision Tree classifier object


clf = DecisionTreeClassifier()
# Train Decision Tree Classifier
clf = clf.fit(x_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(x_test)

print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

Accuracy: 0.6666666666666666

You might also like