Skip to content

Instantly share code, notes, and snippets.

@MattLowrieDS
Created January 6, 2021 03:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattLowrieDS/f977588f8ef54c903711836918145914 to your computer and use it in GitHub Desktop.
Save MattLowrieDS/f977588f8ef54c903711836918145914 to your computer and use it in GitHub Desktop.
Example using sklearn compute_class_weight()
from sklearn.utils.class_weight import compute_class_weight
pass_results = plays_df.loc[plays_df['passResult'].isin(category_lookup.keys()), 'passResult']
all_labels = pass_results.apply(lambda lbl: category_lookup[lbl])
# Create class weights to counter-balance classification during training
y = np.stack(all_labels).argmax(axis=1)
classes = np.unique(y)
weights = compute_class_weight('balanced', classes=classes, y=y)
class_weights = {k: v for k, v in zip(classes, weights)}
print('Class weights:', class_weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment