Suppose I've got highly imbalanced data and I want to train a model, for binary classification. So I upsample the minority class or downsample the majority class or whatever. My question is whether predicted probabilities, obtained after training the model, need to be adjusted because I resampled the training data.

For concreteness, assume I'm using a logistic regression, and assume that y=1 is the minority class. Here I'm upsampling.

print('Number of class 0 samples before:', X_imb[y_imb == 0].shape[0])
print('Number of class 1 samples before:', X_imb[y_imb == 1].shape[0])

# bootstrapping
X_upsampled, y_upsampled = resample(X_imb[y_imb == 1],
                                    y_imb[y_imb == 1],
                                    n_samples=X_imb[y_imb == 1].shape[0]*5,

print('Number of class 1 samples after:', X_upsampled.shape[0])

X_bal = np.vstack((X_imb[y_imb == 0], X_upsampled))
y_bal = np.hstack((y_imb[y_imb == 0], y_upsampled))

original_rate = X_imb[y_imb == 1].shape[0] / X_imb.shape[0]

rate_after_upsampling = X_bal[y_bal == 1].shape[0] / X_bal.shape[0]

Is this necessary:

adjust = log((1-original_rate)/original_rate * (rate_after_upsampling)/(1-rate_after_upsampling))
# adjust == log(5)

predicted_y_adjusted = np.array([1/(1+exp(-j)) for j in X_bal.dot(log_reg.coef_[0])+log_reg.intercept_[0]-adjust])

Thank you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.