Explore Library
Code Quiz

Regularizing an Overfit Logistic Model

Spot the subtle mistake when tuning regularization strength to fight overfitting in scikit-learn.

Codepython
from sklearn.linear_model import LogisticRegression

# train accuracy = 0.99, validation accuracy = 0.71 -> clearly overfitting.
# Goal: add MORE regularization to reduce variance.

model = LogisticRegression(penalty='l2', C=100.0, max_iter=1000)
model.fit(X_train, y_train)

print('train:', model.score(X_train, y_train))
print('val:  ', model.score(X_val, y_val))
# Author expects the gap to shrink, but it stays the same or gets worse.

The model is overfitting and the author wants stronger regularization, but this code does the opposite. What is the bug?