Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions reduce overfit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

Check failure on line 4 in reduce overfit.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

reduce overfit.py:1:1: I001 Import block is un-sorted or un-formatted

# Load dataset
data = load_iris()
X, y = data.data, data.target

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42)

# Decision Tree with Overfitting Controls
model = DecisionTreeClassifier(
max_depth=3, # limit depth of tree
min_samples_split=4, # minimum samples to split a node
min_samples_leaf=2, # minimum samples in each leaf
ccp_alpha=0.01, # pruning parameter (cost-complexity pruning)
random_state=42
)

model.fit(X_train, y_train)

# Predictions
y_pred = model.predict(X_test)

# Accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Depth of Tree:", model.get_depth())
print("Number of Leaves:", model.get_n_leaves())
Loading