Python错误 - 贝叶斯分类器

时间:2013-04-26 10:05:23

标签: python classification

尝试制作一个天真的贝叶斯分类器但是我不断得到“返回功能不正确”或“缩进不匹配和水平”的错误。非常感谢任何帮助,

这是我的代码片段

if row[2] == 'C1':
   self.c1_data.append(row)
else:
    self.c2_data.append(row)

#Get relative frequencies from variables
#using the values in A ,B and C to
#return a dictionary
def getCatProbs(self, data):
  a_count = 0
  b_count = 0
  c_count = 0
  probs = {}
#For every row in the datafile
for row in data:
  #Based on figure increase counts
  if row[1] == ">50K":
     a_count = a_count + 1
  if row[1] == "<=50K":
     b_count = b_count + 1
  else:
     c_count = c_count + 1

probs[">50K"] = float(a_count)/len(data)
probs["<=50K"] = float(b_count)/len(data)
probs['C'] = float(c_count)/len(data)

返回probs

1 个答案:

答案 0 :(得分:1)

之后的所有行都缺少空格

试试这个缩进:

#Get relative frequencies from variables
#using the values in A ,B and C to
#return a dictionary
def getCatProbs(self, data):
    a_count = 0
    b_count = 0
    c_count = 0
    probs = {}

    #For every row in the datafile
    for row in data:
        #Based on figure increase counts
        if row[1] == ">50K":
            a_count = a_count + 1
        if row[1] == "<=50K":
            b_count = b_count + 1
        else:
            c_count = c_count + 1

    probs[">50K"] = float(a_count)/len(data)
    probs["<=50K"] = float(b_count)/len(data)
    probs['C'] = float(c_count)/len(data)
    return probs