我try 使用函数直方图将一个变量(SST)作为另一个变量(TCWV)的函数进行采样,并将权重设置为示例变量,如下所示:

# average sst over bins
num, _   = np.histogram(tcwv, bins=bins)
sstsum, _ = np.histogram(tcwv, bins=bins,weights=sst)
out=np.zeros_like(sstsum)
out[:]=np.nan
sstav  = np.divide(sstsum,num,out=out, where=num>100)

重现性的完整代码如下所示.我的问题是,当我绘制原始数据的散点图,然后绘制计算出的平均值时,平均值就像这样位于数据"云"之外(请参见右侧的点):

enter image description here

我想不出为什么会发生这种情况,除非这可能是一个舍入误差?

这是我的全部代码:

import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset

# if you have a recent netcdf libraries you can access it directly here 
url = ('http://clima-dods.ictp.it/Users/tompkins/CRM/data/WRF_1min_mem3_grid4.nc#mode=bytes')
ds=Dataset(url)

### otherwise need to download, and use this:
###ifile="WRF_1min_mem3_grid4.nc"
###ds=Dataset(idir+ifile)


# axis bins
bins=np.linspace(40,80,21)

iran1,iran2=40,60

# can put in dict and loop 
sst=ds.variables["sst"][iran1:iran2+1,:,:]
tcwv=ds.variables["tcwv"][iran1:iran2+1,:,:]

# don't need to flatten, just tried it to see if helps (it doesn't)
sst=sst.flatten()
tcwv=tcwv.flatten()

# average sst over bins
num, _   = np.histogram(tcwv, bins=bins)
sstsum, _ = np.histogram(tcwv, bins=bins,weights=sst)
out=np.zeros_like(sstsum)
out[:]=np.nan
sstav  = np.divide(sstsum,num,out=out,where=num>100)

# bins centroids
avbins=(np.array(bins[1:])+np.array(bins[:-1]))/2

#plot
subsam=2
fig,(ax)=plt.subplots()
plt.scatter(tcwv.flatten()[::subsam],sst.flatten()[::subsam],s=0.05,marker=".")
plt.scatter(avbins,sstav,s=3,color="red")
plt.ylim(299,303)
plt.savefig("scatter.png")

推荐答案

我想不出为什么会发生这种情况,除非这可能是一个舍入误差?

这实际上是一个舍入误差.

具体地说,当您在这里计算每个bin中的sst总和时:

sstsum, _ = np.histogram(tcwv, bins=bins,weights=sst)

与我try 的两种计算总和的替代方法相比,结果错误了0.1%.

对于如何解决这个问题,我有两个 idea .

方法1

最简单的解决办法是以更精确的方式进行计算.

sstsum, _ = np.histogram(tcwv, bins=bins,weights=sst.astype('float64'))

如果不进行此更改,sst的dtype为Float32.

方法2

出于性能原因,您可能希望将计算保持在32位浮点数中.它们比64位浮点数要快一些.另一种解决方案是在求和之前减go 平均值,以提高数值 solidity .

sst_mean = sst.mean()
num, _   = np.histogram(tcwv, bins=bins)
sstsum, _ = np.histogram(tcwv, bins=bins,weights=sst - sst_mean)
out=np.zeros_like(sstsum)
out[:]=np.nan
sstav  = np.divide(sstsum,num,out=out,where=num>100)
sstav += sst_mean

这将从每个数据点减go sst的总体平均值,然后将其加回到末尾.由于浮点数在0附近具有更高的精度,这使得计算更精确.

比较

以下是第一种方法的图表:

plot of sst vs tcwv done in higher precision

方法2的情节看起来是一样的.这两种方法在彼此的1.32*10-5范围内相等.

Python相关问答推荐

如何终止带有队列的Python进程?+ 队列大小的错误?

如何在PIL、Python中对图像应用彩色面膜?

如何销毁框架并使其在tkinter中看起来像以前的样子?

如何防止Plotly在输出到PDF时减少行中的点数?

计算所有前面行(当前行)中列的值

Locust请求中的Python和参数

使用mySQL的SQlalchemy过滤重叠时间段

按顺序合并2个词典列表

为什么默认情况下所有Python类都是可调用的?

Python,Fitting into a System of Equations

运输问题分支定界法&

将tdqm与cx.Oracle查询集成

基于索引值的Pandas DataFrame条件填充

pandas:排序多级列

如何使用OpenGL使球体遵循Python中的八样路径?

在Admin中显示从ManyToMany通过模型的筛选结果

使用__json__的 pyramid 在客户端返回意外格式

处理Gekko的非最优解

如何防止html代码出现在quarto gfm报告中的pandas表之上

用来自另一个数据框的列特定标量划分Polars数据框中的每一列,