Loading W Code...
6
Topics
sklearn
Implementation
Indian Context
Bank Loan Approval (SBI, HDFC), Credit Scoring, Insurance Underwriting
Concept Level: Beginner
# DECISION TREE BASICS
import numpy as np
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
# Create sample data
# Features: [Temperature, Humidity, Wind Speed, Ground Wetness]
# Label: Play Cricket (1) or Not (0)
np.random.seed(42)
X = np.array([
[35, 80, 10, 0], # Hot, Humid, Low Wind, Dry
[25, 60, 5, 0], # Pleasant weather
[28, 50, 8, 0], # Good conditions
[42, 40, 15, 0], # Too hot
[20, 90, 20, 1], # Cold, very humid, wet ground
[30, 70, 5, 0], # Nice weather
[38, 85, 12, 1], # Hot and wet
[22, 55, 6, 0], # Perfect
[40, 75, 18, 0], # Too hot, windy
[26, 65, 7, 0], # Good weather
])
y = np.array([1, 1, 1, 0, 0, 1, 0, 1, 0, 1]) # 1=Play, 0=Don't Play
# Build Decision Tree
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X, y)
# Visualize the tree
plt.figure(figsize=(20, 10))
feature_names = ['Temperature', 'Humidity', 'Wind', 'Ground_Wet']
class_names = ['No Play', 'Play']
plot_tree(tree,
feature_names=feature_names,
class_names=class_names,
filled=True,
rounded=True,
fontsize=12)
plt.title('Decision Tree: Should I Play Cricket?', fontsize=16)
plt.tight_layout()
plt.show()
# Make predictions
test_conditions = np.array([
[28, 60, 8, 0], # Pleasant day
[41, 85, 20, 1], # Bad conditions
])
predictions = tree.predict(test_conditions)
print("\nPredictions:")
print(f"Pleasant day (28°C, 60% humidity): {'PLAY!' if predictions[0] else 'Stay Home'}")
print(f"Bad weather (41°C, wet ground): {'PLAY!' if predictions[1] else 'Stay Home'}")
# Feature Importance
print("\nFeature Importance:")
for name, importance in zip(feature_names, tree.feature_importances_):
print(f" {name}: {importance:.2%}")