机器学习算法(五) 贝叶斯分类-鸢尾花数据集

基于贝叶斯对鸢尾花数据进行分类

1. python3.7
2. numpy >= '1.16.4'
3. sklearn >= '0.23.1'

 

# import base package

import warnings 
warnings.filterwarnings('ignore')
import numpy as np 
from sklearn import datasets

from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split

# import data
X, y = datasets.load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# train
clf = GaussianNB(var_smoothing = 1e-8)
clf.fit(X_train, y_train)
print(clf)

# evaluate y_test == y_pred rate
y_pred = clf.predict(X_test)
acc = np.sum(y_test == y_pred) / X_test.shape[0]
print('y_test:', y_test)
print('y_pred:', y_pred)
print('X_test:', X_test.shape[0])
print('test acc: %.3f' % acc)

# predict
y_proba = clf.predict_proba(X_test[:1])
print('pre:', clf.predict(X_test[:1])) # three class proba
print('probability value:', y_proba)   # choose max from the three

print('X_test: \n', X_test[:10])
y_proba = clf.predict_proba(X_test[:5])
print('pre:', clf.predict(X_test[:5]))
print('probability value:', y_proba)

 

你可能感兴趣的:(机器学习,朴素贝叶斯算法)