A naive Bayes classifier over categorical features, each taking values , has the same functional form as multinomial logistic regression on one-hot features. Fit one by counting and then read off its softmax weights.
Implement:
nb_fit(X, y, K, C, alpha=1.0, beta=1.0): X is an (N, D) integer array, y holds labels in . Return (pi, theta) using Dirichlet posterior means:
(C,)theta[c, d, k], shape (C, D, K), where counts training rows of class whose feature equals .nb_predict_proba(X, pi, theta): the (N, C) posterior , computed in log space.one_hot(X, K): the (N, D*K) matrix whose column is .nb_to_softmax(pi, theta): return (B, gamma) with B of shape (C, D*K) and gamma of shape (C,) such that equals the naive Bayes posterior for every (column order as in one_hot).With alpha = beta = 0 the estimates are the maximum-likelihood counts.