[英]finding density of points along a line to find the region that has maximum concentration
密集社區的發現點
我有一組(count = 485)范圍在0.015到0.13之間的值(六位數精度)。
我想在python中使用numpy。
我努力了
b = []
with open('data.txt') as infile:
lines = infile.readlines()
for line in lines:
for n in line.split()[2:3]:
b.append(n)
xa = np.asarray(b,dtype=float)
print 'mode-',stats.mode(xa)
這里data.txt在第三列中有值。 這給出了多次出現的價值。 就我而言,0.06出現了兩次。 因此,以上代碼不適用於我的情況。
如果在'x'處有一個峰,是否可以有一個圖可以用來解釋點x具有最密集的鄰域。
我無法定義“鄰里關系”。 您可以自己決定。
謝謝。
您可以使用matplotlib.pyplot.hist
繪制直方圖,以顯示峰值。 您還可以使用np.histogram()
來返回相同的結果。 編輯:我現在在直方圖頻率結果上使用了np.argmax()
來找到我們最大的窗口。 還在直方圖中繪制了一條線以顯示最大頻率。
您也可以檢出numpy.genfromtxt()
或pandas.read_csv()
以便於打開文件。
import matplotlib.pyplot as plt
import numpy as np
#Synthetic Data
dat=np.random.uniform(0.015,0.13,485)#(count=485) of values in range 0.015 to 0.13
binsize=20 #number of windows you want
hist=plt.hist(dat,bins=binsize) #Plot and get our histogram values
#hist=np.histogram(dat,bins=binsize) #will also work, just not plot the answer.
#hist[0] is frequencies and hist[1] is x value
ans=hist[1][np.argmax(hist[0])]
print('Answer: '+str(ans))
buffer=(hist[1][2]-hist[1][1])/2 #Just to centre the black line
plt.axvline(ans+buffer,c='k') #Draw a line of centre
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.