博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
机器学习 logstic 实现二分类(matlab)
阅读量:4217 次
发布时间:2019-05-26

本文共 1321 字,大约阅读时间需要 4 分钟。

实现:

figure;pos = find(label == 1); neg = find(label == 0);plot(data(pos, 1), data(pos,2), '+')hold onplot(data(neg, 1), data(neg, 2), 'o')hold onxlabel('Exam 1 score')ylabel('Exam 2 score')%调用函数 求代价和迭代值[theta J] = logsticTest(data,label);plot_x = [min(data(:,1))-2,  max(data(:,1))+2];plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1)); %theta(1) + theta(2)x + theta(3)y = 0 转化为直线plot(plot_x,plot_y,'g');hold off;MAX_ITR = 7;figure;plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)xlabel('Iteration'); ylabel('J');

logsticTest函数:

function [theta J] = logsticTest(x,y)[m,n] = size(x);x = [ones(m,1),x];%添加第一列全为1theta = zeros(n+1, 1);% Define the sigmoid functiong = inline('1.0 ./ (1.0 + exp(-z))'); % Newton's methodMAX_ITR = 7;%最大迭代次数J = zeros(MAX_ITR, 1);for i = 1:MAX_ITR    % Calculate the hypothesis function    z = x * theta;%列向量    h = g(z);%计算sigmoid值 为列向量    % Calculate gradient and hessian.    % The formulas below are equivalent to the summation formulas    % given in the lecture videos.    grad = (1/m).*x' * (h-y);% 得到列向量 表示 theta(0),theta(1) ... theta(i),的偏导得到的    H = (1/m).* (x'*diag(h) * diag(1-h)*x);%这里采用 对角阵的方式实现 分别的对第i行数据乘积    % Calculate J (for testing convergence)    J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));    theta = theta - H\grad;end

分类结果:

迭代过程:

数据下面的连接中有

参考:

参考:

你可能感兴趣的文章
测试之途,前途?钱途?图何?
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>
input type=“button“与input type=“submit“的区别
查看>>
Linux文件和设备编程
查看>>
文件描述符
查看>>
终端驱动程序:几个简单例子
查看>>
HTML条件注释
查看>>
内核态与用户态
查看>>
趣链 BitXHub跨链平台 (4)跨链网关“初介绍”
查看>>
C++ 字符串string操作
查看>>
MySQL必知必会 -- 了解SQL和MySQL
查看>>
MySQL必知必会 -- 数据检索
查看>>
MySQL必知必会 -- 排序检索数据 ORDER BY
查看>>
POJ 3087 解题报告
查看>>