• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-24
  • 语言: Python
  • 标签: python  贝叶斯  mnist  

资源简介

基于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


评论

共有 条评论