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.
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:
- '
wis the weight vector' - '
bis the intercept or bias term' - '
xis 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.
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:
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 thedfeature 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.
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.
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
- how to get top-k best candidate sequences when using CRF for decoding in tensorflow
- How to get top n arg max/min in tensorflow?
- How to get value of a Keras tensor in TensorFlow 2?
- How to get weight matrix of one layer at every epoch in LSTM model based on Keras?
- How to get/set a pandas index column title or name?
- How to graph grid scores from GridSearchCV?
- How to get weights from .pb model in Tensorflow
- How to get weights from tensorflow fully_connected
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.