How to get the weight vector in Logistic Regression?

bluemoon718 picture bluemoon718 · Nov 11, 2017 · Viewed 15.8k times · Source

I have a X feature matrix and a y label matrix and I am using binary logistic regression how can I get the weight vector w given matrix X feature and Y label matrix. I am a bit confused as to how achieve this within sklean.

How do I solve the problem?

Answer

MaxU picture MaxU · Nov 11, 2017

If i understand correctly you are looking for the coef_ attribute:

lr = LogisticRegression(C=1e5)
lr.fit(X, Y)

print(lr.coef_) # returns a matrix of weights (coefficients)

The shape of coef_ attribute should be: (# of classes, # of features)

If you also need an intercept (AKA bias) column, then use this:

np.hstack((clf.intercept_[:,None], clf.coef_))

this will give you an array of shape: (n_classes, n_features + 1)