当前位置:   article > 正文

【人工智能】机器学习 -- 贝叶斯分类器

【人工智能】机器学习 -- 贝叶斯分类器

目录

一、使用Python开发工具,运行对iris数据进行分类的例子程序NaiveBayes.py,熟悉sklearn机器实习开源库。

1. NaiveBayes.py

2. 运行结果

二、登录https://archive-beta.ics.uci.edu/

三、使用sklearn机器学习开源库,使用贝叶斯分类器对breast-cancer-wisconsin.data进行分类。

1. Python代码

2. 运行截图

四、用java实现贝叶斯分类器算法,并对上述数据进行分类。

1. 流程图

2. 数据结构

3. 算法

4. 测试结果

五、心得体会


一、使用Python开发工具,运行对iris数据进行分类的例子程序NaiveBayes.py,熟悉sklearn机器实习开源库。

1. NaiveBayes.py

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn import datasets
  4. from sklearn.naive_bayes import GaussianNB
  5. import matplotlib
  6. # %matplotlib inline
  7. # 生成所有测试样本点
  8. def make_meshgrid(x, y, h=.02):
  9. x_min, x_max = x.min() - 1, x.max() + 1
  10. y_min, y_max = y.min() - 1, y.max() + 1
  11. xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
  12. np.arange(y_min, y_max, h))
  13. return xx, yy
  14. # 对测试样本进行预测,并显示
  15. def plot_test_results(ax, clf, xx, yy, **params):
  16. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  17. Z = Z.reshape(xx.shape)
  18. ax.contourf(xx, yy, Z, **params)
  19. # 载入iris数据集
  20. iris = datasets.load_iris()
  21. # 只使用前面连个特征
  22. X = iris.data[:, :2]
  23. # 样本标签值
  24. y = iris.target
  25. # 创建并训练正态朴素贝叶斯分类器
  26. clf = GaussianNB()
  27. clf.fit(X, y)
  28. title = ('GaussianBayesClassifier')
  29. fig, ax = plt.subplots(figsize=(5, 5))
  30. plt.subplots_adjust(wspace=0.4, hspace=0.4)
  31. X0, X1 = X[:, 0], X[:, 1]
  32. # 生成所有测试样本点
  33. xx, yy = make_meshgrid(X0, X1)
  34. # 显示测试样本的分类结果
  35. plot_test_results(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
  36. # 显示训练样本
  37. ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
  38. ax.set_xlim(xx.min(), xx.max())
  39. ax.set_ylim(yy.min(), yy.max())
  40. ax.set_xlabel('x1')
  41. ax.set_ylabel('x2')
  42. ax.set_xticks(())
  43. ax.set_yticks(())
  44. ax.set_title(title)
  45. plt.show()

2. 运行结果

二、登录https://archive-beta.ics.uci.edu/

可以查看提供的各类公共数据源,找到Breast Cancer Wisconsin (Original)数据并下载。

也可以直接输入网址:

https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/

下载wisconsin提供的乳腺肿瘤数breast-cancer-wisconsin.data(已经处理好的数据)和breast-cancer-wisconsin.names(对数据的说明,可以用写字体打开)

 在我上传的资源可以免费下载!!解压即可用【在本文置顶

 下载之后如下

三、使用sklearn机器学习开源库,使用贝叶斯分类器对breast-cancer-wisconsin.data进行分类。

1. Python代码

  1. from sklearn import datasets
  2. from matplotlib import pyplot as plt
  3. from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.naive_bayes import GaussianNB
  6. from sklearn.preprocessing import StandardScaler
  7. from sklearn.metrics import mean_squared_error, classification_report
  8. import pandas as pd
  9. import numpy as np
  10. # 构造列标签名字
  11. column = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
  12. 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli',
  13. 'Mitoses', 'Class']
  14. # 读取数据
  15. data = pd.read_csv(
  16. "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",
  17. names=column)
  18. print(data)
  19. # 缺失值进行处理
  20. data = data.replace(to_replace='?', value=np.nan)
  21. # 删除
  22. data = data.dropna()
  23. # 1-10列是特征值,最后一列10 代表11列目标值
  24. x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
  25. #
  26. clf = GaussianNB()
  27. clf.fit(x_train, y_train)
  28. title = ('GaussianBayesClassifier')
  29. y_predict = clf.predict(x_test)
  30. # 首先用分类器自带的.score方法来对准确性进行打印:
  31. print("准确率:", clf.score(x_test, y_test))
  32. print("召回率:", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))

2. 运行截图

四、用java实现贝叶斯分类器算法,并对上述数据进行分类。

1. 流程图

图4-1 主程序流程图

图4-2 贝叶斯分类器流程图

图4-3 计算条件概率流程图

2. 数据结构

(1)用一个二维动态数组存储测试和训练数据。

(2)用一个哈希表存储分类对应的数据

<键:不同的分类,值:分类的数组>  便于计算后验概率。

3. 算法

(1)对breast-cancer-wisconsin.data进行分类:分训练集和测试集再进行一个分类处理:

(2)分类

(3)计算条件概率

(4)贝叶斯分类器

4. 测试结果

(1)当测试和训练比例1:1

(2)当训练集为70%,测试集为30%

五、心得体会

更加深刻地理解了课件上的例子,实现了一个朴素贝叶斯算法。在实现的过程发现,如果不用拉普拉斯修正,结果是不合理的。

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

闽ICP备14008679号