<jupyter_start><jupyter_text>Multinomail Naive Bayes: https://www.youtube.com/watch?v=km2LoOpdB3A  TODO: Check everything again<jupyter_code>import numpy as np
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>**Train set**    [Chinese, Chinese, Bejing, C]    [Chinese, Chinese, Shangai, C]    [Chinese, Macao, C]    [Tokyo, Japan, Chinese, J]**Test set**    [Chinese, Chinese, Chinese, Tokyo, Japan, ?]<jupyter_code>data = np.array([
    [2, 1, 0, 0, 0, 0, 0],
    [2, 0, 1, 0, 0, 0, 0],
    [1, 0, 0, 1, 0, 0, 0],
    [1, 0, 0, 0, 1, 1, 1]
], dtype=np.float32)

X = data[:, :6] + 1.0 # To remove 0, because 0 make problems
y = data[:, 6:] # Class

# Calculate priors
probs = np.array([
    np.sum(X[:3], axis=0) / np.sum(np.sum(X[:3], axis=0)),
    np.sum(X[3:], axis=0) / np.sum(np.sum(X[3:], axis=0))
])
predict = np.array([
    [[4.0, 1.0, 1.0, 1.0, 2.0, 2.0]]
], dtype=np.float32)

with tf.Session() as sess:
    
    dist = tf.distributions.Multinomial(probs=probs, total_count=[3.0, 1.0])
    
    nb_classes, nb_features = 2, 6
    
    bbbb = tf.reshape(dist.log_prob(predict), [-1, 1, 2])
    bbbb = tf.cast(bbbb, tf.float64)
    
    cond_probs = tf.reduce_sum(bbbb, axis=0)
    
    priors = tf.log(tf.constant([0.75, 0.25], dtype=tf.float64))
    
    joint_likelihood = tf.add(priors, cond_probs)
    
    norm_factor = tf.reduce_logsumexp(joint_likelihood, axis=1)
    
    log_prob = joint_likelihood - norm_factor
    
    Z = tf.exp(log_prob)
    
    print(sess.run(Z))
    print(sess.run(tf.argmax(Z, axis=1)))<jupyter_output>(1, 1, 2)
[[0.92735163 0.07264837]]
[0]
{'fraction_non_alphanumeric': 0.18590143480973176, 'fraction_numerical': 0.06487835308796007, 'mean_word_length': 3.3351351351351353, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '314046', 'license': 'MIT', 'n_tokens_mistral': 763, 'n_tokens_neox': 695, 'n_words': 150, 'path': 'source/07_Generative_Algorithms/Homework.ipynb', 'repo_name': 'Rand0mUsername/machine_learning', 'size': '1603'}