<jupyter_start><jupyter_text>1. Load DatasetLoad numerical dataset<jupyter_code>iris = datasets.load_iris()
#dataset explanation
print(iris.DESCR)
print(len(iris['data']))
print(len(iris['target']))
iris.data[:10]
iris.target[:10]<jupyter_output><empty_output><jupyter_text>2. Exploratory Data AnalysisGaussian Naive Bayes expresses likelihood using Gaussian:$$\large p(x_{i}|y) = \large\frac{1}{\sqrt{2\pi\sigma_{y}^{2}}} exp(-\frac{(x_{i} - \mu_{y}^{2})}{2\sigma_{y}^{2}})$$<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (16, 5)
iris.target
from collections import Counter
# count each group, there are three groups : 0, 1, 2
Counter(iris.target)<jupyter_output><empty_output><jupyter_text>2.1 Group 0<jupyter_code>group_0 = iris.data[:50]
# first feature
plt.subplot(1,4,1)
sns.distplot(group_0[:, 0])

# second feature
plt.subplot(1,4,2)
sns.distplot(group_0[:, 1])

# third feature
plt.subplot(1,4,3)
sns.distplot(group_0[:, 2])

# fourth feature
plt.subplot(1,4,4)
sns.distplot(group_0[:, 3])<jupyter_output><empty_output><jupyter_text>2.1 Group 1<jupyter_code>group_1 = iris.data[50:100]
# first feature
plt.subplot(1,4,1)
sns.distplot(group_1[:, 0])

# second feature
plt.subplot(1,4,2)
sns.distplot(group_1[:, 1])

# third feature
plt.subplot(1,4,3)
sns.distplot(group_1[:, 2])

# fourth feature
plt.subplot(1,4,4)
sns.distplot(group_1[:, 3])<jupyter_output><empty_output><jupyter_text>2.1 Group 2<jupyter_code>group_2 = iris.data[100:]
# first feature
plt.subplot(1,4,1)
sns.distplot(group_2[:, 0])

# second feature
plt.subplot(1,4,2)
sns.distplot(group_2[:, 1])

# third feature
plt.subplot(1,4,3)
sns.distplot(group_2[:, 2])

# fourth feature
plt.subplot(1,4,4)
sns.distplot(group_2[:, 3])<jupyter_output><empty_output><jupyter_text>3. Split Train/Test Data<jupyter_code>from sklearn.model_selection import train_test_split

train_x, test_x, train_y, test_y = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
print('Train x', train_x[:1])
print('Train y', train_y[:1])
from collections import Counter
print('original data :', Counter(iris.target))
print('training data :', Counter(train_y))
print('test data :', Counter(test_y))<jupyter_output>original data : Counter({0: 50, 1: 50, 2: 50})
training data : Counter({1: 37, 2: 37, 0: 31})
test data : Counter({0: 19, 1: 13, 2: 13})<jupyter_text>4. Train Data<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

# scale training data
scaler = scaler.fit(train_x)
scaled_train_x = scaler.transform(train_x)

#transform test dataset
scaled_test_x = scaler.transform(test_x)
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb_model = gnb.fit(scaled_train_x, train_y)<jupyter_output><empty_output><jupyter_text>5. Test Model<jupyter_code>pred = gnb_model.predict(scaled_test_x)
print("Accuracy :", (pred == test_y).sum() / len(test_x))<jupyter_output>Accuracy : 0.9777777777777777
{'fraction_non_alphanumeric': 0.1623989218328841, 'fraction_numerical': 0.051549865229110516, 'mean_word_length': 7.810089020771513, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '320123', 'license': 'MIT', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1210, 'n_words': 213, 'path': 'bayesian_decision_tree/Gaussian_Naive_Bayes_sklearn.ipynb', 'repo_name': 'jbaeckn/machine-learning-notes', 'size': '2968'}