Naive Bayes Gaussian Classification
| !uv pip install -q \
numpy==2.3.2 \
matplotlib==3.10.6 \
seaborn==0.13.2 \
scikit-learn==1.7.1
|
| import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_blobs
from sklearn.naive_bayes import GaussianNB
%matplotlib inline
sns.set_style("darkgrid")
sns.set_theme(style="darkgrid")
|
| X, y = make_blobs(100, 2, centers=2, random_state=2, cluster_std=1.5)
x_axis = X[:, 0] # take all rows, first column
y_axis = X[:, 1] # take all rows, second column
plt.scatter(
x_axis,
y_axis,
c=y, # colors according to labels
s=50, # marker size
cmap="RdBu", # color map, Red & Blue
)
plt.show()
|
| model = GaussianNB()
model.fit(X, y)
|
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GaussianNB
?Documentation for GaussianNBiFitted
Parameters
|
|
|
|
priors |
None |
|
var_smoothing |
1e-09 |
| rng = np.random.RandomState(0)
Xnew = [-6, -14] + [14, 18] * rng.rand(2000, 2)
Xnew[:2, :]
|
array([[ 1.68338905, -1.12659141],
[ 2.43868727, -4.19210271]])
| ynew = model.predict(Xnew)
|
| plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap="RdBu")
lim = plt.axis()
plt.scatter(Xnew[:, 0], Xnew[:, 1], c=ynew, s=20, cmap="RdBu", alpha=0.1)
plt.show()
|
| yprob = model.predict_proba(Xnew)
yprob[-8:].round(2)
|
array([[0.89, 0.11],
[1. , 0. ],
[1. , 0. ],
[1. , 0. ],
[1. , 0. ],
[1. , 0. ],
[0. , 1. ],
[0.15, 0.85]])