Reputation: 143
I have a list of numbers in the text.txt file.
2.50
2.56
2.81
2.86
2.84
3.21
3.47
2.91
2.96
3.11
2.83
2.89
2.94
2.94
3.34
3.44
2.94
2.96
3.04
3.01
2.85
3.05
3.10
i want to bin each numbers of set of range. like how many in a range.
2.5-2.7
2.7-2.9
2.9-3.1
3.1-3.3
3.3-3.5
i have try this.
from __future__ import division
from math import *
from numpy import *
from string import*
infile = open('text1.txt', 'r')
text = infile.read().split('\n')
infile.close()
text.remove('')
numbers = []
for i in text:
count = 0
if (numbers[i] > 2.49) and (numbers[i] < 2.59):
count += 1
print("Number of elements", count)
it is not working
Upvotes: 3
Views: 4527
Reputation: 19770
How about using more of the numpy
functions?
import numpy
numbers = numpy.loadtxt('test.txt')
bins = numpy.arange(2.5, 3.51, 0.2) # 3.5 won't work due to floating point issues
counts, _ = numpy.histogram(numbers, bins)
If you don't want to use numpy
, you can benefit by directly calculating what bin the numbers fall into for equal-size bins:
numbers = [float(n) for n in open('test.txt') if len(n.strip())]
start = 2.5
width = 0.2
end = 3.7
def position(n):
return int((n - start)/width)
counts = [0 for i in range(position(end))]
for n in numbers:
counts[position(n)] += 1
Upvotes: 6
Reputation: 53
You can use the following codes
infile = open('text1.txt','r')
text = infile.read().split('\n')
infile.close()
#text.remove('')
#calculate the numbers of each range
numbers = [0,0,0,0,0]
for i in text:
temp = float(i);
temp = (temp-2.5)*100
temp = int(temp)/20
numbers[temp] =numbers[temp] + 1
#display the numbers of each range
print 'Number of elements'
print '2.5-2.7: '+ str(numbers[0])
print '2.7-2.9: '+ str(numbers[1])
print '2.9-3.1: '+ str(numbers[2])
print '3.1-3.3: '+ str(numbers[3])
print '3.3-3.5: '+ str(numbers[4])
Upvotes: 0
Reputation: 9107
First, you can improve your file reading using readlines()
as such:
numbers = [float(i.strip()) for i in infile.readlines() if i is not '']
Next, for the bin counting, assuming the range of each bin is equal, you can create two variables specifying the start value and the delta:
start = 2.5
delta = 0.2
nBins = 5
Then you can use filter
to get the count of each range as such:
counts = [len(filter(lambda x: start+delta*i <= x < start+delta*(i+1), numbers)) for i in xrange(nBins)]
and print the results:
for i,count in enumerate(counts):
print "Number of elements in the range %.1f-%.1f: %d" % (start+delta*i,start+delta*(i+1),count)
Full code:
infile = open('text1.txt', 'r')
numbers = [float(i.strip()) for i in infile.readlines() if i is not '']
start = 2.5
delta = 0.2
nBins = 5
counts = [len(filter(lambda x: start+delta*i <= x < start+delta*(i+1), numbers)) for i in xrange(nBins)]
for i,count in enumerate(counts):
print "Number of elements in the range %.1f-%.1f: %d" % (start+delta*i,start+delta*(i+1),count)
Upvotes: 1
Reputation: 250991
You can use the bisect
module:
>>> import bisect
>>> ranges = [2.5, 2.7, 2.9, 3.1, 3.3, 3.5]
>>> nums = [2.5, 2.56, 2.81, 2.86, 2.84, 3.21, 3.47, 2.91, 2.96, 3.11, 2.83, 2.89, 2.94, 2.94, 3.34, 3.44, 2.94, 2.96, 3.04, 3.01, 2.85, 3.05, 3.1]
>>> lis = [0]*len(ranges)
for item in nums:
ind = bisect.bisect(ranges, item) - 1
lis[ind] += 1
for x, y in zip(zip(ranges, ranges[1:]), lis):
print x, y
...
(2.5, 2.7) 2
(2.7, 2.9) 6
(2.9, 3.1) 9
(3.1, 3.3) 3
(3.3, 3.5) 3
Upvotes: 6
Reputation: 113985
Try this:
ranges = [(2.5, 2.7), (2.7, 2.9), (2.9, 3.1), (3.1, 3.3), (3.3, 3.5)]
counts = {i:0 for i in ranges}
def findBin(n, bins):
mid = len(bins)/2
low, high = bins[mid]
if low <= n <= high:
return (low,high)
elif low >= n:
return findBin(n, bins[mid:])
else:
return findBin(n, bins[:mid])
with open('path/to/file') as infile:
for line in infile:
n = float(line.strip())
counts[findBin(n, ranges)] += 1
for low,high in sorted(counts):
print("There are", counts[(low,high)], "many numbers between", low, "and", high)
Hope this helps
Upvotes: 0
Reputation: 3871
That would not work, since you have nothing stored in numbers[].
numbers = []
count = 0
for i in text:
numbers.append(int(i))
count=count+1
count = 0
for i in text:
if (numbers[i] > 2.49) and (numbers[i] < 2.59):
count += 1
print("Number of elements", count)
Upvotes: 1