2012-08-01 22 views
0

當我運行下的整個代碼,這條線:如何解決這個pysqlite2 AttributeError:X實例沒有屬性'con'?

res=self.con.execute(

從這個函數(其中getfeatures返回dictionary):

def fcount(self,f,cat): 
    res=self.con.execute(
     'select count from fc where feature="%s" and category="%s"' 
     %(f,cat)).fetchone() 
    if res==None: return 0 
    else: return float(res[0]) 

可生產這樣的錯誤:

AttributeError: naivebayes instance has no attribute 'con'    

我已經安裝了pysqlite2,當我運行pysqlite2測試時,我確定。

我也嘗試過使用內置的sqlite3的,而不是pysqlite2(做一個import sqlite3聲明和self.con=sqlite3.connect(":memory:")更換self.con=sqlite.connect(dbfile),但它也不能工作。

如何解決這個問題?

感謝任何幫助

這裏整個代碼:

from pysqlite2 import dbapi2 as sqlite 

import re 
import math 

def getfeatures(doc): 
    splitter=re.compile('\\W*') 
    # Split the words by non-alpha characters 
    words=[s.lower() for s in splitter.split(doc) 
      if len(s)>2 and len(s)<20] 
    # Return the unique set of words only 
# return dict([(w,1) for w in words]).iteritems() 
    return dict([(w,1) for w in words]) 

class classifier: 
    def __init__(self,getfeatures,filename=None): 
    # Counts of feature/category combinations 
    self.fc={} 
    # Counts of documents in each category 
    self.cc={} 
    self.getfeatures=getfeatures 

    def setdb(self,dbfile): 
    self.con=sqlite.connect(dbfile) 
    self.con.execute('create table if not exists fc(feature,category,count)') 
    self.con.execute('create table if not exists cc(category,count)') 


    def incf(self,f,cat): 
    count=self.fcount(f,cat) 
    if count==0: 
     self.con.execute("insert into fc values ('%s','%s',1)" 
         % (f,cat)) 
    else: 
     self.con.execute(
     "update fc set count=%d where feature='%s' and category='%s'" 
     % (count+1,f,cat)) 

    def fcount(self,f,cat): 
    res=self.con.execute(
     'select count from fc where feature="%s" and category="%s"' 
     %(f,cat)).fetchone() 
    if res==None: return 0 
    else: return float(res[0]) 

    def incc(self,cat): 
    count=self.catcount(cat) 
    if count==0: 
     self.con.execute("insert into cc values ('%s',1)" % (cat)) 
    else: 
     self.con.execute("update cc set count=%d where category='%s'" 
         % (count+1,cat)) 

    def catcount(self,cat): 
    res=self.con.execute('select count from cc where category="%s"' 
         %(cat)).fetchone() 
    if res==None: return 0 
    else: return float(res[0]) 

    def categories(self): 
    cur=self.con.execute('select category from cc'); 
    return [d[0] for d in cur] 

    def totalcount(self): 
    res=self.con.execute('select sum(count) from cc').fetchone(); 
    if res==None: return 0 
    return res[0] 


    def train(self,item,cat): 
    features=self.getfeatures(item) 
    # Increment the count for every feature with this category 
    for f in features.keys(): 
## for f in features: 
     self.incf(f,cat) 
    # Increment the count for this category 
    self.incc(cat) 
    self.con.commit() 

    def fprob(self,f,cat): 
    if self.catcount(cat)==0: return 0 

    # The total number of times this feature appeared in this 
    # category divided by the total number of items in this category 
    return self.fcount(f,cat)/self.catcount(cat) 

    def weightedprob(self,f,cat,prf,weight=1.0,ap=0.5): 
    # Calculate current probability 
    basicprob=prf(f,cat) 

    # Count the number of times this feature has appeared in 
    # all categories 
    totals=sum([self.fcount(f,c) for c in self.categories()]) 

    # Calculate the weighted average 
    bp=((weight*ap)+(totals*basicprob))/(weight+totals) 
    return bp 




class naivebayes(classifier): 

    def __init__(self,getfeatures): 
    classifier.__init__(self,getfeatures) 
    self.thresholds={} 

    def docprob(self,item,cat): 
    features=self.getfeatures(item) 

    # Multiply the probabilities of all the features together 
    p=1 
    for f in features: p*=self.weightedprob(f,cat,self.fprob) 
    return p 

    def prob(self,item,cat): 
    catprob=self.catcount(cat)/self.totalcount() 
    docprob=self.docprob(item,cat) 
    return docprob*catprob 

    def setthreshold(self,cat,t): 
    self.thresholds[cat]=t 

    def getthreshold(self,cat): 
    if cat not in self.thresholds: return 1.0 
    return self.thresholds[cat] 

    def classify(self,item,default=None): 
    probs={} 
    # Find the category with the highest probability 
    max=0.0 
    for cat in self.categories(): 
     probs[cat]=self.prob(item,cat) 
     if probs[cat]>max: 
     max=probs[cat] 
     best=cat 

    # Make sure the probability exceeds threshold*next best 
    for cat in probs: 
     if cat==best: continue 
     if probs[cat]*self.getthreshold(best)>probs[best]: return default 
    return best 


def sampletrain(cl): 
    cl.train('Nobody owns the water.','good') 
    cl.train('the quick rabbit jumps fences','good') 
    cl.train('buy pharmaceuticals now','bad') 
    cl.train('make quick money at the online casino','bad') 
    cl.train('the quick brown fox jumps','good') 


nb = naivebayes(getfeatures) 

sampletrain(nb) 

#print ('\nbuy is classified as %s'%nb.classify('buy')) 
#print ('\nquick is classified as %s'%nb.classify('quick')) 

##print getfeatures('Nobody owns the water.') 

回答

0

你的代碼是一團糟。首先閱讀http://www.python.org/dev/peps/pep-0008/。從對象繼承新類,調用父方法使用super()函數而不是直接調用,並且是 - 在使用con屬性之前將self.set_db()調用__init__方法。 AttributeError: naivebayes instance has no attribute 'con'當沒有這樣的屬性時引發,它根本不涉及數據庫。

+0

謝謝,維克多,但是這個代碼是從(優秀)的書「編程集體情報」。我只是從https://raw.github.com/cataska/programming-collective-intelligence-code/master/chapter6/docclass.py複製它,並將代碼的一部分(fisherclassifier,因爲我只使用naivebayes分類)。不管怎樣,謝謝! – craftApprentice 2012-08-01 22:25:35

+0

由於naivebayes __init __()被重新顯式調用超類(分類),以延長它的行爲,這種方式: 類naivebayes(分類): 高清__init __(自我,getfeatures): 分類.__的init __(自我,getfeatures) 我無法理解繼承的問題。如何修復它? – craftApprentice 2012-08-01 23:44:34

0

你的代碼集在setdb中的連接,但從未調用該方法。也許你可以從__init__的方法來調用它。

0

在第133頁的「持久訓練的分類器」一節中,它說:「初始化分類器後,需要使用數據庫文件的名稱調用setdb方法。」這應該照顧你的問題。

實施例:
CL = docclass.fisherclassifier(docclass.getwords)
cl.setdb( 'test1.db')

相關問題