0

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

xnx
  • 24,509
  • 11
  • 70
  • 109
John Barton
  • 1,581
  • 4
  • 25
  • 51

2 Answers2

3

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.

nagylzs
  • 3,954
  • 6
  • 39
  • 70
  • As a side-note, you really don't need the first part of `0.1 <= row[num] < 0.2:` in any of the `elif` clauses. The nature of the `if-elif-else` construct ensures that the value is already greater or equal to 0.1 or the other values. This essentially ensures the first part to always be true. – Kendas Dec 08 '15 at 08:41
  • @Kendas : While correct, I find `0.3 <= num_as_float < 0.4` more elegant to read, and avoids error from moving blocks of code. – DainDwarf Dec 08 '15 at 08:42
1

The problem is that row[num] is a string, not a number. Put the following at the top of the loop:

val = float(row[num])

Then use val in place of row[num] for any numeric operations, including comparisons.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41