SKLearner Home | About | Contact | Examples

Get LogisticRegression "intercept_" Attribute

This example demonstrates how to access and utilize the intercept_ attribute of a fitted LogisticRegression model in scikit-learn. The intercept_ attribute stores the intercept (also known as the bias) term of the logistic regression model.

LogisticRegression is a linear model used for binary classification tasks. It models the probability of a binary outcome as a linear combination of the input features.

The intercept_ attribute contains the intercept term(s) of the fitted model. For binary classification, it is a single value; for multi-class classification, it is an array with one intercept per class.

Accessing the intercept_ attribute helps understand the bias term in the decision function of the logistic regression model. This can be important for interpreting the model’s predictions and understanding the baseline level of the outcome variable.

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
log_reg = LogisticRegression(random_state=42)

# Fit the model on the training data
log_reg.fit(X_train, y_train)

# Access the intercept_ attribute and print its value
intercept = log_reg.intercept_
print(f"Intercept: {intercept}")

# Calculate and print the accuracy of the model on the test set
accuracy = log_reg.score(X_test, y_test)
print(f"Model accuracy on the test set: {accuracy:.3f}")

Running the example gives an output like:

Intercept: [0.17149987]
Model accuracy on the test set: 0.900

The key steps in this example are:

  1. Generate a synthetic binary classification dataset using make_classification and split it into train and test sets.
  2. Initialize a LogisticRegression model and fit it on the training data.
  3. Access the intercept_ attribute to retrieve the intercept term of the fitted model.
  4. Print the value of the intercept to understand the bias term of the model.
  5. Calculate the accuracy of the model on the test set and print the result to evaluate its performance.


See Also