How to use GaussianNB (sklearn)



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding


Release date:2024/7/30         

In Japanese


■Description of the GaussianNB function

This is a naive Bayes classifier whose probability distribution follows a Gaussian distribution. Gaussian naive Bayes finds the Gaussian distribution of data belonging to the same label, and determines which distribution new data is closer to.



■A concrete example of the GaussianNB function

In the graph below, the label values ​​of the test data are classified from the feature data.



The program is as follows. To install various libraries such as scikit-learn, click here.

from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt

X = np.array([[5,10],[8,50],[10,20]])  # Feature data
Y = np.array([1, 2, 3])    # Feature data labels
t = np.array([[9,20]])    # test data

clf = GaussianNB()    # Definition of Gaussian Naive Bayes
clf.fit(X, Y)    # Fitting to feature data
print(clf.predict(t))    # Classify the test data

plt.plot([5,8,10],[10,50,20],'o')    # Plotting feature data
plt.plot([9],[20],'^')    # Plotting the test data
plt.grid(True)
plt.show()


The result should have returned a value of 3. This is certainly close to the value of 3 in the feature data, so the result seems reasonable.









List of related articles



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding