Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在分布中的数据点所在位置绘制数据点_Python_Arrays_Numpy_Matplotlib_Distribution - Fatal编程技术网

Python 在分布中的数据点所在位置绘制数据点

Python 在分布中的数据点所在位置绘制数据点,python,arrays,numpy,matplotlib,distribution,Python,Arrays,Numpy,Matplotlib,Distribution,假设我有一个大的数据集,在那里我可以在某种分析中操作它。它可以是概率分布中的值 现在我有了这个大数据集,我想将已知的、实际的数据与之进行比较。首先,我的数据集中有多少值与已知数据具有相同的值或属性。例如: 这是一个累积分布。连续线来自模拟生成的数据,减少的强度只是预测的百分比。然后根据生成的数据绘制观测(已知)数据 我举的另一个例子是如何在视觉上将点投影到直方图上: 我很难标记已知数据点在生成的数据集中的位置,并沿着生成的数据的分布累积绘制它 如果我尝试检索掉在生成数据附近的点数,我会这样开

假设我有一个大的数据集,在那里我可以在某种分析中操作它。它可以是概率分布中的值

现在我有了这个大数据集,我想将已知的、实际的数据与之进行比较。首先,我的数据集中有多少值与已知数据具有相同的值或属性。例如:

这是一个累积分布。连续线来自模拟生成的数据,减少的强度只是预测的百分比。然后根据生成的数据绘制观测(已知)数据

我举的另一个例子是如何在视觉上将点投影到直方图上:

我很难标记已知数据点在生成的数据集中的位置,并沿着生成的数据的分布累积绘制它

如果我尝试检索掉在生成数据附近的点数,我会这样开始(这不对):

def SameValue(SimData、DefData、uncert):
numb=[(DefData uncert)

但我很难计算出落在值范围内的点,然后将其全部设置到可以绘制的位置。你知道如何收集这些数据并将其投射到一个累积分布上吗?

这个问题非常混乱,有很多不相关的信息,但在关键点上仍然模糊不清。我会尽力把它翻译出来

我认为你所追求的是:给定一个未知分布的有限样本,以固定值获得新样本的概率是多少

我不确定是否有一个普遍的答案,但无论如何,这将是一个问题,要问统计或数学的人。我的猜测是,您需要对分布本身进行一些假设

然而,在实际情况下,找出新值将位于采样分布的哪个箱子就足够了

因此,假设我们有一个分布
x
,我们将其划分为
。我们可以使用
numpy.histogram
计算直方图
h
。然后通过
h/h.sum()

有一个值
v=0.77
,我们想知道根据分布的概率,我们可以通过在bin数组中查找索引
ind
来找出它所属的bin,在该索引中,需要插入该值才能使数组保持排序。这可以通过使用

因此,概率是5.8%,在0.77左右的容器中对一个值进行采样

另一种选择是在bin中心之间插入直方图,以找到概率

在下面的代码中,我们绘制了一个类似于问题中图片的分布,并使用两种方法,第一种用于频率直方图,第二种用于累积分布

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt

x = np.random.rayleigh(size=1000)
y = np.random.normal(size=1000)
bins = np.linspace(0,4,41)
h, bins_ = np.histogram(x, bins=bins)
hcum = np.cumsum(h)/float(np.cumsum(h).max())

points = [[.77,-.55],[1.13,1.08],[2.15,-.3]]
markers = [ur'$\u2660$',ur'$\u2665$',ur'$\u263B$']
colors = ["k", "crimson" , "gold"]
labels = list("ABC")

kws = dict(height_ratios=[1,1,2], hspace=0.0)
fig, (axh, axc, ax) = plt.subplots(nrows=3, figsize=(6,6), gridspec_kw=kws, sharex=True)

cbins = np.zeros(len(bins)+1)
cbins[1:-1] = bins[1:]-np.diff(bins[:2])[0]/2.
cbins[-1] = bins[-1]
hcumc = np.linspace(0,1, len(cbins))
hcumc[1:-1] = hcum
axc.plot(cbins, hcumc, marker=".", markersize="2", mfc="k", mec="k" )
axh.bar(bins[:-1], h, width=np.diff(bins[:2])[0], alpha=0.7, ec="C0", align="edge")
ax.scatter(x,y, s=10, alpha=0.7)

for p, m, l, c in zip(points, markers, labels, colors):
    kw = dict(ls="", marker=m, color=c, label=l, markeredgewidth=0, ms=10)
    # plot points in scatter distribution
    ax.plot(p[0],p[1], **kw)
    #plot points in bar histogram, find bin in which to plot point
    # shift by half the bin width to plot it in the middle of bar
    pix = np.searchsorted(bins, p[0], side="right")
    axh.plot(bins[pix-1]+np.diff(bins[:2])[0]/2., h[pix-1]/2., **kw)
    # plot in cumulative histogram, interpolate, such that point is on curve.
    yi = np.interp(p[0], cbins, hcumc)
    axc.plot(p[0],yi, **kw)
ax.legend()
plt.tight_layout()  
plt.show()

对于那些对我的帖子投了否决票的人,你能详细解释一下为什么我可以改进我做错的事情吗?非常感谢你花时间给出一个简洁的答案。我要看看我是否能用我的数据来做这件事,看看我能从那里走到哪里。
import numpy as np; np.random.seed(0)

x = np.random.rayleigh(size=1000)
bins = np.linspace(0,4,41)
h, bins_ = np.histogram(x, bins=bins)
prob = h/float(h.sum())

ind = np.searchsorted(bins, 0.77, side="right")
print prob[ind] # which prints 0.058
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt

x = np.random.rayleigh(size=1000)
y = np.random.normal(size=1000)
bins = np.linspace(0,4,41)
h, bins_ = np.histogram(x, bins=bins)
hcum = np.cumsum(h)/float(np.cumsum(h).max())

points = [[.77,-.55],[1.13,1.08],[2.15,-.3]]
markers = [ur'$\u2660$',ur'$\u2665$',ur'$\u263B$']
colors = ["k", "crimson" , "gold"]
labels = list("ABC")

kws = dict(height_ratios=[1,1,2], hspace=0.0)
fig, (axh, axc, ax) = plt.subplots(nrows=3, figsize=(6,6), gridspec_kw=kws, sharex=True)

cbins = np.zeros(len(bins)+1)
cbins[1:-1] = bins[1:]-np.diff(bins[:2])[0]/2.
cbins[-1] = bins[-1]
hcumc = np.linspace(0,1, len(cbins))
hcumc[1:-1] = hcum
axc.plot(cbins, hcumc, marker=".", markersize="2", mfc="k", mec="k" )
axh.bar(bins[:-1], h, width=np.diff(bins[:2])[0], alpha=0.7, ec="C0", align="edge")
ax.scatter(x,y, s=10, alpha=0.7)

for p, m, l, c in zip(points, markers, labels, colors):
    kw = dict(ls="", marker=m, color=c, label=l, markeredgewidth=0, ms=10)
    # plot points in scatter distribution
    ax.plot(p[0],p[1], **kw)
    #plot points in bar histogram, find bin in which to plot point
    # shift by half the bin width to plot it in the middle of bar
    pix = np.searchsorted(bins, p[0], side="right")
    axh.plot(bins[pix-1]+np.diff(bins[:2])[0]/2., h[pix-1]/2., **kw)
    # plot in cumulative histogram, interpolate, such that point is on curve.
    yi = np.interp(p[0], cbins, hcumc)
    axc.plot(p[0],yi, **kw)
ax.legend()
plt.tight_layout()  
plt.show()