Skip to content

Instantly share code, notes, and snippets.

@GarrettMooney
Created February 7, 2023 15:46
Show Gist options
  • Save GarrettMooney/369cc23bfc3b23e04b212a5784a9cb0e to your computer and use it in GitHub Desktop.
Save GarrettMooney/369cc23bfc3b23e04b212a5784a9cb0e to your computer and use it in GitHub Desktop.
from typing import Union
import numpy as np
import pandas as pd
def create_sample_weights(
y_train: np.ndarray,
X_train: Union[np.ndarray, pd.DataFrame]
) -> pd.Series:
y_series = pd.DataFrame({"y": y_train})["y"]
class_weights = len(X_train) / y_series.value_counts()
sample_weights = y_series.map(class_weights)
return sample_weights
"""Example:
>>> sample_weights = create_sample_weights(y_train, X_train)
>>> model = HistGradientBoostingClassifier(**params)
>>> model.fit(X_train, y_train, sample_weight=sample_weights)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment