吴恩达机器学习第三周编程作业(Python实现)

课程作业 提取码:ayri
1、逻辑回归(无正则化)
ex2.py

import numpy as np
from plotData import *
from costFunction import *
import scipy.optimize as opt
import plotDecisionBoundary as pdb
import predict as predict
from sigmoid import *

data=np.loadtxt('./data/ex2data1.txt',delimiter=',')
X=data[:,0:2]
y=data[:,2]

 # ==================== Part 1: Plotting ====================

plot_data(X,y)

plt.axis([30,100,30,100])
plt.legend(['Admitted','Not Admitted'],loc=1)
plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
plt.show()

 # ============ Part 2: Compute Cost and Gradient ============
(m,n)=X.shape

X=np.c_[np.ones(m),X]

initial_theta=np.zeros(n+1)

cost,grad=cost_Function(initial_theta,X,y)
print('Cost at initial theta (zeros): \n', cost)
print('Expected cost (approx): 0.693\n')
print('Gradient at initial theta (zeros): \n')
print(grad)
print('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n')

test_theta = np.array([-24, 0.2, 0.2])
cost, grad = cost_Function(test_theta, X, y)
print('\nCost at test theta: \n', cost)
print('Expected cost (approx): 0.218\n')
print('Gradient at test theta: \n')
print(grad)
print('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n')

# == == == == == == = Part3: Optimizing using fminunc == == == == == == =
def cost_func(t):
    return cost_Function(t,X,y)[0]
def grad_func(t):
    return cost_Function(t,X,y)[1]

theta,cost, *unused=opt.fmin_bfgs(f=cost_func,fprime=grad_func,x0=initial_theta,maxiter=400,full_output=True,disp=False)
print('Cost at theta found by fmin: {:0.4f}'.format(cost))
print('Expected cost (approx): 0.203')
print('theta: \n{}'.format(theta))
print('Expected Theta (approx): \n-25.161\n0.206\n0.201')

pdb.plot_decision_boundary(theta, X, y)

plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
plt.show()

# ============== Part 4: Predict and Accuracies ==============
prob =sigmoid(np.array([1,45,85]).dot(theta))
print('For a student with scores 45 and 85, we predict an admission probability of {:0.4f}'.format(prob))
print('Expected value : 0.775 +/- 0.002')

p = predict.predict(theta, X)
#与期望值进行比较 验证正确性
print('Train accuracy: {}'.format(np.mean(y == p) * 100))
print('Expected accuracy (approx): 89.0')

input('ex2 Finished. Press ENTER to exit')

costFunction.py

import numpy as np
def h(theta,X):
    z=np.dot(X,theta)
    g=1/(1+np.exp(-z))
    return g
def cost_Function(theta,X,y):
    m=y.size
    myh=h(theta,X)
    t1=-y.dot(np.log(myh))
    t2=(1-y).dot(np.log(1-myh))
    cost=(t1-t2)/m
    grad=(myh-y).dot(X)/m
    return cost,grad

plotData.py

import matplotlib.pyplot as plt
def plot_data(X,y):
    plt.figure()

    postive=X[y==1]
    negtive=X[y==0]

    plt.scatter(postive[:,0],postive[:,1],marker='+' , c='black',alpha=0.8,label='Admitted')
    plt.scatter(negtive[:,0],negtive[:, 1], marker='o', c='yellow', alpha=0.8,label='Not Admitted')

mapFeature.py

import numpy as np


def map_feature(x1, x2):
    degree = 6

    x1 = x1.reshape((x1.size, 1))
    x2 = x2.reshape((x2.size, 1))
    result = np.ones(x1[:, 0].shape)

    for i in range(1, degree + 1):
        for j in range(0, i + 1):
            result = np.c_[result, (x1**(i-j)) * (x2**j)]

    return result

plotDecisionBoundary.py

import matplotlib.pyplot as plt
import numpy as np
from plotData import *
from mapFeature import *

