2013-07-29 00:45:20 +08:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 08:44:50 +08:00
|
|
|
def _balance_weights(y):
|
2013-07-29 00:45:20 +08:00
|
|
|
"""Compute sample weights such that the class distribution of y becomes
|
|
|
|
|
balanced.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
y : array-like
|
|
|
|
|
Labels for the samples.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
weights : array-like
|
|
|
|
|
The sample weights.
|
|
|
|
|
"""
|
2014-07-20 19:31:45 +08:00
|
|
|
y = np.asarray(y)
|
2013-07-29 00:45:20 +08:00
|
|
|
y = np.searchsorted(np.unique(y), y)
|
|
|
|
|
bins = np.bincount(y)
|
|
|
|
|
|
|
|
|
|
weights = 1. / bins.take(y)
|
|
|
|
|
weights *= bins.min()
|
|
|
|
|
|
|
|
|
|
return weights
|