Some times you will be tasked to fit a model to data with high variance, as in the data varies from the average a lot. In such cases, we can use regularization techniques. Logisitc regression over some options for regularization. In this article, we will see how to use regularization with Logistic Regression in Sklearn.
To regularize a logistic regression model, we can use two paramters penalty
and Cs
(cost). In practice, we would use something like GridCV
or a loop to try multipel paramters and pick the best model from the group. Below is an example of how to specify these parameters on a logisitc regression model.
from sklearn.linear_model import LogisticRegressionCV
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
iris = datasets.load_iris()
features = iris.data
target = iris.target
scaler = StandardScaler()
features_standardized = scaler.fit_transform(features)
logistic_regression = LogisticRegressionCV(
penalty='l2',
Cs=10
)
model = logistic_regression.fit(features_standardized, target)
print(mode.score())