资源简介
基于Mnist数据集的贝叶斯分类器,用python编写,读取原始数据集数据,进行分类,完整,简洁
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Tue May 01 16:08:32 2018
@author: Fsl
“““
import numpy as np
import cv2
import time
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from readset import loadImageSet
from readset import loadLabelSet
# 二值化
def binaryzation(img):
cv_img = img.astype(np.uint8)
cv2.threshold(cv_img501cv2.cv.CV_THRESH_BINARY_INVcv_img)
return cv_img
def Train(trainsettrain_labels):
prior_probability = np.zeros(class_num) # 先验概率
conditional_probability = np.zeros((class_numfeature_len2)) # 条件概率
# 计算先验概率及条件概率
for i in range(len(train_labels)):
img = binaryzation(trainset[i]) # 图片二值化
imgsize=np.reshape(img[2828])
label = train_labels[i]
prior_probability[label] += 1
for j in range(feature_len):
t=img[j]
conditional_probability[label[0]][j][t] += 1
# 将概率归到[1.10001]
for i in range(class_num):
for j in range(feature_len):
# 经过二值化后图像只有0,1两种取值
pix_0 = conditional_probability[i][j][0]
pix_1 = conditional_probability[i][j][1]
# 计算0,1像素点对应的条件概率
probalility_0 = (float(pix_0)/float(pix_0+pix_1))*1000000 + 1
probalility_1 = (float(pix_1)/float(pix_0+pix_1))*1000000 + 1
conditional_probability[i][j][0] = probalility_0
conditional_probability[i][j][1] = probalility_1
return prior_probabilityconditional_probability
# 计算概率
def calculate_probability(imglabel):
probability = int(prior_probability[label])
for i in range(len(img)):
probability *= int(conditional_probability[label][i][img[i]])
return probability
def Predict(testsetprior_probabilityconditional_probability):
predict = []
for img in testset:
# 图像二值化
img = binaryzation(img)
max_label = 0
max_probability = calculate_probability(img0)
for j in range(110):
probability = calculate_probability(imgj)
if max_probability < probability:
max_label = j
max_probability = probability
predict.append(max_label)
return np.array(predict)
class_num = 10
feature_len = 784
if __name__ == ‘__main__‘:
print ‘Start read data‘
time_1 = time.time()
# 读取训练数据与测试数据
train_features=loadImageSet(0)
test_features=loadImageSet(1)
train_labels=loadLabelSet(0)
test_labels = loadLabelSet(1)
# print train_features.shape
# print train_features.shape
time_2 = time.time()
print ‘read data cost ‘time_2 - time_1‘ second‘‘\n‘
print ‘Start training‘
prior_probabilityconditional_probability = Train(train_featurestrain_labels)
time_3 = time.time()
print ‘training cost ‘time_3 - time_2‘ second‘‘\n‘
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3548 2018-05-02 13:24 基于Mnist数据集的贝叶斯分类器\bayes.py
文件 1844 2018-05-01 16:51 基于Mnist数据集的贝叶斯分类器\readset.py
目录 0 2018-05-04 17:57 基于Mnist数据集的贝叶斯分类器
----------- --------- ---------- ----- ----
5392 3
相关资源
- python题库112732
- python 实现股票分时图K线图及抓取免费
- textrank自动文摘抽取python代码
- arcpy 工具包
- pyexcelerator
- PYTHON题库
- MNIST数据集获取 input_data.py
- 利用selenium编写的python网络爬虫-淘宝
- 人脸检测python源代码
- python实现可暂停的动态曲线绘制,横
- python2048游戏源代码
- 机器学习-python处理UCI鲍鱼数据集.ra
- python带基因元胞自动机代码
- python-web系统实时监控
- numpy-1.17.0+mkl-cp37-cp37m-win_amd64.whl百度云
- 四行Python代码实现将word文件转换为
- python评分卡模型数据源
- 传智播客 python基础班 + 就业班 + 课件
- python实现简易3D方块动画
- 利用贝叶斯算法实现垃圾邮件分类
- 基于PYTHON+OPENCV的SIFT SURF图像特征匹配
- python总结
- boost.python 动态编译库
- 高斯投影正反算Python源码
- python xlutils
- Python3网络爬虫实战思维导图
- ros_arduino_python 调通版
- 数据挖掘十大算法源代码Python)
- XModem -发送端源代码Python语言实现
- pytracking/pytracking/evaluation/tracker.py
评论
共有 条评论