Decision Tree
Decision Tree
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Confusion Matrix:
[[1 0]
[0 2]]
Classification Report:
Precision recall f1-score support
accuracy 1.00 3
macro avg 1.00 1.00 1.00 3
weighted avg 1.00 1.00 1.00 3
Step by Step Explanation
1. Import Necessary Libraries:
import pandas as pd
Explanation:
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy', 'Rainy', 'Overcast', 'Sunny', 'Sunny',
'Rainy', 'Sunny', 'Overcast', 'Overcast', 'Rainy'],
'Temperature': ['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)
Explanation:
X = df.drop('PlayTennis', axis=1)
y = df['PlayTennis']
Explanation:
Explanation:
We split the dataset into training and testing sets using train_test_split function.
We use 80% of the data for training and 20% for testing.
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, y_train)
Explanation:
y_pred = decision_tree.predict(X_test)
Explanation:
print("Accuracy:", accuracy)
print("\nConfusion Matrix:")
print(conf_matrix)
print("\nClassification Report:")
print(class_report)
Explanation:
We print the accuracy, confusion matrix, and classification report to evaluate the
model's performance.
plt.figure(figsize=(12, 8))
plt.show()
Explanation:
Finally, we plot the decision tree using plot_tree function to visualize the model's
decision-making process.
We specify feature names and class names for better interpretation of the tree.
**************************