吴恩达机器学习编程题三(逻辑回归解决多分类问题)(Python)

import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio

data = sio.loadmat('ex3data1.mat')

data

print(type(data))

data.keys()

raw_X = data['X']
raw_y = data['y']

print(raw_X.shape,raw_y.shape)

def plot_an_image(X):                    #显示一张图片
    
    pick_one = np.random.randint(5000)   #随机选取一个
    
    image = X[pick_one,:]  
    
    fig,ax = plt.subplots(figsize=(1,1))
    ax.imshow(image.reshape(20,20).T,cmap = 'gray_r')
    
    plt.xticks([])                       ##去掉图片显示的刻度
    plt.yticks([])
    
    
plot_an_image(raw_X)

def plot_100_image(X):        
    
    sample_index = np.random.choice(len(X),100)      #随机选取100张图片
    images = X[sample_index,:]  
    print(images.shape)
    
    fig,ax = plt.subplots(ncols=10,nrows=10,figsize=(8,8),sharex=True,sharey=True)  #10×10网格,共享x,y属性
    
    for r in range(10):
        for c in range(10):
            
            ax[r,c].imshow(images[10 * r + c].reshape(20,20).T,cmap='gray_r')
            
    
    plt.xticks([])
    plt.yticks([])
    
    plt.show
    
    
plot_100_image(raw_X)

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


def costFunction(theta,X,y,lamda):
    A = sigmoid(X@theta)
    
    first = y*np.log(A)
    second = (1-y) * np.log(1-A)
    
#     reg = np.sum(np.power(theta[1:],2)) * (lamda / (2 * len(X)))        #这行代码也可以算内积
    reg = theta[1:] @ theta[1:] * (lamda / (2 * len(X)))                  #算内积
    return -np.sum(first + second ) / len(X) + reg


def gradient_reg(theta,X,y,lamda):
    reg = theta[1:] * (lamda / len(X))
    reg = np.insert(reg,0,values=0,axis=0)
    
    first = (X.T@(sigmoid(X@theta) - y)) / len(X)
    
    return first + reg


X  = np.insert(raw_X,0,values=1,axis=1)
X.shape

y = raw_y.flatten()
y.shape

from scipy.optimize import minimize

def one_vs_all(X,y,lamda,K):
    
    n = X.shape[1]                    #1代表X的列数,如果是0,则代表X的行数
    
    theta_all = np.zeros((K,n))
    
    for i in range(1,K+1):
        theta_i = np.zeros(n,)
        
        res = minimize(fun =costFunction,
                      x0 = theta_i,
                      args = (X, y == i,lamda),
                      method = 'TNC',
                      jac =gradient_reg )
        theta_all[i-1,:] = res.x
        
    return theta_all

lamda  = 1
K =10

theta_final = one_vs_all(X,y,lamda,K)

theta_final

def predict(X,theta_final):
    
    h = sigmoid(X@theta_final.T) #(5000,401) (10,401) =>(5000,10)
    
    h_argmax = np.argmax(h,axis=1)
    
    return h_argmax + 1


y_pred = predict(X,theta_final)

acc = np.mean(y_pred == y)

acc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值