2017-04-01 60 views
-1

可以說我有一個大型數據集,可以在某種分析中操縱它。其中可以查看概率分佈中的值。繪製數據點的位置分佈在哪裏

現在我有了這個大數據集,然後我想比較已知的實際數據。主要是,我的數據集中有多少個值與已知數據具有相同的值或屬性。例如:

enter image description here

這是一個累積分佈。連續線來自模擬產生的數據,而降低的強度只是預測的百分比。然後觀測星星(已知)數據,並將其與生成的數據作圖。

我已又如視覺上有多點可能可能上的直方圖被投影:

enter image description here

我有難度的標記,其中已知數據點在所生成的數據集和繪圖落入它累積地與生成的數據的分佈一致。

如果我嘗試和檢索落在產生的數據的附近點的數量,我就開始了這樣的(它不是右):

def SameValue(SimData, DefData, uncert): 
    numb = [(DefData-uncert) < i < (DefData+uncert) for i in SimData] 
    return sum(numb) 

但我有麻煩佔對於落在數值範圍內的點,然後將它全部設置到我可以繪製它的位置。關於如何收集這些數據並將其投影到累積分佈上的想法?

+0

給誰就給誰downvoted我的帖子,你會爲什麼這樣我就可以不管我做錯了完善詳細點嗎? – DarthLazar

回答

1

這個問題很混亂,有很多不相關的信息,但在基本點上保持模糊。我會盡我所能解釋它。

我認爲你在做什麼之後是:給定一個來自未知分佈的有限樣本,獲得一個固定值的新樣本的概率是多少?

我不確定是否有一個普遍的答案,但無論如何這將是一個問題,要求統計或數學人。我的猜測是,你需要對分配本身做一些假設。

但是,對於實際情況,找出新值將位於採樣分佈的哪個分箱中可能就足夠了。

因此,假設我們有一個分配x,我們分爲bins。我們可以使用numpy.histogram來計算直方圖h。然後由h/h.sum()給出在每個箱中找到一個值的概率。
有一個值v=0.77,其中我們想知道根據分佈的概率,我們可以通過查找bin數組中的索引ind來找到它所屬的bin,其中需要插入此值該數組保持排序。這可以使用numpy.searchsorted完成。

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 

所以概率爲5.8%的樣本在0.77左右的bin中取值。

一個不同的選擇是插入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() 

enter image description here

+0

非常感謝您花時間給出一個簡潔的答案。我會看看是否可以使用我的數據進行此項工作,並確定我可以從哪裏開始。 – DarthLazar