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) print('Number of class 1 samples before:', X_imb[y_imb == 1].shape) # bootstrapping X_upsampled, y_upsampled = resample(X_imb[y_imb == 1], y_imb[y_imb == 1], replace=True, n_samples=X_imb[y_imb == 1].shape*5, random_state=123) print('Number of class 1 samples after:', X_upsampled.shape) 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 / X_imb.shape rate_after_upsampling = X_bal[y_bal == 1].shape / X_bal.shape
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_)+log_reg.intercept_-adjust])