How to get the weight vector in Logistic Regression?

Viewed 25224

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?

2 Answers

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)

clf_bow_perb = LogisticRegression(C= 10, penalty= 'l2')
clf_bow_perb.fit(X_1,y_1)
y_pred = clf_bow_perb.predict(X_1)
print("Accuracy on test set: %0.3f%%"%(accuracy_score(y_1, y_pred)*100))
print("Non Zero weights:",np.count_nonzero(clf.coef_))
Related