当前位置:   article > 正文

机器学习算法(五) 贝叶斯分类-鸢尾花数据集_用朴素贝叶斯算法实现鸢尾花

用朴素贝叶斯算法实现鸢尾花

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

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

 

  1. # import base package
  2. import warnings
  3. warnings.filterwarnings('ignore')
  4. import numpy as np
  5. from sklearn import datasets
  6. from sklearn.naive_bayes import GaussianNB
  7. from sklearn.model_selection import train_test_split
  8. # import data
  9. X, y = datasets.load_iris(return_X_y = True)
  10. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
  11. # train
  12. clf = GaussianNB(var_smoothing = 1e-8)
  13. clf.fit(X_train, y_train)
  14. print(clf)
  15. # evaluate y_test == y_pred rate
  16. y_pred = clf.predict(X_test)
  17. acc = np.sum(y_test == y_pred) / X_test.shape[0]
  18. print('y_test:', y_test)
  19. print('y_pred:', y_pred)
  20. print('X_test:', X_test.shape[0])
  21. print('test acc: %.3f' % acc)
  22. # predict
  23. y_proba = clf.predict_proba(X_test[:1])
  24. print('pre:', clf.predict(X_test[:1])) # three class proba
  25. print('probability value:', y_proba) # choose max from the three
  26. print('X_test: \n', X_test[:10])
  27. y_proba = clf.predict_proba(X_test[:5])
  28. print('pre:', clf.predict(X_test[:5]))
  29. print('probability value:', y_proba)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/340770
推荐阅读