gpt4 book ai didi

python - 查找沿线的点密度以查找具有最大浓度的区域

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:30 25 4
gpt4 key购买 nike

寻找具有密集邻域的点

我有一组 (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()

关于python - 查找沿线的点密度以查找具有最大浓度的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323033/

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