tfp.substrates.numpy.stats.find_bins
Stay organized with collections
Save and categorize content based on your preferences.
Bin values into discrete intervals.
tfp.substrates.numpy.stats.find_bins(
x,
edges,
extend_lower_interval=False,
extend_upper_interval=False,
dtype=None,
name=None
)
Given edges = [c0, ..., cK]
, defining intervals
I_0 = [c0, c1)
, I_1 = [c1, c2)
, ..., I_{K-1} = [c_{K-1}, cK]
,
This function returns bins
, such that x[i]
lies within I_{bins[i]}
.
Args |
x
|
Numeric N-D Tensor with N > 0 .
|
edges
|
Tensor of same dtype as x . The first dimension indexes edges
of intervals. Must either be 1-D or have
x.shape[1:] == edges.shape[1:] . If rank(edges) > 1 , edges[k]
designates a shape edges.shape[1:] Tensor of bin edges for the
corresponding dimensions of x .
|
extend_lower_interval
|
Python bool . If True , extend the lowest
interval I0 to (-inf, c1] .
|
extend_upper_interval
|
Python bool . If True , extend the upper
interval I_{K-1} to [c_{K-1}, +inf) .
|
dtype
|
The output type (int32 or int64 ). Default value: x.dtype .
This effects the output values when x is below/above the intervals,
which will be -1/K+1 for int types and NaN for float s.
At indices where x is NaN , the output values will be 0 for int
types and NaN for floats.
|
name
|
A Python string name to prepend to created ops. Default: 'find_bins'
|
Returns |
bins
|
Tensor with same shape as x and dtype .
Has whole number values. bins[i] = k means the x[i] falls into the
kth bin, ie, edges[bins[i]] <= x[i] < edges[bins[i] + 1] .
|
Raises |
ValueError
|
If edges.shape[0] is determined to be less than 2.
|
Examples
Cut a 1-D
array
x = [0., 5., 6., 10., 20.]
edges = [0., 5., 10.]
tfp.stats.find_bins(x, edges)
==> [0., 1., 1., 1., np.nan]
Cut x
into its deciles
x = tf.random.stateless_uniform(shape=(100, 200))
decile_edges = tfp.stats.quantiles(x, num_quantiles=10)
bins = tfp.stats.find_bins(x, edges=decile_edges)
bins.shape
==> (100, 200)
tf.reduce_mean(bins == 0.)
==> approximately 0.1
tf.reduce_mean(bins == 1.)
==> approximately 0.1
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.substrates.numpy.stats.find_bins\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/substrates/numpy/stats/quantiles.py#L160-L289) |\n\nBin values into discrete intervals.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tfp.experimental.substrates.numpy.stats.find_bins`](https://www.tensorflow.org/probability/api_docs/python/tfp/substrates/numpy/stats/find_bins)\n\n\u003cbr /\u003e\n\n tfp.substrates.numpy.stats.find_bins(\n x,\n edges,\n extend_lower_interval=False,\n extend_upper_interval=False,\n dtype=None,\n name=None\n )\n\nGiven `edges = [c0, ..., cK]`, defining intervals\n`I_0 = [c0, c1)`, `I_1 = [c1, c2)`, ..., `I_{K-1} = [c_{K-1}, cK]`,\nThis function returns `bins`, such that `x[i]` lies within `I_{bins[i]}`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | Numeric `N-D` `Tensor` with `N \u003e 0`. |\n| `edges` | `Tensor` of same `dtype` as `x`. The first dimension indexes edges of intervals. Must either be `1-D` or have `x.shape[1:] == edges.shape[1:]`. If `rank(edges) \u003e 1`, `edges[k]` designates a shape `edges.shape[1:]` `Tensor` of bin edges for the corresponding dimensions of `x`. |\n| `extend_lower_interval` | Python `bool`. If `True`, extend the lowest interval `I0` to `(-inf, c1]`. |\n| `extend_upper_interval` | Python `bool`. If `True`, extend the upper interval `I_{K-1}` to `[c_{K-1}, +inf)`. |\n| `dtype` | The output type (`int32` or `int64`). `Default value:` `x.dtype`. This effects the output values when `x` is below/above the intervals, which will be `-1/K+1` for `int` types and `NaN` for `float`s. At indices where `x` is `NaN`, the output values will be `0` for `int` types and `NaN` for floats. |\n| `name` | A Python string name to prepend to created ops. Default: 'find_bins' |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `bins` | `Tensor` with same `shape` as `x` and `dtype`. Has whole number values. `bins[i] = k` means the `x[i]` falls into the `kth` bin, ie, `edges[bins[i]] \u003c= x[i] \u003c edges[bins[i] + 1]`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------|\n| `ValueError` | If `edges.shape[0]` is determined to be less than 2. |\n\n\u003cbr /\u003e\n\n#### Examples\n\nCut a `1-D` array \n\n x = [0., 5., 6., 10., 20.]\n edges = [0., 5., 10.]\n tfp.stats.find_bins(x, edges)\n ==\u003e [0., 1., 1., 1., np.nan]\n\nCut `x` into its deciles \n\n x = tf.random.stateless_uniform(shape=(100, 200))\n decile_edges = tfp.stats.quantiles(x, num_quantiles=10)\n bins = tfp.stats.find_bins(x, edges=decile_edges)\n bins.shape\n ==\u003e (100, 200)\n tf.reduce_mean(bins == 0.)\n ==\u003e approximately 0.1\n tf.reduce_mean(bins == 1.)\n ==\u003e approximately 0.1"]]