12

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?

Emma
  • 27,428
  • 11
  • 44
  • 69
bluemoon718
  • 139
  • 1
  • 1
  • 5

2 Answers2

20

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)

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
-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_))
Partho63
  • 3,117
  • 2
  • 21
  • 39
Dush
  • 1