Understanding the weights of the features in a logistic regression model is crucial for interpreting the model. By retrieving and interpreting the coef_
attribute of a fitted LogisticRegression
model, you can gain insights into the influence of each feature on the predictions.
LogisticRegression
is a linear model used for binary classification tasks, which fits a logistic function to the data. The coef_
attribute of a fitted LogisticRegression
model stores the coefficients (weights) for each feature, indicating their influence on the prediction. Accessing the coef_
attribute is useful for feature interpretation and understanding the impact of each feature on the model’s predictions. This can guide feature selection and model refinement.
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# Generate a synthetic binary classification dataset
X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=42, shuffle=False)
# Split the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize a LogisticRegression model
lr = LogisticRegression(random_state=42)
# Fit the model on the training data
lr.fit(X_train, y_train)
# Access the coef_ attribute and print the coefficients for each feature
coefficients = lr.coef_
print(f"Coefficients of the logistic regression model: {coefficients}")
Running the example gives an output like:
Coefficients of the logistic regression model: [[-0.26262277 2.1285876 0.16353088 -0.08982462]]
The key steps in this example are:
- Generate a synthetic binary classification dataset using
make_classification
and split it into train and test sets. - Initialize a
LogisticRegression
model and fit it on the training data. - Access the
coef_
attribute to retrieve the coefficients of the fitted model. - Print the coefficients to interpret the influence of each feature on the model’s predictions.