吴恩达机器学习笔记Python--ex3神经网络

导入数据集
导入ex3data1.mat,并显示100张图片

import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report

def loda_data(path,transpose=True):
    data=sio.loadmat(path)
    y=data.get('y') #(5000,1)
    y=y.reshape(y.shape[0]) #make sure it is column vector(make it back to column vector)
    X=data.get('X') #(5000,400)
    if transpose:
        X=np.array([im.reshape((20,20)).T for im in X])
        X=np.array([im.reshape(400) for im in X])
    return X,y
raw_X,raw_y=loda_data(r'D:\MachineLearning\ex3\ex3data1.mat')
#print(raw_X.shape,raw_y.shape) (5000,400)(5000,)

def plot_100_image(X):
    size=int(np.sqrt(X.shape[1]))
    sample_idx=np.random.choice(np.arange(X.shape[0]),100)#100*400
    sample_images=X[sample_idx,:]
    fig,ax_array=plt.subplots(nrows=10,ncols=10,sharey=True,sharex=True,figsize=(8,8))
    for r in range(10):
        for c in range(10):
            ax_array[r,c].matshow(sample_images[10*r+c].reshape((size,size)),cmap=matplotlib.cm.binary)
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))
 plot_100_image(raw_X)
 plt.show()

成功导入的显示结果如下图所示
在这里插入图片描述准备数据
图片来自https://github.com/fengdu78/Coursera-ML-AndrewNg-Notes/blob/ed91b58dc1e09113a93e5b90465d5ff70dcfc4c5/code/ex3-neural%20network/%E5%90%91%E9%87%8F%E5%8C%96%E6%A0%87%E7%AD%BE.png?raw=true
正则化cost和gradient

#regularized the cost function
def cost(theta,X,y):
    return np.mean(-y*np.log(sigmoid(X@theta))-(1-y)*np.log(1-sigmoid(X@theta)))
def regularized_cost(theta,X,y,l=1):
    #you dont penalize theta_0
    theta_j1_to_n=theta[1:]
    regularized_term=(l/(2*len(X)))*np.power(theta_j1_to_n,2).sum()
    return cost(theta,X,y)+regularized_term

#regularized the gradient
def regularized_gradient(theta,X,y,l=1):
    #still, leave theta_0 alone
    theta_j1_to_n=theta[1:]
    regularized_theta=(l/len(X))*theta_j1_to_n
    #by doing this , no offset is on theta_0
    regularized_term=np.concatenate([np.array([0]),regularized_theta])#concatenate用于多数组的拼接,默认axis=0
    #axis应该从轴来考虑,为1时是横轴从左到右,为0时从上到下
    return gradient(theta,X,y)+regularized_term
def sigmoid(z):
    return 1/(1+np.exp(-z))
def gradient(theta,X,y):
    return (1/len(X))*X.T@(sigmoid(X@theta)-y)
def logistic_regression(X,y,l=1):
    theta=np.zeros(X.shape[l])
    res=opt.minimize(fun=regularized_cost,x0=theta,args=(X,y,l),method='TNC',jac=regularized_gradient,options={'disp':True})
    final_theta=res.x
    return final_theta
def predict(x,theta):
    prob=sigmoid(x@theta)
    return (prob>=0.5).astype(int)
t0=logistic_regression(X,y[0])
#print(t0.shape)
y_pred=predict(X,t0)
#print('Accuracy={}'.format(np.mean(y[0]==y_pred)))

#train k model
k_theta=np.array([logistic_regression(X,y[k]) for k in range(10)])
#print(k_theta.shape)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值