Bernoulli Naive Bayes Classifier
Problem statement
For this exercise, implement a Bernoulli naive Bayes classifier directly, without a machine-learning library. The training matrix train has binary feature values and one integer class label in labels per row. All classes 0..numClasses-1 occur in training.
Fit the following fixed model. Let count[c] be the number of training rows labeled c, and ones[c][j] the number of those rows whose feature j is 1.
- Class prior:
prior[c] = count[c] / n. - Add-one conditional smoothing:
p[c][j] = (ones[c][j] + 1) / (count[c] + 2). - For a query
x, its natural-log score for classcisln(prior[c]) + sum_j(x[j]*ln(p[c][j]) + (1-x[j])*ln(1-p[c][j])).
For each row of queries, find the maximum class score and return the smallest class label whose score differs from that maximum by at most 10^-10. This numerical tie rule is part of the exercise contract. Return labels in query order; an empty query matrix returns an empty array. Do not smooth the class prior or transform the already-binary features.
Function
bernoulliClassify(train: int[][], labels: int[], numClasses: int, queries: int[][]) → int[]Examples
Example 1
train = [[0],[1]]labels = [0,1]numClasses = 2queries = [[0],[1]]return = [0,1]Each class has one example; smoothed likelihood favors the class matching the query.
Example 2
train = [[0,1],[1,0],[0,1],[1,0]]labels = [0,1,0,1]numClasses = 2queries = [[0,1],[1,0],[1,1],[0,0]]return = [0,1,0,0]The first two queries favor their matching class. The last two have equal scores and choose label0.
Example 3
train = [[1],[1],[1],[0]]labels = [0,0,0,1]numClasses = 2queries = [[0],[1]]return = [1,0]The empirical prior favors class0, but a zero query still favors class1: 1/6 is larger than 3/20.
Constraints
2 <= numClasses <= 10andnumClasses <= train.length <= 500.labels.length = train.length; every class is represented.- Every training row and query row has the same feature count
d, where1 <= d <= 50. - Every matrix entry is
0or1, and0 <= labels[i] < numClasses. 0 <= queries.length <= 200.- For every query, each class has exactly the maximum mathematical log score or trails that maximum by at least
10^-8.