Change language

# sciPy function stats.binned_statistic () | python

` stats.binned_statistic (x, values, statistic = & # 39; mean & # 39 ;, bins = 10, range = None) ` calculates statistic value for given data (array elements).
It works similarly to the mean -median-mode-in-python-without-libraries/">median, count, or other statistics of values ​​for each bin.

Parameters:
arr: [array_like] input array to be binned.
values: [array_like] on which stats to be calculated.
statistics: Statistics to compute { mean , count, mean -median-mode-in-python-without-libraries/">median, sum, function}. Default is mean .
bin: [int or scalars] If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges.
range: (float, float) Lower and upper range of the bins and if not provided, range is from x.max () to x.min ().

Results: Statistics value for each bin; bin edges; bin number.

Code # 1:

 ` # stats.binned_statistic () method ` ` import ` ` numpy as np ` ` from ` ` scipy ` ` import ` ` stats `   ` # 1D array ` ` arr ` ` = ` ` [` ` 20 ` `, ` ` 2 ` `, ` ` 7 ` `, ` ` 1 ` `, ` ` 34 ` `] ` ` print ` ` ( "arr:" , arr) ``     # mean -median-mode-in-python-without-libraries/">median print ( "binned_statistic for mean -median-mode-in-python-without-libraries/">median:" , stats.binned_statistic ( arr, np.arange ( 5 ), statistic = ’mean -median-mode-in-python-without-libraries/">median’ , bins = 4 )) `

Output:

` arr: [20, 2, 7, 1, 34] binned_statistic for mean -median-mode-in-python-without-libraries/">median: BinnedStatisticResult (statistic = array ([2., nan, 0., 4.]), bin_edges = array ([1 ., 9.25, 17.5, 25.75, 34.]), binnumber = arr ay ([3, 1, 1, 1, 4], dtype = int64)) `

Code # 2:

 ` # stats.binned_statistic () method ` ` import ` ` numpy as np ` ` from ` ` scipy ` ` import ` ` stats `   ` # greedy ` ` arr ` ` = ` ` [` ` 20 ` `, ` ` 2 ` `, ` ` 7 ` `, ` ` 1 ` `, ` ` 34 ` `] ` ` print ` ` (` `" binned_statistic for mean : "` `, stats.binned_statistic (` ` arr, np.arange (` ` 5 ` `), statistic ` ` = ` `’ mean ’ ` `, bins ` ` = ` ` 2 ` `)) `

Output:

` binned_statistic for  mean : BinnedStatisticResult (statistic = array ([2., 2.]), bin_edges = array ([1., 17.5, 34.]), binnumber = array ([2, 1, 1, 1, 2], dtype = int64)) `

## Shop

Learn programming in R: courses

\$FREE

Best Python online courses for 2022

\$FREE

Best laptop for Fortnite

\$399+

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries