Decision Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy