Une seule caractériqtique (feature) :¶
In [9]:
import numpy as np
def inferer_1(X, y):
X0 = X[y==0]
X1 = X[y==1]
mu_0, sigma_0 = np.mean(X0), np.std(X0)
mu_1, sigma_1 = np.mean(X1), np.std(X1)
return (mu_0, sigma_0), (mu_1, sigma_1)
X = np.array([[1], [2], [3], [4]])
y = np.array([0, 1, 0, 1])
inferer_1(X, y)
Out[9]:
((2.0, 1.0), (3.0, 1.0))
In [45]:
from scipy import stats
def classify_1(u, X, y):
p_0, p_1 = len(X[y==0])/len(X), len(X[y==1])/len(y)
#print('p_0: ', p_0)
#print('p_1: ', p_1)
(mu_0, sigma_0), (mu_1, sigma_1) = inferer_1(X, y)
f_0 = stats.norm.pdf(u, mu_0, sigma_0)
f_1 = stats.norm.pdf(u, mu_1, sigma_1)
#print('f_0: ', f_0)
#print('f_1: ', f_1)
return 0 if p_0 * f_0 >= p_1 * f_1 else 1
X = np.array([[1], [2], [3], [4]])
y = np.array([0, 1, 0, 1])
u = 4
classify_1(u, X, y)
Out[45]:
1
Plusieurs features :¶
In [46]:
def inferer(X, y):
X0 = X[y==0]
X1 = X[y==1]
l = X.shape[1]
means = []
means.append([np.mean(X0[:,d]) for d in range(l)])
means.append([np.mean(X1[:,d]) for d in range(l)])
#print('means[0]: ', means[0])
#print('means[1]: ', means[1])
sigmas = []
sigmas.append([np.std(X0[:,d]) for d in range(l)])
sigmas.append([np.std(X1[:,d]) for d in range(l)])
#print('sigmas[0]: ', means[0])
#print('sigmas[1]: ', means[1])
return means, sigmas
X = np.array([[1, 1], [2, 2], [3, 3], [4, 4]])
y = np.array([0, 1, 0, 1])
inferer(X, y)
Out[46]:
([[2.0, 2.0], [3.0, 3.0]], [[1.0, 1.0], [1.0, 1.0]])
In [47]:
def classify(u, X, y):
p_0, p_1 = len(X[y==0])/len(X), len(X[y==1])/len(y)
#print('p_0: ', p_0)
#print('p_1: ', p_1)
means, sigmas = inferer(X, y)
for d in range(len(X[0,:])):
p_0 *= stats.norm.pdf(u[d], means[0][d], sigmas[0][d])
p_1 *= stats.norm.pdf(u[d], means[1][d], sigmas[1][d])
#print('pr_0: ', p_0)
#print('pr_1: ', p_0)
return 0 if p_0 >= p_1 else 1
X = np.array([[1, 1], [2, 2], [3, 3], [4, 4]])
y = np.array([0, 1, 0, 1])
u = [4, 4]
classify(u, X, y)
Out[47]:
1
On teste sur un exemple un peu plus conséquent :¶
In [48]:
import pandas as pa
dic = {'S': [0, 0, 0, 0, 1, 1, 1, 1],
# 'S': ['M', 'M', 'M', 'M', 'F', 'F', 'F', 'F'],
'H': [1.82, 1.80, 1.70, 1.80, 1.52, 1.65, 1.68, 1.75],
'W': [82, 86, 77, 75, 45, 68, 59, 68],
'F': [30, 28, 30, 25, 15, 20, 18, 23]}
data = pa.DataFrame(data=dic)
In [53]:
X = np.array(data[['H', 'W', 'F']])
y = data['S']
u = [1.81, 59, 21]
'F' if classify(u, X, y) == 1 else 'M'
Out[53]:
'F'
En utilisant sklearn
¶
In [56]:
dic = {'S': [0, 0, 0, 0, 1, 1, 1, 1],
# 'S': ['M', 'M', 'M', 'M', 'F', 'F', 'F', 'F'],
'H': [1.82, 1.80, 1.70, 1.80, 1.52, 1.65, 1.68, 1.75],
'W': [82, 86, 77, 75, 45, 68, 59, 68],
'F': [30, 28, 30, 25, 15, 20, 18, 23]}
data = pa.DataFrame(data=dic)
X = np.array(data[['H', 'W', 'F']])
y = data['S']
u = [1.81, 59, 21]
In [57]:
from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X, y)
Out[57]:
GaussianNB()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()
In [58]:
'F' if nb.predict([u]) == 1 else 'M'
Out[58]:
'F'
In [ ]: