Advertisement
gandalfbialy

Untitled

May 17th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. def log_prob(model, rec, class_name):
  2. logp = math.log(model["class_counts"][class_name] / model["total_docs"])
  3. V = len(model["vocab"])
  4. a = model["alpha"]
  5.  
  6.  
  7. for tag in rec["tags"]:
  8. wc = model["word_counts"][class_name].get(tag, 0)
  9. logp += math.log((wc + a) / (model["total_words"][class_name] + a * V))
  10. return logp
  11.  
  12. def predict(model, rec):
  13. best_class, best_log = None, -1e99
  14. for c in model["class_counts"]:
  15. lp = log_prob(model, rec, c)
  16. if lp > best_log:
  17. best_class, best_log = c, lp
  18. return best_class
  19.  
  20. def evaluate(model, test):
  21. correct = 0
  22. for rec in test:
  23. if predict(model, rec) == rec["label"]:
  24. correct += 1
  25. accuracy = correct / len(test)
  26. print(f"Dokładność (accuracy) = {accuracy:.2%}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement