Skip to content

Statistics: keras_proba.stats

Percentiles and quantiles, ported from TensorFlow Probability.

percentile

percentile(x, q, axis=None, interpolation=None, keepdims=False)

Computes the q-th percentile(s) of x.

Given a vector x, the q-th percentile is the value q / 100 of the way from the minimum to the maximum in a sorted copy of x. The two nearest neighbors and interpolation determine the result when the normalized rank does not match a sample exactly. Compare to numpy.percentile.

Example:

x = [1.0, 2.0, 3.0, 4.0]
percentile(x, q=30.0)                          # 2.0
percentile(x, q=30.0, interpolation="linear")  # 1.9
percentile(x, q=[30.0, 70.0], interpolation="lower")  # [1.0, 3.0]
percentile([[1.0, 2.0], [3.0, 4.0]], q=100.0, axis=[0])  # [3.0, 4.0]

Parameters:

Name Type Description Default
x

Numeric tensor with at least one dimension.

required
q

Scalar or 1-D percentile(s) in [0, 100], as concrete values.

required
axis

Int or list of ints: the axes indexing samples. If None, all dimensions are sample dimensions and the result is scalar per q.

None
interpolation

One of "nearest" (default), "linear", "lower", "higher" or "midpoint", used when a percentile lies between samples i < j: i + (j - i) * fraction, i, j, the nearest of the two, or (i + j) / 2. "linear" and "midpoint" require a floating point x.

None
keepdims

Whether to keep the reduced axes with size 1.

False

Returns:

Type Description

Tensor whose leading rank(q) dimensions index the percentiles,

followed by the dimensions of x not in axis.

quantiles

Multi-backend percentiles and quantiles in Keras 3.

Ported from TensorFlow Probability's tensorflow_probability/python/stats/quantiles.py (percentile and quantiles). The requested percentiles q must be concrete (Python or NumPy values), since the gathered indices are computed statically.

__cached__ module-attribute

__cached__ = '/home/runner/work/keras-proba/keras-proba/keras_proba/stats/__pycache__/quantiles.cpython-312.pyc'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__doc__ module-attribute

__doc__ = "Multi-backend percentiles and quantiles in Keras 3.\n\nPorted from TensorFlow Probability's `tensorflow_probability/python/stats/quantiles.py`\n(`percentile` and `quantiles`). The requested percentiles `q` must be concrete\n(Python or NumPy values), since the gathered indices are computed statically.\n"

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__file__ module-attribute

__file__ = '/home/runner/work/keras-proba/keras-proba/keras_proba/stats/quantiles.py'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__name__ module-attribute

__name__ = 'keras_proba.stats.quantiles'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__package__ module-attribute

__package__ = 'keras_proba.stats'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

percentile

percentile(x, q, axis=None, interpolation=None, keepdims=False)

Computes the q-th percentile(s) of x.

Given a vector x, the q-th percentile is the value q / 100 of the way from the minimum to the maximum in a sorted copy of x. The two nearest neighbors and interpolation determine the result when the normalized rank does not match a sample exactly. Compare to numpy.percentile.

Example:

x = [1.0, 2.0, 3.0, 4.0]
percentile(x, q=30.0)                          # 2.0
percentile(x, q=30.0, interpolation="linear")  # 1.9
percentile(x, q=[30.0, 70.0], interpolation="lower")  # [1.0, 3.0]
percentile([[1.0, 2.0], [3.0, 4.0]], q=100.0, axis=[0])  # [3.0, 4.0]

Parameters:

Name Type Description Default
x

Numeric tensor with at least one dimension.

required
q

Scalar or 1-D percentile(s) in [0, 100], as concrete values.

required
axis

Int or list of ints: the axes indexing samples. If None, all dimensions are sample dimensions and the result is scalar per q.

None
interpolation

One of "nearest" (default), "linear", "lower", "higher" or "midpoint", used when a percentile lies between samples i < j: i + (j - i) * fraction, i, j, the nearest of the two, or (i + j) / 2. "linear" and "midpoint" require a floating point x.

None
keepdims

Whether to keep the reduced axes with size 1.

False

Returns:

Type Description

Tensor whose leading rank(q) dimensions index the percentiles,

followed by the dimensions of x not in axis.

quantiles

quantiles(x, num_quantiles, axis=None, interpolation=None, keepdims=False)

Computes num_quantiles + 1 cut points of x along axis.

The cut points (c0, ..., cn) divide the samples into num_quantiles intervals [c0, c1), ..., [c_{n-1}, cn] with roughly equal numbers of samples; c0 is the minimum and cn the maximum.

Example:

x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
quantiles(x, num_quantiles=4)                          # [0., 2., 5., 8., 10.]
quantiles(x, num_quantiles=4, interpolation="linear")  # [0., 2.5, 5., 7.5, 10.]

Parameters:

Name Type Description Default
x

Numeric tensor with at least one dimension.

required
num_quantiles

Number of intervals.

required
axis

Int or list of ints: the axes indexing samples. If None, all dimensions are sample dimensions.

None
interpolation

See percentile.

None
keepdims

Whether to keep the reduced axes with size 1.

False

Returns:

Type Description

Tensor of shape (num_quantiles + 1, ...), where the trailing shape is

that of x without axis (unless keepdims).