# sciPy stats.binned_statistic_2d () function | python

| | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

` stats.binned_statistic_2d (arr1, arr2, values, statistic = & # 39; mean & # 39 ;, bins = 10, range = None) ` calculates the statistic value for given two-dimensional data.
It works in a similar way to histogram2d. Since the histogram function makes bins and counts no. points in each basket; this function calculates the sum, average, mean -median-mode-in-python-without-libraries/">median, count, or other statistics of values ‚Äã‚Äãfor each bin.

Parameters:
arr1: [array_like] input array to be binned along the first dimension.
arr2: [array_like] input array to be binned along the second dimension.
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 along first and second dimension; bin number.

Code # 1:

 ` # stats.binned_statistic_2d () method ` ` import ` ` numpy as np ` ` from ` ` scipy ` ` import ` ` stats ` ` x ` ` = ` ` np.random.rand (` ` 10 ` `) ` ` y ` ` = ` ` np.random.rand (` ` 10 ` `) ` ` z ` ` = ` ` np.arange (` ` 10 ` `) ` ` print ` ` (` ` "x:" ` ` , x) ` ` print ` ` (` ` "y : "` `, y) ` ` print ` ` ( ` ` "z:" ` `, z) ` ` ` ` # amount ` ` print ` ` (` ` "binned_statistic_2d for count:" ` `, ` ` stats.binned_statistic_2d (x, y, values ‚Äã‚Äã` ` = ` ` z , ` ` statistic ` ` = ` `’ count’ ` `, bins ` ` = ` ` [` ` 5 ` `, ` ` 5 ` `])) `

Output:

x:
[0.31218238 0.86791445 0.42763346 0.79798587 0.91361299 0.09005856
0.54419846 0.18973948 0.67016378 0.8083121]

y:
[0.35959238 0.69265819 0.18751529 0.98863414 0.97810927 0.24054104
0.76764562 0.60635485 0.61551806 0.63884672]

1: 2 3 4 5 6 7 8 9]

binned_statistic_2d for count: BinnedStatistic2dResult (statistic = array ([[1., 0., 1., 0., 0.],
[0 ., 1., 0., 0., 0.],
[1., 0., 0., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 1., 1., 2.]]), x_edge = array ([0.09005856, 0.25476945, 0.41948033, 0.58419122, 0.74890211,
0.91361299 ]), y_edg e = array ([0.18751529, 0.34773906, 0.50796283, 0.6681866, 0.82841037,
0.98863414]), binnumber = array ([16, 39, 22, 40, 40, 8, 25, 10, 31, 38], dtype = int64))

Code # 2:

` `

 ` # stats.binned_statistic_2d () method ` ` import ` ` numpy as np ` ` from ` ` scipy ` ` import ` ` stats ` ` x ` ` = ` ` np.random.rand (` ` 10 ` `) ` ` y ` ` = ` ` np.random.rand (` ` 10 ` `) ` ` z ` ` = ` ` np.arange (` ` 10 ` `) ` ` # greedy ` ` print ` ` (` ` "binned_statistic_2d for mean : "` `, ` ` ` ` stats.binned_statistic_2d ( x, y, values ‚Äã‚Äã` ` = ` ` z, ` ` statistic ` ` = ` `’ mean ’ ` `, bins ` ` = ` ` [` ` 5 ` `, ` ` 5 ` `])) `
` `

` `

Output:

binned_statistic_2d for mean : BinnedStatistic2dResult (statistic = array ([[5., nan, 7., nan, nan],
[nan, 0., nan, nan, nan],
[2., nan, nan, 6., nan],
[nan, nan, 8., nan, nan],
[nan, nan, 9., 1., 3.5]]), x_edge = array ([0.09005856, 0.25476945, 0.41948033, 0.58419122, 0.74890211,
0.91361299]), y_edge = array ([0.18751529, 0.34773906, 0.50796283, 0.6681866, 0.82841037,
0.98863414]), binnumber = array ([16, 39, 22, 40, 40, 8, 25, 10, 31, 38], dtype = int64))

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from sciPy stats.binned_statistic_2d () function | python, check other array Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Angelo Jackson

Warsaw | 2023-02-02

I was preparing for my coding interview, thanks for clarifying this - sciPy stats.binned_statistic_2d () function | python in Python is not the simplest one. Checked yesterday, it works!

Boris Williams

Warsaw | 2023-02-02

Thanks for explaining! I was stuck with sciPy stats.binned_statistic_2d () function | python for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Davies Krasiko

London | 2023-02-02

imp Python module is always a bit confusing 😭 sciPy stats.binned_statistic_2d () function | python is not the only problem I encountered. Checked yesterday, it works!

## 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