当前位置:   article > 正文

深度学习&机器学习——python手写朴素贝叶斯识别mnist数据集_朴素贝叶斯 mnist pytorch

朴素贝叶斯 mnist pytorch

 引入了手写读取mnist数据集的模块Read_File+内置可视化函数

Read_File模块参见icon-default.png?t=M4ADhttps://blog.csdn.net/wangdiedang/article/details/125335812?spm=1001.2014.3001.5502

  1. # creator : wangdiedang
  2. # time : 2022/6/7 11:57
  3. # filename : Bayes.py
  4. # 引入读文件模块
  5. import Read_File as RF
  6. import numpy as np
  7. from collections import Counter
  8. import time
  9. def featureExtraction(img, dim, num):
  10. res = np.empty((dim, dim))
  11. for i in range(0, dim):
  12. for j in range(0, dim):
  13. # 算出每一个片区像素点的个数 若大于某一特定数则设为1
  14. tmp = img[num * i:num * (i + 1), num * j:num * (j + 1)].sum()
  15. if tmp > max((28 // dim - 1), 1):
  16. res[i, j] = 1
  17. else:
  18. res[i, j] = 0
  19. return res
  20. # 输入图像集和转化维度
  21. def Extraction2AllImgs(imgs, dim):
  22. res = np.empty((imgs.shape[0], dim, dim))
  23. num = 28 // dim
  24. for k, img in enumerate(imgs):
  25. # 对于每一个图像进行特征降维
  26. res[k] = featureExtraction(imgs[k], dim, num)
  27. return res
  28. def read_data(dim=7):
  29. # 返回生数据 二值图像
  30. a, b, c, d = RF.read_main()
  31. RF.show_img(a)
  32. # 降低训练集和测试集的特征维度 初始28*28转化为dim*dim
  33. if dim < 28:
  34. a = Extraction2AllImgs(a, dim)
  35. c = Extraction2AllImgs(c, dim)
  36. return a, b, c, d
  37. # 训练所有图片
  38. def trainsAllImgs(train_imgs, train_labels):
  39. # shape 为(60000, dim, dim)
  40. total, row, col = train_imgs.shape
  41. # 统计出标签在训练集中总计个数
  42. cnt = Counter(train_labels)
  43. # 初始化先验概率矩阵 维度为(10,)
  44. priori_ = np.empty(10)
  45. for i in range(10):
  46. # 拉普拉斯修正
  47. priori_[i] = (cnt[i] + 1) / (total + 10)
  48. # 将图像矩阵转化为二维矩阵方便计算 shape为(60000, dim*dim)
  49. new_train_imgs = train_imgs.reshape(total, row * col)
  50. # 初始化后验概率矩阵 维度为(10, dim*dim)
  51. posterior_ = np.empty((10, row * col))
  52. for i in range(10):
  53. # 在为某一数时,某一像素点取值为1的频率 取值为0的频率为 1 - posterior_[i]
  54. # 拉普拉斯修正
  55. posterior_[i] = (new_train_imgs[train_labels == i].sum(axis=0) + 1) / (cnt[i] + 2)
  56. return priori_, posterior_
  57. # 利用 先验和后验概率进行极大似然估计分类
  58. def bayesClassifier_MLE(test_imgs, priori_, posterior_):
  59. row, col = test_imgs.shape
  60. new_test_imgs = test_imgs.reshape(row * col)
  61. # 初始化标签对应属性的取值概率
  62. each_P = np.empty(10)
  63. for j in range(10):
  64. tmpsum = 0
  65. # 对数似然求和 防止连乘下溢
  66. for i, c in enumerate(new_test_imgs):
  67. if new_test_imgs[i] == 0:
  68. tmpsum += np.log(1 - posterior_[j][i])
  69. else:
  70. tmpsum += np.log(posterior_[j][i])
  71. each_P[j] = np.log(priori_[j]) + tmpsum
  72. return np.argmax(each_P)
  73. def evaluate_Bayes_model_MLE(train_imgs, train_labels, test_imgs, test_labels):
  74. # forecast = np.empty(test_labels.shape[0])
  75. # 初始化预测正确的个数
  76. correctCnt = 0
  77. # 使用测试集训练并返回先验概率和后验概率
  78. print("-----训练模型获得模型的先验和后验概率-----")
  79. priori_, posterior_ = trainsAllImgs(train_imgs, train_labels)
  80. print("-----成功获得模型的先验和后验概率-----")
  81. # print(priori_)
  82. # print(posterior_)
  83. # 利用先验概率和后验概率进行对测试集的预测
  84. print("-----将测试集载入模型进行精确度评估-----")
  85. for i, img in enumerate(test_imgs):
  86. t = bayesClassifier_MLE(test_imgs[i], priori_, posterior_)
  87. # print(t, test_labels[i])
  88. if t == test_labels[i]:
  89. correctCnt += 1
  90. # forecast[i] = bayesClassifier_MLE(img, priori_, posterior_)
  91. current_time = time.time()
  92. print("-----模型评估结束-----")
  93. print("--------------------------------------------------------------")
  94. print("训练模型样本:%d,样本尺寸(%d, %d)" % (60000, dim, dim))
  95. print("总样本: %d, 预测成功数: %d, 预测成功率: %.3f" % (test_labels.shape[0], correctCnt, correctCnt / test_labels.shape[0] * 100) + "%")
  96. print("运行时间为" + str(current_time - old_time) + "s")
  97. print("--------------------------------------------------------------")
  98. if __name__ == '__main__':
  99. old_time = time.time()
  100. print("-----读取数据集-----")
  101. dim = 28
  102. train_imgs, train_labels, test_imgs, test_labels = read_data(dim)
  103. RF.show_img(train_imgs)
  104. print("-----读取成功开始训练-----")
  105. # 开始训练
  106. evaluate_Bayes_model_MLE(train_imgs, train_labels, test_imgs, test_labels)

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

闽ICP备14008679号