Finding point with dense neighbourhood
I have a set (count=485) of values in range 0.015 to 0.13 with six digit precision.
I want to use numpy in python .
I have tried
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)
Here data.txt has values in third column. This gives value that appears multiple times . In my case 0.06 appeared twice. So, above code does not work for my case.
Can there be a graph using which it can be interpreted that point x has densest neighbourhood if there is a peak at 'x' .
I am not able to define 'neigbourhood' . You can decide by yourself .
Thanks.
you can use matplotlib.pyplot.hist
to plot a histogram that will show you a peak. You can also use np.histogram()
which will return identical results. Edit: I have used np.argmax()
here now on the histogram frequency results to find our largest window. Have also plotted a line on the histogram to show heighest frequency.
You can also check out numpy.genfromtxt()
or pandas.read_csv()
for easy opening of files.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.