|
|
|
@ -1,47 +0,0 @@
|
|
|
|
|
from sklearn.datasets import load_iris
|
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
|
from sklearn.naive_bayes import GaussianNB
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bayes_iris():
|
|
|
|
|
"""
|
|
|
|
|
使用朴素贝叶斯对鸢尾花种类进行预测
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
#1)获取数据集
|
|
|
|
|
iris = load_iris()
|
|
|
|
|
print("查看特征值的名字:\n", iris.feature_names)
|
|
|
|
|
print("查看特征值:\n", iris.data, iris.data.shape)
|
|
|
|
|
|
|
|
|
|
#2)划分数据集
|
|
|
|
|
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22,test_size=0.7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#3)建立模型
|
|
|
|
|
|
|
|
|
|
estimator= GaussianNB();
|
|
|
|
|
|
|
|
|
|
#4)模型训练
|
|
|
|
|
estimator.fit(x_train,y_train)
|
|
|
|
|
|
|
|
|
|
#5)模型评估
|
|
|
|
|
#对比真实值:
|
|
|
|
|
y_predict = estimator.predict(x_test)
|
|
|
|
|
print("y_predict:\n",y_predict)
|
|
|
|
|
print("直接对比真实值:\n",y_predict==y_test)
|
|
|
|
|
|
|
|
|
|
#计算准确率
|
|
|
|
|
score = estimator.score(x_test,y_test)
|
|
|
|
|
print("模型准确率为:\n",score)
|
|
|
|
|
|
|
|
|
|
#7)做出预测
|
|
|
|
|
#[0]表示setosa,[1]表示versicolor,[2]表示virginica
|
|
|
|
|
X_new = np.array([[1.1, 5.9, 1.4, 2.2]]) #sklearn输入的数据必须是二维数组
|
|
|
|
|
prediction = estimator.predict(X_new)
|
|
|
|
|
print("预测的目标类别是:{}".format(prediction))
|
|
|
|
|
print("预测的目标类别花名是:{}".format(iris['target_names'][prediction]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
bayes_iris()
|