Skip to content

Instantly share code, notes, and snippets.

@MattLowrieDS
MattLowrieDS / compute_class_weight
Created January 6, 2021 03:07
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)
def label_smoothing(y, factor=0.1):
y *= 1 - factor
y += factor / y.shape[1]
return y
lbl_vc = feature_label_df['labels'].value_counts()
total = lbl_vc.sum()
mu = 3.0
class_weights = {}
for i, k in enumerate(lbl_vc.keys()):
w = np.log(mu * total / float(lbl_vc[k]))
class_weights[i] = w if w > 1 else 1
print(class_weights)
@MattLowrieDS
MattLowrieDS / basic-r-example.ipynb
Last active January 21, 2019 20:44
R notebook example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.