机器学习之逻辑回归(logistics regression)代码(牛顿法实现)
来源:互联网 发布:windows repair汉化版 编辑:程序博客网 时间:2024/05/19 08:37
先贴一张图解释牛顿法原理:
然后以一道问题为例:
这个问题是《机器学习》周志华版本上的题目,给了西瓜的密度和含糖率数据,判断西瓜的好坏。数据在代码里。
下面贴一下代码:
<span style="font-size:14px;">clear all;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 onplot(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 = sigmoid(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)));endhold 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')</span>
最后,贴一下结果。
对训练数据的分类结果:
损失函数随迭代次数的变化,可以看出,下降非常快!
0 0
- 机器学习之逻辑回归(logistics regression)代码(牛顿法实现)
- 【机器学习 sklearn】逻辑斯蒂回归模型--Logistics regression
- 机器学习/逻辑回归(logistic regression)/--附python代码
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习算法之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归 Logistic Regression(一)
- 机器学习-logistic Regression(逻辑回归)
- 机器学习之逻辑回归 Logistic Regression(二)Python实现
- 逻辑回归(Logistic Regression)-牛顿法求解参数
- MySQL数据库Latin1转utf8编码
- 结构体struct字对齐问题
- Usage of mainframe DB2 trigger
- JavaScript变量作用域
- 【脑洞系列】胡策思维题
- 机器学习之逻辑回归(logistics regression)代码(牛顿法实现)
- ubuntu 16.04 64位安装flash
- LABVIEW输入控件的一些用法
- Qt-输入/输出设备(API & source code 简要分析,一小部分)
- [LeetCode]--55. Jump Game
- LML(Lifelong Machine Learning)介绍
- 深入 Docker:容器和镜像 原理分析
- 鱼的记忆[较为重要的知识点/技巧]
- Oracle学习笔记:游标Cursor