Aggregations and custom reducers¶
Regressogram.agg and each optional descriptive ci endpoint accept named
statistics, native Polars expression functions, or the adapters below.
Named reductions¶
| Name | Unweighted meaning | With observation weights |
|---|---|---|
mean |
Arithmetic mean | Weighted mean, undefined at zero total mass. |
sum |
Response total | Sum of weight × response; zero at zero mass. |
median |
Median | Requires an explicit weighted callback. |
min, max |
Extremal observed response | Requires an explicit weighted callback. |
count |
Number of non-null responses (all fitted responses are validated) | Requires an explicit weighted callback. |
std, var |
Polars sample standard deviation/variance with ddof=1; singleton gives null/unsupported |
Requires an explicit weighted callback. |
first, last |
Response in original within-bin row order | Requires an explicit weighted callback. |
Callbacks must return one real numeric scalar per bin, or one Polars scalar reduction expression for native callbacks. NaN is an explicit missing estimate; infinity and nonscalar outputs are rejected. A bare callable means a native Polars-expression callback, not an automatically detected NumPy function.
Factory functions¶
rgram.array_aggregation¶
rgram.array_aggregation(
func,
*,
weighted=False,
)Wrap a NumPy/Python scalar function for agg or a ci endpoint.
Parameters¶
funccallableCallback func(values), or func(values, weights) with weighted=True.
weightedbool, default=FalseWhether the callback explicitly accepts paired observation weights.
Returns¶
adapterArrayAggregationCloneable reducer, retaining the callable and weighting option.
Examples¶
>>> import numpy as np
>>> from rgram import Regressogram, array_aggregation
>>> model = Regressogram(n_bins=1, agg=array_aggregation(np.median))
>>> model.fit([0., 1., 2.], [1., 3., 9.]).predict([1.])
array([3.])
rgram.weighted_aggregation¶
rgram.weighted_aggregation(func)Wrap a native weighted Polars scalar reduction.
Parameters¶
funccallableCallback func(values_expr, weights_expr) returning a Polars expression.
Returns¶
adapterWeightedAggregationCloneable native reducer. With no fitting weights, the adapter uses ones.
Examples¶
>>> from rgram import Regressogram, weighted_aggregation
>>> reducer = weighted_aggregation(lambda values, weights:
... (values * weights).sum() / weights.sum())
>>> model = Regressogram(n_bins=1, agg=reducer)
>>> model.fit([0., 1.], [2., 8.], sample_weight=[1., 2.]).predict([0.5])
array([6.])
rgram.quantile¶
rgram.quantile(
q,
*,
interpolation='linear',
)Create an unweighted native Polars quantile reducer.
Parameters¶
qfloatProbability in [0, 1]. Validated when the reducer is evaluated.
interpolationstr, default='linear'nearest, higher, lower, midpoint, linear, or equiprobable, passed to Polars.
Returns¶
reducerQuantileCloneable quantile callable for agg or either descriptive ci endpoint.
Examples¶
>>> from rgram import Regressogram, quantile
>>> model = Regressogram(n_bins=1, agg=quantile(0.5))
>>> model.fit([0., 1., 2.], [1., 3., 9.]).predict([1.])
array([3.])
Returned adapter objects¶
These classes are importable from rgram.aggregation. The factories are the
recommended way to create them. Their parameters participate in cloning and
nested tuning, for example model.set_params(agg__q=0.9) followed by refitting.
A custom callback must implement any desired zero-mass or weighted-quantile
convention explicitly; the library does not infer it.
rgram.aggregation.ArrayAggregation¶
class rgram.aggregation.ArrayAggregation(
func,
*,
weighted=False,
)Adapt a numeric callback to a cloneable per-bin scalar reduction.
Parameters¶
funccallableFunction returning one real numeric scalar from values, or values and weights when weighted=True. Receives independent arrays in bin-row order.
weightedbool, default=FalseExplicitly require a two-argument callback. If fitting weights are absent, the callback receives ones. Unweighted callbacks reject supplied weights.
Notes¶
Usually constructed with array_aggregation. No values are sorted, filtered, or imputed. Callback exceptions propagate. NaN marks unsupported results; infinity is rejected by Regressogram. This adapter inherits sklearn get_params/set_params.
Methods¶
| Method | Purpose |
|---|---|
| call | Apply the numeric callback to independent input copies. |
| get_metadata_routing | Get metadata routing of this object. |
| get_params | Get parameters for this estimator. |
| set_params | Set the parameters of this estimator. |
call¶
rgram.aggregation.ArrayAggregation.__call__(values, weights=None)Apply the numeric callback to independent input copies.
Parameters¶
valuesarray-likeResponses in one bin.
weightsarray-like or None, default=NonePaired influence weights. A weighted adapter uses ones when absent.
Returns¶
valuefloatOne real scalar. Nonscalar, complex, and imprecisely converted integer callback results raise ValueError.
get_metadata_routing¶
rgram.aggregation.ArrayAggregation.get_metadata_routing()Get metadata routing of this object.
Please check metadata routing guide on how the routing mechanism works.
Returns¶
routingMetadataRequestA
sklearn.utils.metadata_routing.MetadataRequestencapsulating routing information.
get_params¶
rgram.aggregation.ArrayAggregation.get_params(deep=True)Get parameters for this estimator.
Parameters¶
deepbool, default=TrueIf True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns¶
paramsdictParameter names mapped to their values.
set_params¶
rgram.aggregation.ArrayAggregation.set_params(**params)Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as sklearn.pipeline.Pipeline). The latter have
parameters of the form <component>__<parameter> so that it's
possible to update each component of a nested object.
Parameters¶
**paramsdictEstimator parameters.
Returns¶
selfestimator instanceEstimator instance.
rgram.aggregation.WeightedAggregation¶
class rgram.aggregation.WeightedAggregation(
func,
)Adapt a native Polars weighted reduction.
Parameters¶
funccallableFunction accepting (values_expr, weights_expr) and returning one scalar reduction as a Polars expression. Usually use weighted_aggregation.
Notes¶
Without supplied weights, nonmissing values receive unit weights. The adapter inherits sklearn get_params/set_params and is cloneable.
Methods¶
| Method | Purpose |
|---|---|
| call | Build the weighted Polars reduction expression. |
| get_metadata_routing | Get metadata routing of this object. |
| get_params | Get parameters for this estimator. |
| set_params | Set the parameters of this estimator. |
call¶
rgram.aggregation.WeightedAggregation.__call__(values, weights=None)Build the weighted Polars reduction expression.
Parameters¶
valuespolars.ExprExpression selecting bin responses.
weightspolars.Expr or None, default=NoneWeight expression. None gives unit weights for nonmissing values.
Returns¶
expressionpolars.ExprThe expression returned by func; Regressogram checks the result type.
get_metadata_routing¶
rgram.aggregation.WeightedAggregation.get_metadata_routing()Get metadata routing of this object.
Please check metadata routing guide on how the routing mechanism works.
Returns¶
routingMetadataRequestA
sklearn.utils.metadata_routing.MetadataRequestencapsulating routing information.
get_params¶
rgram.aggregation.WeightedAggregation.get_params(deep=True)Get parameters for this estimator.
Parameters¶
deepbool, default=TrueIf True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns¶
paramsdictParameter names mapped to their values.
set_params¶
rgram.aggregation.WeightedAggregation.set_params(**params)Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as sklearn.pipeline.Pipeline). The latter have
parameters of the form <component>__<parameter> so that it's
possible to update each component of a nested object.
Parameters¶
**paramsdictEstimator parameters.
Returns¶
selfestimator instanceEstimator instance.
rgram.aggregation.Quantile¶
class rgram.aggregation.Quantile(
q,
*,
interpolation='linear',
)Cloneable Polars quantile reduction with explicit interpolation.
Parameters¶
qfloatQuantile probability in [0, 1], excluding booleans.
interpolationstr, default='linear'One of nearest, higher, lower, midpoint, linear, equiprobable. Passed directly to Polars quantile.
Notes¶
Usually constructed with quantile. Observation weights are unsupported; choose an explicit weighted adapter if you require weighted quantiles. Inherits sklearn get_params/set_params.
Methods¶
| Method | Purpose |
|---|---|
| call | Build an unweighted quantile expression. |
| get_metadata_routing | Get metadata routing of this object. |
| get_params | Get parameters for this estimator. |
| set_params | Set the parameters of this estimator. |
call¶
rgram.aggregation.Quantile.__call__(values)Build an unweighted quantile expression.
Parameters¶
valuespolars.ExprExpression selecting bin responses.
Returns¶
expressionpolars.ExprQuantile reduction with the configured probability and interpolation.
Raises¶
ValueErrorIf q or interpolation is invalid.
get_metadata_routing¶
rgram.aggregation.Quantile.get_metadata_routing()Get metadata routing of this object.
Please check metadata routing guide on how the routing mechanism works.
Returns¶
routingMetadataRequestA
sklearn.utils.metadata_routing.MetadataRequestencapsulating routing information.
get_params¶
rgram.aggregation.Quantile.get_params(deep=True)Get parameters for this estimator.
Parameters¶
deepbool, default=TrueIf True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns¶
paramsdictParameter names mapped to their values.
set_params¶
rgram.aggregation.Quantile.set_params(**params)Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as sklearn.pipeline.Pipeline). The latter have
parameters of the form <component>__<parameter> so that it's
possible to update each component of a nested object.
Parameters¶
**paramsdictEstimator parameters.
Returns¶
selfestimator instanceEstimator instance.
A callback may transform its independent input copies, but Rgram adds no sorting or filtering around it. Named reducers and adapters with importable top-level callbacks can be pickled. Lambdas and closures may not work with standard pickle.
Implementation source: aggregation.py.