Decision Tree play tennis
#numpy and pandas initialization
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.metrics import accuracy_score
import pandas as pd
# Read the data from the CSV file
data = pd.read_csv("tennis.csv")
# Encode categorical variables into numerical values
label_encoder = LabelEncoder()
data_encoded = data.copy()
for column in ['Outlook', 'Temperature', 'Humidity', 'Wind', 'Play Tennis']:
data_encoded[column] = label_encoder.fit_transform(data[column])
# Split the data into features (X) and the target variable (y)
x = data_encoded.drop('Play Tennis', axis=1)
y = data_encoded['Play Tennis']
# Split the data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=1)
# Create Decision Tree classifer object
clf = DecisionTreeClassifier(criterion = 'entropy' , max_depth = 3)
# Train Decision Tree Classifer
clf = clf.fit(x_train , y_train)
#Predict the response for test dataset
y_pred = clf.predict(x_test)
# Calculate and print the accuracy of the model on the testing data
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy on the testing data:",accuracy*100)
# from sklearn.preprocessing import LabelEncoder
# Le = LabelEncoder()
#
# PlayTennis['outlook'] = Le.fit_transform(PlayTennis['outlook'])
# PlayTennis['temp'] = Le.fit_transform(PlayTennis['temp'])
# PlayTennis['humidity'] = Le.fit_transform(PlayTennis['humidity'])
# PlayTennis['windy'] = Le.fit_transform(PlayTennis['wind'])
# PlayTennis['play'] = Le.fit_transform(PlayTennis['play'])
#
# PlayTennis
#
# y = PlayTennis['play']
# X = PlayTennis.drop(['play'],axis=1)
# # Fitting the model
#
# clf = DecisionTreeClassifier(criterion = 'entropy', max_depth = 5)
# clf = clf.fit(X, y)
#
# #Predict the response for test dataset
# y_pred = clf.predict(x_test)
#
# # Model Accuracy
# print("Accuracy:" , metrics.accuracy_score(y_test, y_pred)*100)
Comments
Post a Comment