使用OpenCV 与sklearn 构建朴素贝叶斯分类器

1.使用OpenCv  构建正太贝叶斯分类器

from sklearn import datasets
X, y = datasets.make_blobs(100, 2, centers=2, random_state=1701, cluster_std=2)

import matplotlib.pyplot as plt
plt.style.use('ggplot')

plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=y, s=50)

import numpy as np
from sklearn import model_selection as ms
X_train, X_test, y_train, y_test = ms.train_test_split(
    X.astype(np.float32), y, test_size=0.1
)

import cv2
#构造贝叶斯分类器
model_norm = cv2.ml.NormalBayesClassifier_create()

model_norm.train(X_train, cv2.ml.ROW_SAMPLE, y_train)

ret,y_predict = model_norm.predict(X_test)
#评估
from sklearn import  metrics
score = metrics.accuracy_score(y_predict,y_test)
print(score)

OUTPUT:

1.0

#除此之外,贝叶斯分类器还可以返回每个数据分分类的概率

ret,y_predict2,y_proba=model_norm.predictProb(X_test)

print(y_proba.round(2))

OUTPUT:

[[0.   0.32]
 [0.25 0.  ]
 [0.   0.29]
 [0.   0.21]
 [0.   0.16]
 [0.   0.13]
 [0.17 0.  ]
 [0.25 0.  ]
 [0.   0.25]
 [0.   0.08]]

(2)利用sklearn 构建真正朴素贝叶斯分类器

from sklearn import  naive_bayes

model_native  = naive_bayes.GaussianNB()

model_native.fit(X_train,y_train)
s = model_native.score(X_test,y_test)
print(s)
y_proba2 =model_native.predict_proba(X_test)
print(y_proba2)

OUT

1.0
[[4.69372777e-10 1.00000000e+00]
 [4.10741550e-11 1.00000000e+00]
 [2.50299468e-10 1.00000000e+00]
 [9.99998551e-01 1.44937231e-06]
 [9.99998950e-01 1.04978139e-06]
 [3.24916861e-07 9.99999675e-01]
 [2.19740750e-08 9.99999978e-01]
 [1.63405721e-13 1.00000000e+00]
 [3.58789410e-08 9.99999964e-01]
 [2.48783413e-07 9.99999751e-01]]

Process finished with exit code 0

 

PUT:

 

你可能感兴趣的:(使用OpenCV 与sklearn 构建朴素贝叶斯分类器)