Logistic Regression
Weight Vector
Machine Learning
Data Science
Regression Analysis

How to get the weight vector in Logistic Regression?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In logistic regression, the weight vector is the set of learned coefficients that multiplies the input features before the sigmoid or softmax step. If you are using a machine learning library, you usually do not compute this vector by hand. You fit the model, then read the learned coefficients from the trained estimator. The only wrinkle is understanding whether the intercept is stored separately and how multiclass models represent their coefficients.

What The Weight Vector Means

For binary logistic regression, the model is typically written as:

  • 'z = w^T x + b'
  • 'p = sigmoid(z)'

Here:

  • 'w is the weight vector'
  • 'b is the intercept or bias term'
  • 'x is the feature vector'

So the weight vector tells you how strongly each feature pushes the log-odds up or down.

A positive weight means the feature increases the log-odds of the positive class when all else is fixed. A negative weight means it decreases them.

Using scikit-learn

In scikit-learn, the learned coefficients are exposed after fitting.

python
1from sklearn.datasets import make_classification
2from sklearn.linear_model import LogisticRegression
3
4X, y = make_classification(n_samples=100, n_features=4, random_state=42)
5
6model = LogisticRegression()
7model.fit(X, y)
8
9print("Weights:", model.coef_)
10print("Intercept:", model.intercept_)

For a binary classifier, model.coef_ usually has shape (1, n_features), and model.intercept_ has shape (1,).

That means the weight vector is typically:

python
weights = model.coef_[0]

Intercept Is Usually Separate

A common mistake is to assume the bias term is included inside the coefficient array. In many libraries, including scikit-learn, it is stored separately as intercept_.

So if your feature vector has length d, then:

  • 'coef_ covers the d feature weights'
  • 'intercept_ is the bias term'

Do not silently append them together unless your downstream math expects that format explicitly.

Multiclass Logistic Regression

For multiclass logistic regression, the coefficient structure changes.

python
1from sklearn.datasets import load_iris
2from sklearn.linear_model import LogisticRegression
3
4X, y = load_iris(return_X_y=True)
5model = LogisticRegression(max_iter=500)
6model.fit(X, y)
7
8print(model.coef_.shape)
9print(model.intercept_.shape)

Here coef_ is often shaped like (n_classes, n_features). That means each class has its own weight vector.

So the question "what is the weight vector" becomes:

  • which class's weight vector do you mean

Manual Logistic Regression Example

If you implement logistic regression yourself, the weight vector is usually just a parameter tensor you optimize.

python
1import numpy as np
2
3
4def sigmoid(z):
5    return 1 / (1 + np.exp(-z))
6
7
8X = np.array([
9    [1.0, 2.0],
10    [1.5, 1.8],
11    [3.0, 3.2],
12    [2.5, 2.7],
13])
14y = np.array([0, 0, 1, 1])
15
16w = np.zeros(X.shape[1])
17b = 0.0
18lr = 0.1
19
20for _ in range(1000):
21    z = X @ w + b
22    p = sigmoid(z)
23
24    grad_w = X.T @ (p - y) / len(y)
25    grad_b = np.mean(p - y)
26
27    w -= lr * grad_w
28    b -= lr * grad_b
29
30print("Learned weights:", w)
31print("Learned bias:", b)

In this manual implementation, w is literally the weight vector you are asking about.

Regularization Changes The Weights

Be careful when interpreting coefficients. If the model uses L1 or L2 regularization, the learned weights are affected by that penalty.

That means the vector you read is not just the unconstrained maximum-likelihood solution. It is the regularized solution under the training setup you chose.

So if weights seem smaller than expected, regularization strength may be the reason.

Common Pitfalls

  • Forgetting that the intercept is often stored separately from the main coefficient vector.
  • Reading coef_ before fitting the model.
  • Assuming binary and multiclass logistic regression expose coefficients in the same shape.
  • Interpreting coefficients as direct probability effects instead of effects on log-odds.
  • Ignoring regularization when comparing coefficient magnitudes across runs.

Summary

  • The logistic regression weight vector is the learned coefficient vector multiplying the input features.
  • In scikit-learn, read it from model.coef_ after fitting.
  • The intercept is usually stored separately as model.intercept_.
  • In multiclass logistic regression, there is usually one weight vector per class.
  • If you implement logistic regression manually, the optimized parameter vector is the weight vector.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.