lolamontes69

Ch9 Ex1- Programming Collective Intelligence

Sep 14th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. """ Chapter 9 Exercise 1: Bayesian classifier.
  2.  
  3.    "Can you think of ways that the Bayesian classifier you built in Chapter 6 could be used on the matchmaker dataset?
  4.    What would be good examples of features?"
  5.  
  6.    I implemented this with features for smoking?, want-kids? and interest combinations.
  7. """
  8.  
  9. import docclass_matchmaker as docclass
  10. import advancedclassify1 as advancedclassify
  11. matchmaker=advancedclassify.loadmatch('matchmaker.csv')
  12. cl=docclass.naivebayes(docclass.get_features1)
  13. cl.setdb('matchmaker.db')
  14.  
  15. """
  16. # add train1() to the classifier class in docclass.py
  17.  
  18.    def train1(self,item,cat):
  19.        features=self.getfeatures(item)
  20.        # Increment the count for every feature with this category
  21.        for f in features:
  22.            self.incf(f,cat)
  23.        # Increment the count for this category
  24.        self.incc(cat)
  25.        self.con.commit()
  26.  
  27. # add get_features1() to the beginning of docclass.py
  28.  
  29. def get_features1(item):
  30.    interests1 = item[3].split(':')
  31.    interests2 = item[8].split(':')
  32.    list1=[]
  33.    for a in interests1:
  34.        for b in interests2:
  35.            feat = a+' '+b
  36.            list1.append(feat)
  37.    smokingornot = 'smoke '+item[1]+' '+item[6]
  38.    list1.append(smokingornot)
  39.    kidsornot = 'kids '+item[1]+' '+item[6]
  40.    list1.append(kidsornot)
  41.    return list1
  42. """
  43.  
  44. # Train the database
  45. for a in range(len(matchmaker)):
  46.     item = matchmaker[a].data
  47.     cat = str(matchmaker[a].match)
  48.     cl.train1(item,cat)
  49.  
  50. # And it classifies <<<hurrah>>>
  51.  
  52. >>> cl.classify(['39', 'yes', 'no', 'skiing:knitting:dancing', '220 W 42nd St New York NY', '43', 'no', 'yes', 'soccer:reading:scrabble', '824 3rd Ave New York NY'])
  53. u'0'
  54.  
  55. >>>cl.classify(['39', 'no', 'yes', 'running:fashion:art:travel', '556 7th Ave New York NY', '39', 'no', 'no', 'knitting:photography', '404 E 14th St New York NY'])
  56. u'1'
  57.  
  58. """
Advertisement
Add Comment
Please, Sign In to add comment