<jupyter_start><jupyter_text>Coding Exercise 0502  1. Calssification with Naive Bayes:<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as st
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1.1. Read in data and explore:<jupyter_code># Load data.
data = load_iris()
# Display the description.
print(data.DESCR)
# Explanatory variables.
X = data['data']
columns = list(data['feature_names'])
print(columns)
# Response variable.
Y = data['target']
labels = list(data['target_names'])
print(labels)
# Visualize the frequency table.
ser = pd.Series(Y)
table = ser.value_counts()
table = table.sort_index()                                         # Has to be sorted for correct labeling.
sns.barplot(labels,table.values)
plt.show()
# Array of Scatter plots.
X_df = pd.DataFrame(X,columns=['Sepal_L','Sepal_W','Petal_L','Petal_W'])
my_cols_dict = {0:'red', 1:'green', 2:'blue'}
my_cols = pd.Series(Y).apply(lambda x: my_cols_dict[x])
pd.plotting.scatter_matrix(X_df, c=my_cols, marker='o', alpha=0.5)
plt.show()<jupyter_output><empty_output><jupyter_text>1.2. Naive Bayes train and test:<jupyter_code># Split the dataset into training and testing.
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1234)
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
# Train and predict.
GNB = GaussianNB()
GNB.fit(X_train,Y_train)
Y_pred_test = GNB.predict(X_test)                 
# Confusion matrix.
conf_mat = metrics.confusion_matrix(Y_test, Y_pred_test)
print(conf_mat)
# Accuracy.
accuracy = metrics.accuracy_score(Y_test, Y_pred_test)
print('Accuracy    = ' + str(np.round(accuracy,3)))<jupyter_output><empty_output><jupyter_text>1.3. Visualize the posterior probabilities:<jupyter_code>centers = GNB.theta_
variances = GNB.sigma_
for i in range(4):
    x_min = X[:,i].min()
    x_max = X[:,i].max()
    x_range = x_max-x_min
    x_grid = np.linspace(x_min-x_range/3,x_max+x_range/3,300)
    fig=plt.figure(figsize=(4,2), dpi=100)      # Width, height and DPI setting.
    ax = fig.add_axes([0,0,1,1])                # Left, bottom, width, height.
    ax.set_xlabel(columns[i])
    ax.set_ylabel('Probability')
    for j in range(3):
        center = centers[j,i]
        sigma = np.sqrt(variances[j,i])
        ax.plot(x_grid, st.norm.pdf(x_grid,loc=center,scale=sigma),color=my_cols_dict[j],linestyle="--",label=labels[j])
    ax.legend(loc=0)
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1348148148148148, 'fraction_numerical': 0.014814814814814815, 'mean_word_length': 4.897379912663755, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '254111', 'license': 'MIT', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1014, 'n_words': 200, 'path': 'SIC_AI_Coding_Exercises/SIC_AI_Chapter_06_Coding_Exercises/ex_0502.ipynb', 'repo_name': 'BattlePlot/SIC-Artificial-Intelligence', 'size': '2700'}