2013-04-26 114 views
-4

試圖做一個樸素的貝葉斯分類器但是我不斷收到錯誤,「返回超出函數」或「縮進不匹配和級別」。任何幫助是極大的讚賞,Python錯誤 - 貝葉斯分類器

繼承人我的代碼片段

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

+2

我們不在這裏做*緊急*。從你的問題中刪除。 – jamylak 2013-04-26 10:06:15

+1

請提供返回probs縮進的正確方法。 – 2013-04-26 10:08:05

+0

:/是真的有必要嗎?只是尋找問題的幫助,我不能通過研究找出 – user2311700 2013-04-26 10:08:33

回答

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 
+0

我已經通過包含空間編輯了上面的代碼,但它仍然拋出相同的錯誤 – user2311700 2013-04-26 10:13:47

+0

for循環缺少空格,並且return語句縮進錯誤。 – 2013-04-26 11:40:49

+0

我改變了上面代碼中的標識。 – 2013-04-26 13:15:34