<jupyter_start><jupyter_text>Goals:1. Review Naive Bayes2. Discuss what the different Naive Bayes classifiers are3. Go through an example: this is taken from sklearn's website:    https://scikit-learn.org/stable/modules/naive_bayes.htmlgaussian-naive-bayes4. Python - Jupyter notebooks, what to turn in, how to view the dataThen:Give a preview of what we'll talk about next time (evaluation) Useful packages* Plotting: matplotlib    other plotting tools: (seaborn, pandas, ...)* pandas: good for data manipulation (dataframes, reading CSV's) * numpy: math in python, numpy arrays are especially usefulTo save your script (to turn in), go to File, Download As, and select .py.Note: To add a python hook to do so a, edit the jupyter config file (e.g. vim ~/.jupyter/jupyter_notebook_config.py) and add a post-save hook code block from the example: https://jupyter-notebook.readthedocs.io/en/stable/extending/savehooks.html<jupyter_code>from sklearn import datasets
import pylab<jupyter_output><empty_output><jupyter_text>Data manipulation and exploratory data analysis<jupyter_code>iris = datasets.load_iris()
iris
# 4 dimensional matrix X
X_train = iris.data
X_train
y_train = iris.target
y_train
# extract a row
iris.data[1]
# extract a column
iris.data[:,1]
# plot the dimensions 1, 2 (note: the first dimension is 0)
pylab.scatter(iris.data[:,1],iris.data[:,2], c=iris.target)

# always label both axes for any plot so the reader can interpret it
pylab.xlabel("dimension 1")
pylab.ylabel("dimension 2")

# sometimes titles are helpful
pylab.title("2D plot of Iris data")

# TODO:
# add a legend explaining what the colors are
# plot dimensions 2 and 3
pylab.scatter(iris.data[:,2],iris.data[:,3], c=iris.target)

# always label the dimensions
pylab.xlabel("dimension 2")
pylab.ylabel("dimension 3")

# sometimes titles are helpful
pylab.title("2D plot of Iris data")

# TODO:
# add a legend explaining what the colors are<jupyter_output><empty_output><jupyter_text>Recap: making plots is important for exploratory data analysis.Good thing to include a careful exploratory data analysis in your report to help you motivate why you chose a certain model or class of models.  ClassificationNow we step through the classification part from the sklearn example.<jupyter_code># Question: why is the Gaussian one the right one to use here?
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb
# fit the training data w/ the Gaussian NB classifier
trained_classifier = gnb.fit(iris.data, iris.target)
# pressing tab at the period lets you see different functions/variables
trained_classifier.
# making predictions on data set - here we just use the original training set but normally want e.g. a test set
predictions = trained_classifier.predict(iris.data)
# predicted labels
predictions
# true labels
y_train<jupyter_output><empty_output><jupyter_text>Generative models introData distribution: think carefully about how the data are generated.The above data were real-valued, so we chose to think about Gaussian-distributed data.Some examples below about how to generate simulated data from these distributions.<jupyter_code># generate 500 Normal(0,1) samples
data = np.random.normal(0,1,500)

pylab.hist(data, bins=20)
pylab.ylabel("frequency");
# generate 10 Multinomial(5, p=(0.2,0.3,0.5)) samples
data = np.random.multinomial(5, [0.2,0.3,0.5],10)
# each row is a sample - the counts sum to 5
data
# sum up rows
rowsum = data.sum(0)
# compute the proportions
rowsum / data.sum()
# Now take a larger sample
data = np.random.multinomial(5, [0.2,0.3,0.5],100)
# sum up rows
rowsum = data.sum(0)
# compute the proportions - with a larger sample we can see it approaches the probabilities used to generate them
rowsum / data.sum()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08921933085501858, 'fraction_numerical': 0.01725969198088157, 'mean_word_length': 5.2574750830564785, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '21190', 'license': 'Apache-2.0', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1169, 'n_words': 443, 'path': 'week2/test_example_NB.ipynb', 'repo_name': 'dicai/cos424', 'size': '3766'}