Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Scikit learn Scikit learn LogisticRegression Fit

From Leeroopedia


Field Value
source scikit-learn|https://github.com/scikit-learn/scikit-learn
domains Data_Science, Machine_Learning
last_updated 2026-02-08 15:00 GMT

Overview

Concrete tool for training a logistic regression model on labeled data provided by scikit-learn.

Description

The LogisticRegression.fit method performs the actual parameter estimation for logistic regression. It validates the input data, selects the appropriate internal solver routine based on the estimator's hyperparameters, and stores the resulting learned parameters as instance attributes. The method supports both binary and multiclass classification, various regularization schemes, and multiple optimization backends.

Usage

  • Training a logistic regression classifier on a labeled dataset.
  • Re-fitting after hyperparameter tuning or data changes.
  • Warm-starting from a previously fitted model to speed up convergence.

Code Reference

Source Location

sklearn/linear_model/_logistic.py, method LogisticRegression.fit

Signature

def fit(self, X, y, sample_weight=None):

Import

from sklearn.linear_model import LogisticRegression

clf = LogisticRegression()
clf.fit(X_train, y_train)

I/O Contract

Inputs

Parameter Type Default Description
X {array-like, sparse matrix} of shape (n_samples, n_features) (required) Training feature matrix. Accepted sparse format is CSR.
y array-like of shape (n_samples,) (required) Target vector of class labels relative to X.
sample_weight array-like of shape (n_samples,) or None None Weights assigned to individual samples. If not provided, all samples have unit weight.

Outputs

Return Type Description
self LogisticRegression The fitted estimator instance (enables method chaining).

Fitted Attributes

After a successful call to fit, the following attributes are available on the estimator:

Attribute Type Description
coef_ ndarray of shape (1, n_features) or (n_classes, n_features) Coefficient (weight) matrix. Shape is (1, n_features) for binary classification; (n_classes, n_features) for multiclass.
intercept_ ndarray of shape (1,) or (n_classes,) Intercept (bias) term added to the decision function.
classes_ ndarray of shape (n_classes,) The unique class labels found in y.
n_iter_ ndarray of shape (1,) or (n_classes,), dtype int32 Actual number of iterations performed by the solver.

Usage Examples

Basic training:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)

clf = LogisticRegression(max_iter=200, random_state=42)
clf.fit(X_train, y_train)

print(clf.coef_.shape)       # (3, 4)
print(clf.intercept_.shape)  # (3,)
print(clf.classes_)          # [0 1 2]
print(clf.n_iter_)           # array of iteration counts

Training with sample weights:

import numpy as np
from sklearn.linear_model import LogisticRegression

clf = LogisticRegression(max_iter=200)
weights = np.ones(len(y_train))
weights[y_train == 2] = 2.0  # upweight class 2
clf.fit(X_train, y_train, sample_weight=weights)

Method chaining:

from sklearn.linear_model import LogisticRegression

predictions = LogisticRegression(max_iter=200).fit(X_train, y_train).predict(X_test)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment