python 単纯ベイズで手书き文字2クラス分类(MNIST)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 単纯ベイズで手书き文字2クラス分类(MNIST)相关的知识,希望对你有一定的参考价值。

import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.datasets import fetch_mldata
import matplotlib.pyplot as plt

def draw_digit(data):
    size = 28
    plt.figure(figsize=(3, 3))
    X, Y = np.meshgrid(range(size),range(size))
    Z = data.reshape(size,size)   # convert from vector to 28x28 matrix
    Z = Z[::-1,:]             # flip vertical
    plt.xlim(0,27)
    plt.ylim(0,27)
    plt.pcolor(X, Y, Z)
    plt.gray()
    plt.tick_params(labelbottom="off")
    plt.tick_params(labelleft="off")
    plt.show()

def NB():
    train_num = 8
    test_num = 128
    mnist = fetch_mldata('MNIST original', data_home = "./")
    #mnist.data = np.round(mnist.data.astype(np.float32)/255)
    for a in xrange(10):
        for b in xrange(10):
            if a == b:
                continue

            label = [a,b]
        
            extractData = [[[],[]],[[],[]]] 
        
            l_count = 0
            for n in label:
                for i in range(len(mnist.target)):
                    if mnist.target[i] == n:
                        extractData[l_count][0].append(n)
                        extractData[l_count][1].append(mnist.data[i])
                l_count += 1
            
            X_train = []
            X_train = extractData[0][1][0:train_num]
            X_train.extend(extractData[1][1][0:train_num])
            
            Y_train = []
            Y_train = extractData[0][0][0:train_num]
            Y_train.extend(extractData[1][0][0:train_num])
            
            X_test = []
            X_test = extractData[0][1][train_num + 1:train_num +test_num]
            X_test.extend(extractData[1][1][train_num + 1:train_num + test_num])
            
            Y_test = []
            Y_test = extractData[0][0][train_num + 1:train_num +test_num]
            Y_test.extend(extractData[1][0][train_num + 1:train_num + test_num])
        
            X = np.array(X_train)
            Y = np.array(Y_train)
            clf = MultinomialNB()
            clf.fit(X,Y)
            
            Z =  np.array(X_test)
            
            classify = clf.predict(Z)
            print label
            print np.mean(classify == Y_test)
            #clf.predict_proba(Z)
            #print classify == Y_test
            #draw_digit(X_test[139])
    
if __name__ == '__main__':
    NB()

以上是关于python 単纯ベイズで手书き文字2クラス分类(MNIST)的主要内容,如果未能解决你的问题,请参考以下文章

python 练习问题3.1コーシー·ベイズ推定量をモンテカルロ积分で计算する

python 2クラスのロジスティック回帰(勾配降下法版)

python 2クラスのロジスティック回帰(确率的勾配降下法版)

Odata実行命令

php タグクラウドカスタマイズ

text Java的クラス