Classifying numbers in python
Question: I have tried to classify numbers in python from 0.0 to 1.0. However, all
numbers are getting the else condition. I need each number to get the
appropriate condition, but even when the number is 0.3 or 0.2 is getting the
last bin. What I am doing wrong?

    
    
    #!/usr/bin/python
    
    import csv
    import sys
    import re
    
    bin1=0;
    bin2=0;
    bin3=0;
    bin4=0;
    bin5=0;
    bin6=0;
    bin7=0;
    bin8=0;
    bin9=0;
    bin10=0;
    
    
    def Read():
         with open (file_name) as csvfile:
            readCSV = csv.reader (csvfile, delimiter=',')
            for row in readCSV:
                for num in range(1,2):
                    print row[num]                
                    #Include in 10 bins
                    if 0.0 <= row[num] < 0.1:
                       bin1+=1;
                       print "bin1";
                    elif 0.1 <= row[num] < 0.2:
                       bin2+=1;
                       print "bin2"
                    elif 0.2 <= row[num] < 0.3:
                       bin3+=1;
                       print "bin3"
                    elif 0.3 <= row[num] < 0.4:
                       bin4+=1;
                       print "bin4"
                    elif 0.4 <= row[num] < 0.5:
                       bin5+=1;
                       print "bin5"
                    elif 0.5 <= row[num] < 0.6:
                       bin6+=1;
                       print bin6;
                       print "bin6"
                    elif 0.6 <= row[num] < 0.7:
                       bin7+=1;
                       print bin7;
                       print "bin7"
                    elif 0.7 <= row[num] < 0.8:
                       bin8+=1;
                       print "bin8"
                    elif 0.8 <= row[num] < 0.9:
                       bin9+=1;
                       print "bin9"
                    else:
                       print "bin10"
    
    
    file_name = str(sys.argv[1])
    print file_name
    Read()
    

What is wrong with this simple classifier?

Thank you

Cheers


Answer: It is wrong because you are reading strings from the csv file. when you
compare a string to a number, then the names of their types (!!!) are
compared.

Solution: convert `row[num]` into a number before comparing it to floating
point values.

