吴恩达机器学习作业笔记(三)多分类实现手写数字分类

手写数字分类

以下为参考,整理学习
吴恩达机器学习作业
作业参考

调用库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.io import loadmat

准备数据

data = loadmat('ex3data1.mat')

画出输出图

plt.rcParams['figure.figsize'] = (15.0, 8.0)
for i in range(10):
    plt.subplot(1,10,i+1)
    index = i*500
    plt.title(data['y'][index,0])
    plt.imshow(data['X'][index].reshape((20,20)).astype('uint8'))
    plt.axis('off')
plt.show

定义激活函数

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

定义损失函数

def cost(theta, X, y, learningRate):#学习率
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
    cross_cost = np.multiply(-y,np.log(sigmoid(X * theta.T)))-np.multiply((1-y),np.log(1-sigmoid(X * theta.T)))
    reg = (learningRate / (2 * len(X))) * np.sum(np.power(theta[1:], 2))
    whole_cost = np.sum(cross_cost) / len(X) + reg
    return whole_cost

定义梯度函数

def gradient(theta, X, y, learningRate):
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
    parameters = int(theta.ravel().shape[1])
    error = sigmoid(X * theta.T) - y
    grad = ((X.T * error) / len(X)).T + ((learningRate / len(X) * theta))
    grad[0, 0] = np.sum(np.multiply(error, X[:,0])) / len(X)
    #更新第一项不加正则项求导
    return np.array(grad).ravel()

梯度下降算法

from scipy.optimize import minimize
def one_vs_all(X, y, num_labels, learning_rate):
    rows = X.shape[0]
    params = X.shape[1]
    all_theta = np.zeros((num_labels, params + 1))
    X = np.insert(X, 0, values=np.ones(rows), axis=1)#插入一列1
    for i in range(1, num_labels + 1):
        theta = np.zeros(params + 1)
        y_i = np.array([1 if label == i else 0 for label in y])
        y_i = np.reshape(y_i, (rows, 1))
        fmin = minimize(fun=cost, x0=theta, args=(X, y-i, learning_rate), method='TNC', jac=gradient)
        all_theta[i-1,:] = fmin.x
        print(i)
    return all_theta

计算

rows = data['X'].shape[0]
params = data['X'].shape[1]
all_theta = np.zeros((10, params + 1))
X = np.insert(data['X'], 0, values=np.ones(rows), axis=1)
theta = np.zeros(params + 1)
y_0 = np.array([1 if label == 0 else 0 for label in data['y']])
y_0 = np.reshape(y_0, (rows, 1))
np.unique(data['y'])
all_theta = one_vs_all(data['X'], data['y'], 10, 1)

评价

def predict_all(X, all_theta):
    rows = X.shape[0]
    params = X.shape[1]
    num_labels = all_theta.shape[0]
    X = np.insert(X, 0, values=np.ones(rows), axis=1)
     X = np.matrix(X)
    all_theta = np.matrix(all_theta)
    h = sigmoid(X@all_theta.T)
    h_argmax = np.argmax(h, axis=1)
    h_argmax = h_argmax + 1
    return h_argmax

预测

y_pred = predict_all(data['X'], all_theta)
correct = [1 if a == b else 0 for (a, b) in zip(y_pred, data['y'])]
accuracy = (sum(map(int, correct)) / float(len(correct)))
print ('accuracy = {0}%'.format(accuracy * 100))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值