Advertisement
gandalfbialy

Untitled

May 24th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. def train_nb(train, vocabulary):
  2. class_counts = {}
  3. word_counts = {}
  4. total_words = {}
  5.  
  6. for record in train:
  7. c = record["label"]
  8. class_counts[c] = class_counts.get(c, 0) + 1
  9. word_counts.setdefault(c, {})
  10. total_words.setdefault(c, 0)
  11.  
  12. for tag in record["tags"]:
  13. word_counts[c][tag] = word_counts[c].get(tag, 0) + 1
  14. total_words[c] += 1
  15.  
  16. model = {
  17. "class_counts": class_counts,
  18. "word_counts": word_counts,
  19. "total_words": total_words,
  20. "vocab": vocabulary,
  21. "alpha": 1.0,
  22. "total_docs": len(train)
  23. }
  24.  
  25. print(model)
  26. return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement