Weighted logistic regression in Python

user5497 picture user5497 · Sep 22, 2011 · Viewed 19.6k times · Source

I'm looking for a good implementation for logistic regression (not regularized) in Python. I'm looking for a package that can also get weights for each vector. Can anyone suggest a good implementation / package? Thanks!

Answer

William Darling picture William Darling · Feb 13, 2013

I notice that this question is quite old now but hopefully this can help someone. With sklearn, you can use the SGDClassifier class to create a logistic regression model by simply passing in 'log' as the loss:

sklearn.linear_model.SGDClassifier(loss='log', ...).

This class implements weighted samples in the fit() function:

classifier.fit(X, Y, sample_weight=weights)

where weights is a an array containing the sample weights that must be (obviously) the same length as the number of data points in X.

See http://scikit-learn.org/dev/modules/generated/sklearn.linear_model.SGDClassifier.html for full documentation.