0% found this document useful (0 votes)
4 views

Decision Tree

Decision tree dwm

Uploaded by

god914166
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Decision Tree

Decision tree dwm

Uploaded by

god914166
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, accuracy_score
from sklearn import tree
import matplotlib.pyplot as plt

def load_sample_data():
data = {
'Day': ['D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11', 'D12',
'D13', 'D14'],
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast',
'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temp': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool',
'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal',
'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong',
'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'PlayTennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
return df

# Function to preprocess data (encoding categorical variables)


def preprocess_data(df):
# Converting categorical columns to numerical values using one-hot
encoding
df_encoded = pd.get_dummies(df.drop(columns=['PlayTennis']),
drop_first=True)
target = df['PlayTennis'].apply(lambda x: 1 if x == 'Yes' else 0) # Encoding
target as binary
return df_encoded, target

# Function to train and evaluate the Decision Tree Classifier


def decision_tree_classifier(X, y):
# Splitting data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)

# Initializing and training the Decision Tree Classifier


clf = DecisionTreeClassifier(criterion='entropy', random_state=42)
clf.fit(X_train, y_train)

# Predicting the results on the test set


y_pred = clf.predict(X_test)

# Evaluating the model


print("Classification Report:\n", classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))

# Plotting the decision tree


plt.figure(figsize=(10, 6))
tree.plot_tree(clf, filled=True, feature_names=X.columns,
class_names=['No', 'Yes'])
plt.show()

if __name__ == "__main__":
df = load_sample_data()
print("Sample Data:\n", df)
X, y = preprocess_data(df)
decision_tree_classifier(X, y)
Output:

You might also like