-
Notifications
You must be signed in to change notification settings - Fork 0
/
clase_06_12_NaiveBayes_KFold.py
58 lines (44 loc) · 1.5 KB
/
clase_06_12_NaiveBayes_KFold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Universidad Adolfo Ibañez
Facultad de Ingeniería y Ciencias
TICS 585 - Reconocimiento de Patrones en imágenes
Ejemplo de clasificacion con NaiveBayes
Características:
+ Dataset: Iris
+ Validación Cruzada
Autor:. Miguel Carrasco (09-11-2021)
rev.1.0
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from seaborn import pairplot
from sklearn.model_selection import KFold, cross_val_score, cross_val_predict
colormap = np.array(['r', 'g', 'b'])
# Empleamos un dataset con clase
X, y = load_iris(return_X_y=True)
df = pd.DataFrame(np.hstack((X,y.reshape(-1,1))), columns=['LSepal', 'WSepal', 'LPetal', 'WPetal', 'target'])
# Graficamos todas las combinaciones.
# En la columna target se encuentra la clase
pairplot(data = df, hue= 'target', kind='scatter',palette='tab10')
plt.show()
# modelo de clasificación
model = GaussianNB()
# Validación cruzada
k_fold = KFold(n_splits=5 , shuffle=True, random_state=None)
#no necesario..obtener indices
indices = k_fold.split(X)
# evaluación de validación cruzada
scores = cross_val_score(model, X, y, cv=k_fold, n_jobs=1)
print(scores)
# predicción de validación cruzada
y_pred = cross_val_predict(model, X, y, cv=k_fold, n_jobs=1)
accuracy = metrics.accuracy_score(y, y_pred)
print(f'Accuracy promedio {accuracy}')
plt.figure()
plt.bar(x=np.arange(len(scores)), height=scores)
plt.title('Accuracy')
plt.show()