Weighted logistic regression in Python

孤人 提交于 2020-06-09 08:14:07

问题


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!


回答1:


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.




回答2:


The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y))

from sklearn.linear_model import LogisticRegression

model = LogisticRegression(class_weight='balanced')

model = model.fit(X, y)

EDIT

Sample Weights can be added in the fit method. You just have to pass an array of n_samples. Check out documentation -

http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.fit

Hope this does it...




回答3:


I think what you want is statsmodels. It has great support for GLM and other linear methods. If you're coming from R, you'll find the syntax very familiar.

statsmodels weighted regression

getting started w/ statsmodels




回答4:


Have a look at scikits.learn logistic regression implementation




回答5:


Do you know Numpy? If no, take a look also to Scipy and matplotlib.



来源:https://stackoverflow.com/questions/7513067/weighted-logistic-regression-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!