lOMoARcPSD| 58778885
import numpy as np import
matplotlib.pyplot as plt import
Points as point
######################################################################## #####
# Generate Training Data
n_generated_points = 1000
n_classes = 2 dimension = 2
N_total = n_generated_points*n_classes
Generated_Data = point.Zone(n_generated_points,n_classes,dimension)
Data = np.zeros((N_total,2))
Label = np.zeros(N_total) for i
in range(N_total):
Data[i,0] = Generated_Data.P[i,0]
Data[i,1] = Generated_Data.P[i,1]
Label[i] = Generated_Data.L[i]
X = Data y
= Label
# Funcon to calculate Gaussian probability density funcon def
gaussian_pdf(x, mean, var):
return (1 / np.sqrt(2 * np.pi * var)) * np.exp(-(x - mean) ** 2 / (2 * var))
# Funcon to train Naive Bayes def
train_naive_bayes(X_train, y_train):
lOMoARcPSD| 58778885
classes = np.unique(y_train)
means = {} vars = {} priors
= {}
for c in classes:
X_c = X_train[y_train == c] means[c] =
np.mean(X_c, axis=0) vars[c] = np.var(X_c,
axis=0) priors[c] = X_c.shape[0] /
X_train.shape[0]
return means, vars, priors
# Step 4: Predict with Naive Bayes def
predict_naive_bayes(X, means, vars, priors):
probs = []
for c in means:
# Start with the prior probability
prob = np.log(priors[c])
# Add the log of the likelihoods for each feature
for i in range(X.shape[1]):
prob += np.log(gaussian_pdf(X[:, i], means[c][i], vars[c][i])) probs.append(prob)
lOMoARcPSD| 58778885
# Return the class with the highest probability
return np.argmax(probs, axis=0)
# Train the Naive Bayes classier means, vars,
priors = train_naive_bayes(X, y)
##############################################################
################### Generate Test Data #######################
##############################################################
N_total = n_generated_points*n_classes
Generated_Data1 = point.Zone(n_generated_points,n_classes,dimension)
Test_Data = np.zeros((N_total,2))
Test_Label = np.zeros(N_total) for i
in range(N_total):
Test_Data[i,0] = Generated_Data1.P[i,0]
Test_Data[i,1] = Generated_Data1.P[i,1]
Test_Label[i] = Generated_Data1.L[i]
X_test = Test_Data y_test
= Test_Label
#############################################################
# Predict on the test data
y_pred = predict_naive_bayes(X_test, means, vars, priors)
lOMoARcPSD| 58778885
print("y_pred:") print(y_pred)
print("y_test:") print(y_test)
# Calculate accuracy accuracy =
np.mean(y_pred == y_test)
print(f'Accuracy: {accuracy * 100:.2f}%')
# Visualize the decision boundary and classicaon #
Create a meshgrid for plong the decision boundary
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min,
y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy =
np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1))
# Predict the labels for all points in the meshgrid grid_points =
np.c_[xx.ravel(), yy.ravel()]
Z = predict_naive_bayes(grid_points, means, vars, priors)
Z = Z.reshape(xx.shape)
# Plot the decision boundary
#plt.contourf(xx, yy, Z, alpha=0.3)
plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='black') plt.scaer(X_test[:, 0], X_test[:, 1],
c=y_pred, cmap='coolwarm', edgecolors='k', marker='o', label='Data test points')
lOMoARcPSD| 58778885
# Add labels and tle plt.xlabel('Feature X_test[0]')
plt.ylabel('Feature X_test[1]') plt.tle('Naive Bayes
Classier Decision Boundary') plt.legend(loc='best')
plt.show()

Preview text:

lOMoAR cPSD| 58778885 import numpy as np import
matplotlib.pyplot as plt import Points as point
######################################################################## ##### # Generate Training Data n_generated_points = 1000 n_classes = 2 dimension = 2
N_total = n_generated_points*n_classes
Generated_Data = point.Zone(n_generated_points,n_classes,dimension) Data = np.zeros((N_total,2))
Label = np.zeros(N_total) for i in range(N_total):
Data[i,0] = Generated_Data.P[i,0]
Data[i,1] = Generated_Data.P[i,1]
Label[i] = Generated_Data.L[i] X = Data y = Label
# Function to calculate Gaussian probability density function def gaussian_pdf(x, mean, var):
return (1 / np.sqrt(2 * np.pi * var)) * np.exp(-(x - mean) ** 2 / (2 * var))
# Function to train Naive Bayes def
train_naive_bayes(X_train, y_train): lOMoAR cPSD| 58778885 classes = np.unique(y_train) means = {} vars = {} priors = {} for c in classes:
X_c = X_train[y_train == c] means[c] =
np.mean(X_c, axis=0) vars[c] = np.var(X_c,
axis=0) priors[c] = X_c.shape[0] / X_train.shape[0] return means, vars, priors
# Step 4: Predict with Naive Bayes def
predict_naive_bayes(X, means, vars, priors): probs = [] for c in means:
# Start with the prior probability prob = np.log(priors[c])
# Add the log of the likelihoods for each feature for i in range(X.shape[1]):
prob += np.log(gaussian_pdf(X[:, i], means[c][i], vars[c][i])) probs.append(prob) lOMoAR cPSD| 58778885
# Return the class with the highest probability
return np.argmax(probs, axis=0)
# Train the Naive Bayes classifier means, vars,
priors = train_naive_bayes(X, y)
##############################################################
################### Generate Test Data #######################
##############################################################
N_total = n_generated_points*n_classes
Generated_Data1 = point.Zone(n_generated_points,n_classes,dimension)
Test_Data = np.zeros((N_total,2))
Test_Label = np.zeros(N_total) for i in range(N_total):
Test_Data[i,0] = Generated_Data1.P[i,0]
Test_Data[i,1] = Generated_Data1.P[i,1]
Test_Label[i] = Generated_Data1.L[i] X_test = Test_Data y_test = Test_Label
############################################################# # Predict on the test data
y_pred = predict_naive_bayes(X_test, means, vars, priors) lOMoAR cPSD| 58778885
print("y_pred:") print(y_pred)
print("y_test:") print(y_test)
# Calculate accuracy accuracy = np.mean(y_pred == y_test)
print(f'Accuracy: {accuracy * 100:.2f}%')
# Visualize the decision boundary and classification #
Create a meshgrid for plotting the decision boundary
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min,
y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy =
np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1))
# Predict the labels for all points in the meshgrid grid_points = np.c_[xx.ravel(), yy.ravel()]
Z = predict_naive_bayes(grid_points, means, vars, priors) Z = Z.reshape(xx.shape) # Plot the decision boundary
#plt.contourf(xx, yy, Z, alpha=0.3)
plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='black') plt.scatter(X_test[:, 0], X_test[:, 1],
c=y_pred, cmap='coolwarm', edgecolors='k', marker='o', label='Data test points') lOMoAR cPSD| 58778885
# Add labels and title plt.xlabel('Feature X_test[0]')
plt.ylabel('Feature X_test[1]') plt.title('Naive Bayes
Classifier Decision Boundary') plt.legend(loc='best') plt.show()