| x1 | x2 | |
|---|---|---|
| 0 | -0.752759 | 2.704286 |
| 1 | 1.391964 | 0.591951 |
| 2 | -2.063888 | -2.064033 |
| x1 | x2 | |
|---|---|---|
| 0 | -0.752759 | 2.704286 |
| 1 | 1.391964 | 0.591951 |
| 2 | -2.063888 | -2.064033 |
| x1 | x2 | x1^2 | x1 x2 | x2^2 | |
|---|---|---|---|---|---|
| 0 | -0.752759 | 2.704286 | 0.566647 | -2.035676 | 7.313162 |
| 1 | 1.391964 | 0.591951 | 1.937563 | 0.823974 | 0.350406 |
| 2 | -2.063888 | -2.064033 | 4.259634 | 4.259933 | 4.260232 |
good model is capturing the pattern of data, NOT to fit all the training points.complexity of the model (e.g., polynomial degree).magnitude of the parameters (e.g., regularization/penalization).Regularization is an important method that can be used with any parametric models not only logtistic regression.
| Set | Common % | Purpose |
|---|---|---|
| Train | 70%–80% | For training the model |
| Validation | 10%–15% | For tuning hyperparameters (\(\alpha\)) |
| Test | 10%–15% | Evaluate final model performance |

[min,...,max]:
f in [1,...,K]:
model with hyperparameter \(\color{blue}{\alpha}\) on all data except for fold f.f denoted by \(\color{red}{\epsilon_f}\).

Tuning Optimal Polynomial Degree using \(10\)-fold Cross-Validation
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
# List of degrees to search over
degrees = list(range(1,11))
# List for storing accuracy
acc = []
for deg in degrees:
poly = PolynomialFeatures(degree=deg, include_bias=False)
x_poly = poly.fit_transform(
pd.DataFrame(x1[:,1], columns=['x1'])
)
x_poly = np.column_stack([x_poly,x1[:,2],x1[:,1]*x1[:,2]])
# model
model = LogisticRegression()
score = cross_val_score(model, x_poly, y_, cv=10,
scoring='accuracy'
).mean()
acc.append(score)Tuning Penalty Strength \(\alpha\) using \(10\)-fold Cross-Validation
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
# Data
poly = PolynomialFeatures(degree=5, include_bias=False)
x_poly = poly.fit_transform(
pd.DataFrame(x1[:,1], columns=['x1']))
x_poly = np.column_stack([x_poly,x1[:,2],x1[:,1]*x1[:,2]])
# Penalty strength
alphas = np.concatenate([
np.linspace(1e-3,10, 50),
np.linspace(10.1, 1000, 50)]
)
coefficients = {f'{alp}': [] for alp in alphas}
acc = []
for alp in alphas:
# model
model = LogisticRegression(penalty="l2", C=1/alp)
score = cross_val_score(model, x_poly, y_, cv=10,
scoring='balanced_accuracy').mean()
acc.append(score)
# Fitting
model.fit(x_poly, y_)
coefficients[f'{alp}'] = model.coef_[0]Tuning Penalty Strength \(\alpha\) using \(10\)-fold Cross-Validation
Tuning Penalty Strength \(\alpha\) using \(10\)-fold Cross-Validation
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
# Data
poly = PolynomialFeatures(degree=5, include_bias=False)
x_poly = poly.fit_transform(
pd.DataFrame(x1[:,1], columns=['x1']))
x_poly = np.column_stack([x_poly,x1[:,2],x1[:,1]*x1[:,2]])
# Penalty strength
alphas = np.concatenate([
np.linspace(1e-3,10, 50),
np.linspace(10.1, 1000, 50)]
)
coefficients = {f'{alp}': [] for alp in alphas}
acc = []
for alp in alphas:
# model
model = LogisticRegression(penalty="l1",
C=1/alp, solver='saga')
score = cross_val_score(model, x_poly, y_, cv=10,
scoring='balanced_accuracy').mean()
acc.append(score)
# Fitting
model.fit(x_poly, y_)
coefficients[f'{alp}'] = model.coef_[0]Tuning Penalty Strength \(\alpha\) using \(10\)-fold Cross-Validation
Logistic Regression is a powerful classification algorithm especially for binary outcomes.
It’s an interpretable model that provides insights into the relationship between features and the target variable.
It can be extended to handle non-linear decision boundaries using polynomial features.
Regularization techniques like \(L_1\) and \(L_2\) help prevent overfitting (especially with working with polynomial features) and improve model generalization.
Hyperparameter tuning, especially using cross-validation, is crucial for selecting the best model parameters.
