sciPy stats.binned_statistic_2d()函数| Python
stats.binned_statistic_2d(arr1, arr2, values, statistic='mean', bins=10, range=None)
函数计算给定二维数据的分箱统计值。
它的工作原理类似于 histogram2d。由于直方图函数制作垃圾箱并计算编号。每个 bin 中的点数;此函数计算每个 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, 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.
代码#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 : \n", x)
print ("\ny : \n", y)
print ("\nz : \n", z)
# count
print ("\nbinned_statistic_2d for count : ",
stats.binned_statistic_2d(x, y, values = z,
statistic ='count', bins = [5, 5]))
输出 :
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]
z :
[0 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_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))
代码#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)
# mean
print ("\nbinned_statistic_2d for mean : ",
stats.binned_statistic_2d(x, y, values = z,
statistic ='mean', bins = [5, 5]))
输出 :
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))