gpt4 book ai didi

python - 创建直方图时考虑错误

转载 作者:太空狗 更新时间:2023-10-30 01:33:59 25 4
gpt4 key购买 nike

我有一组 N 观察值,分布为 (x[i], y[i]), i=0..N 二维空间中的点。每个点在两个坐标 (e_x[i], e_y[i], i=0..N) 和附加到它的权重 (w[i], i =0..N).

我想生成这些 N 点的二维直方图,不仅要考虑权重还要考虑误差,这会导致每个点都被传播 如果错误值足够大,则可能在许多 bin 中(假设错误的标准 Gaussian distribution,尽管可能会考虑其他分布)。

我看到了 numpy.histogram2d有一个 weights 参数,以便处理。问题是如何解释每个 N 观察点中的错误。

是否有一个函数可以让我这样做?我对 numpyscipy 中的任何东西都持开放态度。

最佳答案

根据 user1415946 的评论,您可以假设每个点代表一个 bi-variate normal distribution[[e_x[i]**2,0][0,e_y[i]**2]] 给出的协方差矩阵。但是,生成的分布不是正态分布 - 在运行该示例后,您会看到直方图根本不像高斯分布,而是一组高斯分布。

要根据这组分布创建直方图,我看到的一种方法是使用 numpy.random.multivariate_normal 从每个点生成随机样本.请参阅下面的示例代码和一些人工数据。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


# This is a function I like to use for plotting histograms
def plotHistogram3d(hist, xedges, yedges):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
hist = hist.transpose()
# Transposing is done so that bar3d x and y match hist shape correctly
dx = np.mean(np.diff(xedges))
dy = np.mean(np.diff(yedges))

# Computing the number of elements
elements = (len(xedges) - 1) * (len(yedges) - 1)
# Generating mesh grids.
xpos, ypos = np.meshgrid(xedges[:-1]+dx/2.0, yedges[:-1]+dy/2.0)

# Vectorizing matrices
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = dx * np.ones_like(zpos) * 0.5 # 0.5 factor to give room between bars.
# Use 1.0 if you want all bars 'glued' to each other
dy = dy * np.ones_like(zpos) * 0.5
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Count')
return

"""
INPUT DATA
"""
# x y ex ey w
data = np.array([[1, 2, 1, 1, 1],
[3, 0, 1, 1, 2],
[0, 1, 2, 1, 5],
[7, 7, 1, 3, 1]])

"""
Generate samples
"""
# Sample size (100 samples will be generated for each data point)
SAMPLE_SIZE = 100
# I want to fill in a table with columns [x, y, w]. Each data point generates SAMPLE_SIZE
# samples, so we have SAMPLE_SIZE * (number of data points) generated points
points = np.zeros((SAMPLE_SIZE * data.shape[0], 3)) # Initializing this matrix

for i, element in enumerate(data): # For each row in the data set
meanVector = element[:2]
covarianceMatrix = np.diag(element[2:4]**2) # Diagonal matrix with elements equal to error^2
# For columns 0 and 1, add generated x and y samples
points[SAMPLE_SIZE*i:SAMPLE_SIZE*(i+1), :2] = \
np.random.multivariate_normal(meanVector, covarianceMatrix, SAMPLE_SIZE)
# For column 2, simply copy original weight
points[SAMPLE_SIZE*i:SAMPLE_SIZE*(i+1), 2] = element[4] # weights

hist, xedges, yedges = np.histogram2d(points[:, 0], points[:, 1], weights=points[:, 2])
plotHistogram3d(hist, xedges, yedges)
plt.show()

结果绘制如下:

enter image description here

关于python - 创建直方图时考虑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com