sciPy stats.binned_statistic_dd()函数| Python
stats.binned_statistic_dd(arr, values, statistic='mean', bins=10, range=None)
函数计算给定二维数据的分箱统计值。
它的工作原理类似于 histogram2d。由于直方图函数制作垃圾箱并计算编号。每个 bin 中的点数;此函数计算每个 bin 的值的总和、平均值、中值、计数或其他统计信息。
Parameters :
arr : [array_like] Data to histogram passed as (N, D) array
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; bin number.
代码#1:
# stats.binned_statistic_dd() method
import numpy as np
from scipy import stats
x = np.ones(10)
y = np.ones(10)
print ("x : \n", x)
print ("\ny : \n", y)
print ("\nbinned_statistic_2d for count : ",
stats.binned_statistic_dd([x, y], None, 'count', bins = 3))
输出 :
x :
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
y :
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
binned_statistic_2d for count : BinnedStatisticddResult(statistic=array([[ 0., 0., 0.],
[ 0., 10., 0.],
[ 0., 0., 0.]]), bin_edges=[array([0.5, 0.83333333, 1.16666667, 1.5 ]),
array([0.5, 0.83333333, 1.16666667, 1.5 ])],
binnumber=array([12, 12, 12, 12, 12, 12, 12, 12, 12, 12], dtype=int64))
代码#2:
# importing libraries
import numpy as np
from scipy import stats
# using np.ones for x and y
x = np.ones(10)
y = np.ones(10)
# Using binned_statistic_dd
print ("\nbinned_statistic_2d for count : ",
stats.binned_statistic_dd([x, y], None,
'count', bins=3, range=[[2,3],[0,0.5]]))
输出 :
binned_statistic_2d for count : BinnedStatisticddResult(statistic=array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]), bin_edges=[array([2., 2.33333333, 2.66666667, 3. ]),
array([0., 0.16666667, 0.33333333, 0.5 ])],
binnumber=array([4, 4, 4, 4, 4, 4, 4, 4, 4, 4], dtype=int64))