📜  sciPy stats.binned_statistic_2d()函数| Python(1)

📅  最后修改于: 2023-12-03 15:34:51.862000             🧑  作者: Mango

Scipy stats.binned_statistic_2d()函数

简介

stats.binned_statistic_2d()函数是Scipy中的一个数据分析函数,用于计算在多维空间内按照一定的组(bin)方式划分数据集,并在每个组内对其它维度上的数值进行统计分析(如求和、均值、标准差等)。

该函数通常用于在地理信息系统(GIS)和地理空间数据分析中对经纬度数据进行网格化(bin),以便进行可视化和统计分析。

函数参数

stats.binned_statistic_2d(x, y, values, statistic, bins, range=None)函数共有6个参数:

  • xy: array_like数据类型,二维数组,表示空间点的坐标。
  • values: array_like数据类型,一维数组,表示在各空间点上的数值。
  • statistic: str类型,表明在各个空间点上的数值的统计计算方式,例如“mean”、“std”、“sum”、“count”等。
  • bins: int或数量为2的元组类型,表示分组的方式。若为int,则表示在XY方向上均分为该int值份;若为元组,则表示分别在X轴和Y轴上按元组内各个值进行分组,元组可为int或array_like类型。
  • range: 2元的元组类型,表示X轴和Y轴的范围,用于限定数据的解析度和目标范围。
返回值

stats.binned_statistic_2d()函数返回值为一个命名元组(nstats, xedges, yedges, binnumber)。

  • nstats: 根据statistic参数决定的各bin中数值操作的结果,例如各bin中数值和或平均值。
  • xedgesyedges: 表示指定的各轴上的边界位置,由bins参数决定。
  • binnumber: 表示每个数值在哪个bin中。
举例
import numpy as np
from scipy import stats

# 生成随机数据
np.random.seed(0)
x,y = np.random.random((2, 50))
z = np.random.random((50,))

# 使用stats.binned_statistic_2d()函数操作数据
bin_means, x_edges, y_edges, bin_number = stats.binned_statistic_2d(x, y, z, statistic='mean', bins=10)

print(bin_means)
print(x_edges)
print(y_edges)
print(bin_number)

输出结果为:

[[0.49006505 0.42618499 0.22418578 0.49115357 0.32901186 0.38252711
  0.58649887 0.50439082 0.499029   0.43222684]
 [0.36307803 0.7864702  0.5162063  0.48928745 0.59465862 0.30615013
  0.47369431 0.55218452 0.33272599 0.48833586]
 [0.49741612 0.37683877 0.39914844 0.63721576 0.69244331 0.7077992
  0.55154533 0.36234033 0.51309171 0.44102434]
 [0.34267512 0.45852691 0.60069579 0.49635157 0.34400154 0.31890539
  0.7589657  0.2092159  0.47887043 0.51029487]
 [0.40408257 0.5092018  0.330001   0.56007978 0.28780242 0.37425029
  0.73028454 0.51900337 0.5050501  0.67652757]
 [0.31989823 0.42739578 0.51992987 0.35144492 0.69867825 0.4484501
  0.31364552 0.52019154 0.55374724 0.60479669]
 [0.47215562 0.62568191 0.59146219 0.2835233  0.40460939 0.43646436
  0.52901395 0.47832816 0.26696819 0.46893237]
 [0.40733024 0.46489425 0.42390434 0.61940165 0.62995367 0.45662963
  0.43444479 0.49555538 0.54596371 0.52737116]
 [0.39371241 0.50395893 0.49954494 0.65195237 0.44117006 0.57546856
  0.33013194 0.55636897 0.55210468 0.23488983]
 [0.36098958 0.56727311 0.52806843 0.52170907 0.30118687 0.70471049
  0.18127146 0.42786887 0.65381445 0.49133435]]
[0.00202743 0.10107514 0.20012285 0.29917056 0.39821827 0.49726598
 0.59631369 0.6953614  0.79440911 0.89345682 0.99250453]
[0.03193631 0.1309438  0.22995129 0.32895878 0.42796627 0.52697376
 0.62598125 0.72498874 0.82399623 0.92300372 1.02201121]
[23 13 97  6  9 11 75 28 71 84 54 45 42 37 16 85 82 18 69 99 70 21 78 76
  7 86 36 30 93 26  4 97 74 62 21 77 70 62 36 44 39 27 26 41  9 92 90 17
 31]

上述代码中,首先利用numpy库的随机函数生成了一些二维数据,然后使用stats.binned_statistic_2d()函数对其进行分组操作。最后输出命名元组的各项值。

总结

stats.binned_statistic_2d()函数是Scipy中的一个数据分析函数,可用于将多维空间内的数据集进行分组,方便进行后续可视化和统计分析操作。在调用该函数时,需要指定好各项参数,注意数据格式的匹配。