When working on the imbalanced dataset, I found an interesting question about LogisticRegression in scikit-learn.
For the parameter class_weight, if I send {1:0.5, 0:0.5}, I will get a different outcome with {1:1, 0:1} even though they are actually the same weights mathematically.
Here is what I got,
import numpy as np
from sklearn.linear_model import LogisticRegression
np.random.seed(1)
def sigmoid(x):
return 1/(np.exp(-x)+1)
x1 = np.random.normal(0, 4, 100000)
x2 = np.random.normal(0, 1, 100000)
X = np.array([x1, x2]).T
proba = sigmoid(0.1 + 2*x1 + 3*x2)
y = np.random.binomial(1, proba)
lr1 = LogisticRegression(C=1, class_weight = {1:0.5, 0:0.5}).fit(X, y)
print(lr1.score(X,y)) # 0.93656
lr2 = LogisticRegression(C=1, class_weight = {0:1, 1:1}).fit(X, y)
print(lr2.score(X,y)) # 0.93653
- Could you please tell me how the
class_weightparameter works actually and why it happens? - How to set
class_weightproperly?