Skip to content

Instantly share code, notes, and snippets.

@Tob-iee
Created December 12, 2023 21:27
Show Gist options
  • Save Tob-iee/04b2e714f45f3f25cce08d11df3aa796 to your computer and use it in GitHub Desktop.
Save Tob-iee/04b2e714f45f3f25cce08d11df3aa796 to your computer and use it in GitHub Desktop.
Class Weighting
# Import class weights function
from sklearn.utils.class_weight import compute_class_weight
# Calculate class weights
class_weights = compute_class_weight('balanced', np.unique(y_train), y_train)
# Create a dictionary to map class indices to their respective weights
class_weight_dict = dict(zip(np.unique(y_train), class_weights))
# Define and train a classifier with class weights
classifier = RandomForestClassifier(class_weight=class_weight_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment