逻辑回归原理及softmax回归实现mnist数据集分类python代码




softmax回归实现mnist数据集分类python代码:

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import struct 

#image是一个n*m的数组,n是样本数(行数),m是特征数(列数)。训练数据集包含60000个样本,测试数据集包含10000个样本。在mnist数据集中的每张图片由28*28个像素点构成,每个像素点用一个灰度值表示。我们将28*28的像素展开为一个一维的行向量(每行784个值,或者说每行代表一张图片)。
#label包含相应的目标变量,也就是手写数字的类标签(整数0-9)。
image = []; label = []; testImage = []; testLabel = []
trainData = 60000; testData = 10000

fileImage = 'D:\\Python\\pythonAZ\\Lib\\idlelib\\mnist\\train-images.idx3-ubyte' #训练数据集路径
fileLabel = 'D:\\Python\\pythonAZ\\Lib\\idlelib\\mnist\\train-labels.idx1-ubyte' #训练数据集路径
fileTestImage = 'D:\\Python\\pythonAZ\\Lib\\idlelib\\mnist\\t10k-images.idx3-ubyte' #测试数据集路径
fileTestLabel = 'D:\\Python\\pythonAZ\\Lib\\idlelib\\mnist\\t10k-labels.idx1-ubyte' #测试数据集路径

frImage = open(fileImage,'rb')
bufImage = frImage.read()
indexImage = 0
indexImage += struct.calcsize('>IIII')
#">"是指大端(用来定义字节是如何存储的)。
#"I"这是指一个无符号整数

for i in range(trainData):
    image_i = struct.unpack_from('>784B',bufImage,indexImage)
    image.append(image_i)
    indexImage += struct.calcsize('>784B')
    
frTestImage = open(fileTestImage,'rb')
bufTestImage = frTestImage.read()
indexTestImage = 0
indexTestImage += struct.calcsize('>IIII')

for i in range(testData):
    testImage_i = struct.unpack_from('>784B',bufTestImage,indexTestImage)
    testImage.append(testImage_i)
    indexTestImage += struct.calcsize('>784B')
        
frLabel = open(fileLabel,'rb')
bufLabel = frLabel.read()
indexLabel = 0
indexLabel += struct.calcsize('>II')

label_j = np.zeros((trainData, 10))
     
for i in range(trainData):
    label_i = struct.unpack_from('>1B',bufLabel,indexLabel)
    label_k = label_j[i]
    label_k[label_i] = 1
    label.append(label_k)
    indexLabel += struct.calcsize('1B')
    
frTestLabel = open(fileTestLabel,'rb')
bufTestLabel = frTestLabel.read()
indexTestLabel = 0
indexTestLabel += struct.calcsize('>II')

testLabel_j = np.zeros((testData, 10))
     
for i in range(testData):
    testLabel_i = struct.unpack_from('>1B',bufTestLabel,indexTestLabel)
    testLabel_k = testLabel_j[i]
    testLabel_k[testLabel_i] = 1
    testLabel.append(testLabel_k)
    indexTestLabel += struct.calcsize('1B')
    
#显示图片,改变image中的标签显示不同的图片
im = image[50]
im = np.array(im)  
im = im.reshape(28,28)  
  
fig = plt.figure()  
plotwindow = fig.add_subplot(111)  
plt.imshow(im , cmap = 'gray')  
plt.show()

def objective(w,x,k):
    pp = np.dot(x, w.T)
    pp = pp - np.max(pp)
    ppp = np.exp(pp)
    b = np.sum(ppp,axis = 1)
    b = b.reshape(k,1)
    p = ppp/b
    return p
       
def loss_function(w,x):
    p = np.log(objective(w,x,trainData))
    j = -1/60000 * np.sum(np.dot(np.array(label).T , p))
    return j

def gradweight(w,x):
    pp = objective(w,x,trainData)
    alpha = 1
    gradw = -1/60000 * np.dot((label - pp).T,x)
    w = w - alpha*gradw
    return w

def softmax():
    maxCycles = 600
    x = np.array(image) / 255
    w = np.random.rand(10,784)
    J = []
    for i in range(maxCycles):
        w = gradweight(w,x)
        J.append(loss_function(w,x))
    
    plt.plot(J)
    plt.ylabel('The loss function')
    plt.show()
    return w

def test_function(w):
    x = np.array(testImage) / 255
    #w = softmax()
    count = 0
    p = objective(w,x,testData)
    index1 = np.argmax(p,axis=1) #np.argmax()返回最大值的下标
    index2 = np.argmax(testLabel,axis=1)
    for i in range(10000):
        if index1[i] == index2[i]:
            count = count+1
    corrent_rate = count/testData
    return corrent_rate

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值