2020-12-19

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

  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)

朴素贝叶斯网络的iris鸢尾花数据集分类应用到新闻分类场景

上一篇:虚拟机的使用


下一篇:机器学习29:Sklearn库常用分类器及效果比较