Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs13-23sp
project02
Commits
0157cc10
Commit
0157cc10
authored
1 year ago
by
Yaksher
Browse files
Options
Download
Email Patches
Plain Diff
Comment functions better
parent
cb3610f7
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bayes.py
+13
-4
bayes.py
with
13 additions
and
4 deletions
+13
-4
bayes.py
View file @
0157cc10
...
...
@@ -35,21 +35,30 @@ After training (which is run before your code), the following 3 global variables
num_data_points_in_category[category] = Total number of documents in the category 'category'
"""
@
cache
def
pr_category
(
category
:
str
)
:
# Pr(category)
def
pr_category
(
category
:
str
):
"""
Computes Pr(category)
"""
return
0
@
cache
def
pr_word_given_category
(
word
:
str
,
category
:
str
,
num_words_in_document
:
int
):
# Pr(word | category)
def
pr_word_given_category
(
word
:
str
,
category
:
str
,
num_words_in_document
:
int
):
"""
Computes Pr(word | category)
"""
return
0
def
pr_category_given_words
(
words
:
List
[
str
],
category
:
str
):
# Pr(category | words)
def
log_pr_category_given_words
(
words
:
List
[
str
],
category
:
str
):
"""
Computes log(Pr(category | words))
"""
return
0
def
predict
(
categories
,
words
):
best
=
None
best_likelihood
=
-
inf
for
category
in
categories
:
pr
=
pr_category_given_words
(
words
,
category
)
pr
=
log_
pr_category_given_words
(
words
,
category
)
if
pr
>
best_likelihood
:
best
=
category
best_likelihood
=
pr
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help