Logistic Regression求解classification问题
classification问题和regression问题类似,区别在于y值是一个离散值,例如binary classification,y值只取0或1。
方法来自Andrew Ng的Machine Learning课件的note1的PartII,Classification and logsitic regression.
实验表明,通过多次迭代,能够最大化Likehood,使得分类有效,实验数据为人工构建,没有实际物理意义,matrix的第一列为x0,取常数1,第二列为区分列,第三列,第四列为非区分列,最后对预测起到主导地位的参数是theta[0]和theta[1]。
- #include "stdio.h"
- #include "math.h"
- double matrix[6][4]={{1,47,76,24}, //include x0=1
- {1,46,77,23},
- {1,48,74,22},
- {1,34,76,21},
- {1,35,75,24},
- {1,34,77,25},
- };
- double result[]={1,1,1,0,0,0};
- double theta[]={1,1,1,1}; // include theta0
- double function_g(double x)
- {
- double ex = pow(2.718281828,x);
- return ex/(1+ex);
- }
- int main(void)
- {
- double likelyhood = 0.0;
- float sum=0.0;
- for(int j = 0;j<6;++j)
- {
- double xi = 0.0;
- for(int k=0;k<4;++k)
- {
- xi += matrix[j][k]*theta[k];
- }
- printf("sample %d,%f\n",j,function_g(xi));
- sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;
- }
- printf("%f\n",sum);
- for(int i =0 ;i<1000;++i)
- {
- double error_sum=0.0;
- int j=i%6;
- {
- double h = 0.0;
- for(int k=0;k<4;++k)
- {
- h += matrix[j][k]*theta[k];
- }
- error_sum = result[j]-function_g(h);
- for(int k=0;k<4;++k)
- {
- theta[k] = theta[k]+0.001*(error_sum)*matrix[j][k];
- }
- }
- printf("theta now:%f,%f,%f,%f\n",theta[0],theta[1],theta[2],theta[3]);
- float sum=0.0;
- for(int j = 0;j<6;++j)
- {
- double xi = 0.0;
- for(int k=0;k<4;++k)
- {
- xi += matrix[j][k]*theta[k];
- }
- printf("sample output now: %d,%f\n",j,function_g(xi));
- sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;
- }
- printf("maximize the log likelihood now:%f\n",sum);
- printf("************************************\n");
- }
- return 0;
- }
Logistic Regression求解classification问题的更多相关文章
- Logistic Regression and Classification
分类(Classification)与回归都属于监督学习,两者的唯一区别在于,前者要预测的输出变量\(y\)只能取离散值,而后者的输出变量是连续的.这些离散的输出变量在分类问题中通常称之为标签(Lab ...
- 使用sklearn和caffe进行逻辑回归 | Brewing Logistic Regression then Going Deeper
原文首发于个人博客https://kezunlin.me/post/c50b0018/,欢迎阅读! Brewing Logistic Regression then Going Deeper. Bre ...
- More 3D Graphics (rgl) for Classification with Local Logistic Regression and Kernel Density Estimates (from The Elements of Statistical Learning)(转)
This post builds on a previous post, but can be read and understood independently. As part of my cou ...
- Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)
This semester I'm teaching from Hastie, Tibshirani, and Friedman's book, The Elements of Statistical ...
- 李宏毅机器学习笔记3:Classification、Logistic Regression
李宏毅老师的机器学习课程和吴恩达老师的机器学习课程都是都是ML和DL非常好的入门资料,在YouTube.网易云课堂.B站都能观看到相应的课程视频,接下来这一系列的博客我都将记录老师上课的笔记以及自己对 ...
- 机器学习理论基础学习3.3--- Linear classification 线性分类之logistic regression(基于经验风险最小化)
一.逻辑回归是什么? 1.逻辑回归 逻辑回归假设数据服从伯努利分布,通过极大化似然函数的方法,运用梯度下降来求解参数,来达到将数据二分类的目的. logistic回归也称为逻辑回归,与线性回归这样输出 ...
- Logistic Regression Using Gradient Descent -- Binary Classification 代码实现
1. 原理 Cost function Theta 2. Python # -*- coding:utf8 -*- import numpy as np import matplotlib.pyplo ...
- Classification week2: logistic regression classifier 笔记
华盛顿大学 machine learning: Classification 笔记. linear classifier 线性分类器 多项式: Logistic regression & 概率 ...
- Classification and logistic regression
logistic 回归 1.问题: 在上面讨论回归问题时.讨论的结果都是连续类型.但假设要求做分类呢?即讨论结果为离散型的值. 2.解答: 假设: 当中: g(z)的图形例如以下: 由此可知:当hθ( ...
随机推荐
- Docker简介/安装/使用
什么是Docker?docker是一个开源的应用容器引擎,系统级的轻量虚拟化技术.应用程序的自动化部署解决方案,能够迅速创建一个容器,并在容器上部署和运行应用程序,并通过配置文件可以轻松实现应用程序的 ...
- Swift完成fizz buzz test
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write ...
- High Executions Of Statement "delete from smon_scn_time..."
In this Document Symptoms Cause Solution APPLIES TO: Oracle Database - Enterprise Edition - Ve ...
- argparse库 学习记录
初始化 始见参数 name or flags action nargs default type choices required help dest metavar 总结 继上次的optparser ...
- Servlet - Listener、Filter、Decorator
Servlet 标签 : Java与Web Listener-监听器 Listener为在Java Web中进行事件驱动编程提供了一整套事件类和监听器接口.Listener监听的事件源分为Servle ...
- 通过grub-install命令把grub安装到u盘
通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...
- Intellij IDEA 插件开发之自建插件仓库
Intellij IDEA 有一个自己的官方的插件仓库,但是当我们的开发的 Intellij IDEA 的插件不能够对外公开时,我们就需要搭建自己的 Intellij IDEA 的插件仓库.前不久我们 ...
- Java基本语法-----java进制的转换
进制: 进制是一种记数方式 ,可以用有限的数字符号代表所有的数值.由特定的数值组成. 1整型的表现形式 1.十进制: 都是以0-9这九个数字组成,不能以0开头. 2.二进制: 由0和1两个数字组成. ...
- Findbug在项目中的运用--提高代码质量
FindBugs是一个静态分析工具,它检查类或者 JAR文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析 第一 手动安装 在Ec ...
- 使用java命令重启tomcat
public static void restartMyTomcat() { String location=ConfigUtil.getRestartLocation(); createCmdFil ...