作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
寻找具有密集邻域的点
我有一组 (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/
考虑下面的两张图片: 图片1: 图2: 为了便于论证,我们假设这两个图像具有相同数量的黑色像素。 我想计算黑色像素的“浓度”。 IE。图像 1 比图像 2 具有更高的“索引”,因为黑色像素比图像 2
我是一名优秀的程序员,十分优秀!