• 为了保证你在浏览本网站时有着更好的体验,建议使用类似Chrome、Firefox之类的浏览器~~
    • 如果你喜欢本站的内容何不Ctrl+D收藏一下呢,与大家一起分享各种编程知识~
    • 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此,生命在于折腾,何不年轻时多折腾一下

机器学习导论(9)-对数回归牛顿法matlab与python实现

Matlab admin 3年前 (2016-11-18) 1540次浏览 0个评论 扫描二维码

实验采用的数据是周志华老师的机器学习清华版教材,这本书不错,好顶赞
Matlab

close all;  
clc;  
  
density = [0.697, 0.774, 0.634, 0.608, 0.556, 0.403, 0.481, 0.437, 0.666, 0.243, 0.245, 0.343, 0.639, 0.657, 0.360, 0.593, 0.719];  
density = density';  
sugar = [0.460, 0.376, 0.264, 0.318, 0.215, 0.237, 0.149, 0.211, 0.091, 0.267, 0.057, 0.099, 0.161, 0.198, 0.370, 0.042, 0.103];  
sugar = sugar';  
y = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];  
y = y';  
  
x = [ones(size(y,1),1) density  sugar];  
[m,n] = size(x);  
  
figure,  
pos = find(y);  
neg = find(y==0);  
plot(x(pos,2),x(pos,3),'o');  
hold on  
plot(x(neg,2),x(neg,3),'*');  
xlabel('density'),ylabel('sugar');  
  
  
theta = zeros(n,1);  
MaxIter = 10;  
J=zeros(MaxIter,1);  
  
for i=1:MaxIter  
    z = x*theta;  
    h = logsig(z);  
      
    grad = 1./m*x'*(h-y);  
    H = (1/m).*x' * diag(h) * diag(1-h) * x;  
    theta = theta-H\grad;  
       
    J(i) = 1/m*sum(-y.*log(h)-(1-y).*(log(1-h)));  
end  
  
hold on   
plot_x = [min(x(:,2)),max(x(:,2))];  
plot_y = (-1/theta(3))*(theta(1)+theta(2)*plot_x);  
plot(plot_x,plot_y);  
  
figure,  
plot(0:MaxIter-1,J,'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)  
xlabel('Iteration'); ylabel('J')

结果如下
1d48p1e3ps0e_wjd1u1

对应的偏差如下

y1fign6x_8lav73584tws
对应的 python 代码如下

 

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(inX):
    return 1.0/(1+np.exp(-inX))
wm_sugar=np.array( [0.460, 0.376, 0.264, 0.318, 0.215, 0.237, 0.149, 0.211, 0.091, 0.267, 0.057, 0.099, 0.161, 0.198, 0.370, 0.042, 0.103])
wm_density=np.array([0.697, 0.774, 0.634, 0.608, 0.556, 0.403, 0.481, 0.437, 0.666, 0.243, 0.245, 0.343, 0.639, 0.657, 0.360, 0.593, 0.719])
wm_lable=np.array([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
wm_lable.shape=(1,17)
t_lable=np.transpose(wm_lable)
x=np.array([np.ones(np.size(wm_lable,1)),wm_sugar,wm_density])
x_shape=np.shape(x)
#绘图
plt.figure(1)
pos=wm_lable==1
neg=wm_lable==0
plt.plot(x[1,pos[0,:]], x[2,pos[0,:]],'bx')
plt.hold(True)
plt.plot(x[1,neg[0,:]], x[2,neg[0,:]],'b*')
plt.xlabel('sugar')
plt.ylabel('density')
plt.hold(True)

# 开始牛顿法计算
theta=np.zeros([x_shape[0],1])
MaxIter = 10;  
J=np.zeros([MaxIter,1]);  
  
for i in range(MaxIter):  
    z = np.dot(np.transpose(x),theta)
    h = sigmoid(z)
      
    grad = 1.0/x_shape[1]*np.dot(x,(h-np.transpose(wm_lable)));  
    H = (1.0/x_shape[1])*np.dot(x,np.dot(np.diag(h[:,0]),np.dot(np.diag(1-h[:,0]),x.T)))  
    theta = theta-np.linalg.solve(H, grad)  
       
    J[i]= 1.0/x_shape[1]*sum(-t_lable[:,0]*np.log(h[:,0])-(1-t_lable[:,0])*(np.log(1-h[:,0])));  
plot_x = np.array([np.min(x[1,:]),np.max(x[2,:])])  
plot_y = (-1/theta[2])*(theta[0]+theta[1]*plot_x);  
plt.plot(plot_x, plot_y)
plt.show()
plt.figure(2) 
plt.plot(np.linspace(1, MaxIter, MaxIter),J)  
plt.xlabel('Iteration');
plt.ylabel('J')
plt.show()

实验结果
g7g6adxwk7g9m_a

l0dzl99iqovgozaykol


Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明机器学习导论(9)-对数回归牛顿法 matlab 与 python 实现
喜欢 (0)
admin
关于作者:
互联网行业码农一枚/业余铲屎官/数码影音爱好者/二次元

您必须 登录 才能发表评论!