简体   繁体   中英

Why does the following loop keep returning me results of 0?

So I'm trying to generate a frequency spectrum, and in order to do that I'm trying to sort my data into bins from a column, which I have appended data from, called minorallelefreq. For some reason, the code is not working because I get a value of 0 for all 5 bins.

Here's the code:

minprop = 0
minprop1 = 0
minprop2 = 0
minprop3 = 0
minprop4 = 0 

for x in range(1,100):

    if minorallelefreq[x] <= 0.1:
        minprop = minprop + 1 
    if minorallelefreq[x] > 0.1 and minorallelefreq[x] <= 0.2: 
        minprop1 = minprop1 + 1 
    if minorallelefreq[x] > 0.2 and minorallelefreq[x] <= 0.3: 
        minprop2 = minprop2 + 1
    if minorallelefreq[x] > 0.3 and minorallelefreq[x] <= 0.4: 
        minprop3 = minprop3 + 1
    if minorallelefreq[x] > 0.4 and minorallelefreq[x] <= 0.5:
        minprop4 = minprop4 + 1



bin1 = minprop/float(counter)
bin2 = minprop1/float(counter)
bin3 = minprop2/float(counter)
bin4 = minprop3/float(counter)
bin5 = minprop4/float(counter)  
print "Bin1 (0-0.1)=", bin1, "\t", "Bin2 (0.1-0.2)=", bin2, "\t", "Bin3 (0.2-0.3)=", bin3, "\t", "Bin4 (0.3-0.4)=", bin4, "\t", "Bin5 (0.4-0.5)=", bin5

So it turned out that the reason the loops weren't working is because python wasn't reading my values (which are all decimals) as decimals. So, I had to change it to float(minorallelefreq[x]), and it worked.

There are several possible bugs in this code

  1. int(0.1) => 0, so minprop will always be 0 unless there are negative values
  2. no indentation for minprop4, and it will never be set, because the value cannot simultaneously be > 0.4 and <= 0.4
  3. You assume 100 elements, and that they're all between 0 and 0.4

I would suggest that you try to automatically bucketize based on the actual values, rather than the anticipated values:

import collections
buckets = collections.Counter()

for value in minorallfreq:
    bucket = int(value * 10) / 10.0
    buckets[bucket] += 1
if minorallelefreq[x] <= int(0.1):
    minprop = minprop + 1 
if minorallelefreq[x] > 0.1 and minorallelefreq[x] <= 0.2: 
    minprop1 = minprop1 + 1 

means that there will be no action taken for values > 0 and <= 0.1. What happens if you remove the int() call?

Also, nothing will happen for values > 0.4:

if minorallelefreq[x] > 0.4 and minorallelefreq[x] <= 0.4:  # never True
    minprop4 = minprop4 + 1                                 # indentation??

Generally, you should look into chained comparisons:

if 0.1 < minorallelefreq[x] <= 0.2:   # much easier to read, and faster.
    # etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM