39. End-to-End Machine Learning Project (India Dataset)
39.3 Train Two Models
Why. Try two model families — linear classifier and a small forest — same split, fair fight.
What. Fit LogisticRegression and RandomForestClassifier on X_train only.
How.
Train both
- Fit logistic with max_iter high enough to converge.
- Fit a modest forest (e.g. 100 trees, max_depth=4).
- Predict on X_test for both.
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
log_reg = LogisticRegression(max_iter=500)
log_reg.fit(X_train, y_train)
forest = RandomForestClassifier(
n_estimators=100, max_depth=4, random_state=42
)
forest.fit(X_train, y_train)
pred_lr = log_reg.predict(X_test)
pred_rf = forest.predict(X_test)
print("logistic preds:", pred_lr)
print("forest preds: ", pred_rf)
What you should see. Two prediction vectors. Next page scores them — do not pick a winner by eye alone.