|
| 1 | +--- |
| 2 | +Title: '.quantile()' |
| 3 | +Description: 'Computes the qth quantile of the input array along the specified axis.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Data' |
| 10 | + - 'Functions' |
| 11 | + - 'Methods' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.quantile()`** function in NumPy returns the qth quantile of an array along a specified axis. Quantiles are the division points that separate a data set into equal probabilities. For example, the 25th quantile is the point which 25% of the data set falls below. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, weights=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `a`: The input array containing the data to compute the quantiles from. |
| 28 | +- `q`: The quantile(s) to compute. This can be a float or array-like of floats between `0` and `1`, where `0.5` represents the median. |
| 29 | +- `axis` (Optional): The axis or axes on which to calculate the quantile. `axis=0` computes along columns, and `axis=1` computes along rows. If set to `None` (default), the input is flattened before computation. |
| 30 | +- `out` (Optional): Specifies a different array in which to place the result. It must have the same shape as the expected result. |
| 31 | +- `overwrite_input` (Optional): If `True`, the input array `a` may be modified to save memory. Default is `False`. |
| 32 | +- `method` (Optional): The method used to calculate the quantile. The default is `'linear'`. Valid options include: `'inverted_cdf'`, `'averaged_inverted_cdf'`, `'closest_observation'`, `'interpolated_inverted_cdf'`, `'hazen'`, `'weibull'`, `'median_unbiased'`, and `'normal_unbiased'`. |
| 33 | + `keepdims` (Optional): If `True`, the reduced axes are retained with size one, maintaining the number of dimensions in the output. |
| 34 | + `weights` (Optional): An array of weights corresponding to values in `a`, used to influence the quantile calculation. This parameter is only supported by the `'inverted_cdf'` method. The shape of `weights` must either match `a`, or be 1-dimensional with a length equal to `a` when flattened. |
| 35 | + |
| 36 | +**Return value:** |
| 37 | + |
| 38 | +The `.quantile()` function returns the qth quantile(s) of an array as a NumPy array ([`ndarray`](https://www.codecademy.com/resources/docs/numpy/ndarray)) or a scalar (`float64`) if the result is a single value. |
| 39 | + |
| 40 | +## Example: Computing multiple quantiles from data |
| 41 | + |
| 42 | +The following example creates an array and then uses `.quantile()` to calculate various quantiles from the data: |
| 43 | + |
| 44 | +```py |
| 45 | +import numpy as np |
| 46 | + |
| 47 | +a = np.array([[0,1,2],[3,4,5]]) |
| 48 | + |
| 49 | +print(np.quantile(a, .25)) |
| 50 | +# Computes the 25th quantile along a flattened axis |
| 51 | + |
| 52 | +print(np.quantile(a, .5, axis=0)) |
| 53 | +# Computes the 50th quantile along the vertical axis |
| 54 | + |
| 55 | +print(np.quantile(a, .5, axis=1)) |
| 56 | +# Computes the 50th quantile along the horizontal axis |
| 57 | + |
| 58 | +print(np.quantile(a, .75, axis=1, keepdims=True)) |
| 59 | +# Computes the 75th quantile along the horizontal axis, while retaining the original dimensions of the input array |
| 60 | +``` |
| 61 | + |
| 62 | +This code produces the following output: |
| 63 | + |
| 64 | +```shell |
| 65 | +1.25 |
| 66 | +[1.5 2.5 3.5] |
| 67 | +[1. 4.] |
| 68 | +[[1.5] |
| 69 | + [4.5]] |
| 70 | +``` |
| 71 | + |
| 72 | +## Codebyte Example |
| 73 | + |
| 74 | +The following codebyte example computes various quantiles for an input array, `a`: |
| 75 | + |
| 76 | +```codebyte/python |
| 77 | +import numpy as np |
| 78 | +
|
| 79 | +a = np.array([[0,2,4],[6,8,10],[12,14,16]]) |
| 80 | +
|
| 81 | +print("Array:") |
| 82 | +print(a) |
| 83 | +
|
| 84 | +print("\nThe 30th quantile of the array:") |
| 85 | +print(np.quantile(a, .3)) |
| 86 | +
|
| 87 | +print("\nThe 50th quantile along the horizontal axis (axis=1):") |
| 88 | +print(np.quantile(a, .5, axis=1)) |
| 89 | +
|
| 90 | +print("\nThe 50th quantile along the vertical axis (axis=0):") |
| 91 | +print(np.quantile(a, .5, axis=0)) |
| 92 | +
|
| 93 | +print("\nThe 90th quantile along the horizontal axis with keepdims=True:") |
| 94 | +print(np.quantile(a, .9, axis=1, keepdims=True)) |
| 95 | +``` |
0 commit comments