朴素贝叶斯

案例代码如下:

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
import numpy as np
np.random.seed(0)
iris=datasets.load_iris()
iris_x=iris.data
iris_y=iris.target
indices=np.random.permutation(len(iris_x))
iris_x_train=iris_x[indices[:-10]]
iris_y_train=iris_y[indices[:-10]]
iris_x_test=iris_x[indices[-10:]]
iris_y_test=iris_y[indices[-10:]]
clf=GaussianNB()
clf.fit(iris_x_train,iris_y_train)
iris_y_predict=clf.predict(iris_x_test)
score=clf.score(iris_x_test,iris_y_test,sample_weight=None)
print('iris_y_predict=')
print(iris_y_predict)

print("iris_y_test=")
print(iris_y_test)
print('Accuracy:',score)

运行结果:

iris_y_predict=
[1 2 1 0 0 0 2 1 2 0]
iris_y_test=
[1 1 1 0 0 0 2 1 2 0]
Accuracy: 0.9

Process finished with exit code 0

你可能感兴趣的:(闲谈,机器学习,人工智能)