def plot_decision_boundary(theta,X,y):
    plot_data(X[:,1:3],y)

    if X.shape[1]<=3:
        plot_x=np.array([np.min(X[:,1])-2,np.max(X[:,1])+2])

        plot_y=(-1/theta[2])*(theta[1]*plot_x+theta[0])

        plt.plot(plot_x,plot_y)

        plt.legend(['Decision Boundary', 'Admitted', 'Not admitted'], loc=1)
        plt.axis([30,100,30,100])

    else:
        u = np.linspace(-1, 1.5, 50)
        v = np.linspace(-1, 1.5, 50)

        z = np.zeros((u.size, v.size))

        # Evaluate z = theta*x over the grid
        for i in range(0, u.size):
            for j in range(0, v.size):
                z[i, j] = np.dot(map_feature(u[i], v[j]), theta)

        z = z.T

        # Plot z = 0
        # Notice you need to specify the range [0, 0]
        cs = plt.contour(u, v, z, levels=[0], colors='r', label='Decision Boundary')
        plt.legend([cs.collections[0]], ['Decision Boundary'])

sigmoid.py

import numpy as np
def sigmoid(z):
    g = np.zeros(z.size)
    g = 1 / (1 + np.exp(-z))
    return g

predict.py

import numpy as np
from sigmoid import *
def predict(theta, X):
    m = X.shape[0] #样本数
    p = np.zeros(m) #每个样本预测的标签
    p=sigmoid(X.dot(theta))  #每个样本属于正类的概率
    p[p>=0.5]=1  #概率大于等于0.5 认为属于正类 标签为1 否则为0
    p[p<0.5]=0
    return p

在这里插入图片描述

2、逻辑回归(有正则化)
ex2_reg.py

import numpy as np
from plotData import *
import matplotlib.pyplot as plt
from mapFeature import *
from costFunction import *
import scipy.optimize as opt
import plotDecisionBoundary as pdb
import predict as predict
data=np.loadtxt('./data/ex2data2.txt',delimiter=',')
X=data[:,0:2]
y=data[:,2]

plot_data(X,y)
plt.xlabel('Microchip Test 1')
plt.ylabel('Microchip Test 2')
plt.legend(['y=1','y=0'])
plt.show()

# == == == == == = Part1: Regularized Logistic egression == == == == == ==
X=map_feature(X[:,0],X[:,1])

initial_theta=np.zeros(X.shape[1])
lmd=1
cost,grad=cost_function_reg(initial_theta,X,y,lmd)

np.set_printoptions(formatter={'float': '{: 0.4f}\n'.format})
print('Cost at initial theta (zeros): {}'.format(cost))
print('Expected cost (approx): 0.693')
print('Gradient at initial theta (zeros) - first five values only: \n{}'.format(grad[0:5]))
print('Expected gradients (approx) - first five values only: \n 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115')
input('Program paused. Press ENTER to continue')
test_theta = np.ones(X.shape[1])# 计算参数非0(1)时的代价函数值和梯度cost, grad = cfr.cost_function_reg(test_theta, X, y, lmd)
# #与期望值比较 验证正确性
print('Cost at test theta: {}'.format(cost))
print('Expected cost (approx): 2.13')
print('Gradient at test theta - first five values only: \n{}'.format(grad[0:5]))
print('Expected gradients (approx) - first five values only: \n 0.3460\n 0.0851\n 0.1185\n 0.1506\n 0.0159')

# ============= Part 2: Regularization and Accuracies =============
initial_theta=np.zeros(X.shape[1])
lmd=1

def cost_func(t):
    return cost_function_reg(t,X,y,lmd)[0]
def grad_func(t):
    return cost_function_reg(t,X,y,lmd)[1]
theta,cost,*unused=opt.fmin_bfgs(f=cost_func,fprime=grad_func,x0=initial_theta,maxiter=400,full_output=True,disp=False)
print('Plotting decision boundary...')
pdb.plot_decision_boundary(theta,X,y)
plt.title('lambda={}'.format(lmd))

plt.xlabel('Microchip Test 1')
plt.ylabel('Microchip Test 2')

plt.show()

p=predict.predict(theta,X)
print('Train Accuracy: {:0.4f}'.format(np.mean(y == p) * 100))

print('Expected accuracy (with lambda = 1): 83.1 (approx)')

costFunction.py

import numpy as np
def h(theta,X):
    z=np.dot(X,theta)
    g=1/(1+np.exp(-z))
    return g
def cost_function_reg(theta,X,y,lmd):
    m=y.size
    cost=0
    grad=np.zeros(theta.shape)
    myh=h(theta,X)
    term1=-y.dot(np.log(myh))
    term2=(1-y).dot(np.log(1-myh))
    term3=(lmd/(2*m)*(theta[1:].dot(theta[1:])))
    cost=(term1-term2)/m+term3

    grad=(myh-y).dot(X)/m
    grad[1:]+=(lmd/m)*theta[1:]
    return cost,grad

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值