You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The statsmodelslibrary I'm using for percentile binning doesn't interpolate between points.
Taimoor is using this instead:
defweighted_quantile(values, quantiles, sample_weight=None,
values_sorted=False, old_style=False):
""" Very close to numpy.percentile, but supports weights. NOTE: quantiles should be in [0, 1]! :param values: numpy.array with data :param quantiles: array-like with many quantiles needed :param sample_weight: array-like of the same length as `array` :param values_sorted: bool, if True, then will avoid sorting of initial array :param old_style: if True, will correct output to be consistent with numpy.percentile. :return: numpy.array with computed quantiles. """values=np.array(values)
quantiles=np.array(quantiles)
ifsample_weightisNone:
sample_weight=np.ones(len(values))
sample_weight=np.array(sample_weight)
assertnp.all(quantiles>=0) andnp.all(quantiles<=1), \
'quantiles should be in [0, 1]'ifnotvalues_sorted:
sorter=np.argsort(values)
values=values[sorter]
sample_weight=sample_weight[sorter]
weighted_quantiles=np.cumsum(sample_weight) -0.5*sample_weightifold_style:
# To be convenient with numpy.percentileweighted_quantiles-=weighted_quantiles[0]
weighted_quantiles/=weighted_quantiles[-1]
else:
weighted_quantiles/=np.sum(sample_weight)
returnnp.interp(quantiles, weighted_quantiles, values)
The text was updated successfully, but these errors were encountered:
The
statsmodels
library I'm using for percentile binning doesn't interpolate between points.Taimoor is using this instead:
The text was updated successfully, but these errors were encountered: