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θ( ...
随机推荐
- Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53303872 本文出自:[openXu的博客] 1分析 2定义组合控件布局 3继承最外层控件 ...
- How to Collect Bne Log Files for GL Integrators
In this Document Goal Solution APPLIES TO: Oracle General Ledger - Version 11.0 and laterInforma ...
- Android必知必会-App 常用图标尺寸规范汇总
若移动端访问不佳,请使用 –> Github版 内容持续更新中,更新日期:2016-08-11 1. 程序启动图标(icon launcher) 放在mipmap-*dpi下,文件名为ic_la ...
- EBS开发性能优化之查找需要优化的程序
1.登陆数据库LINUX环境 使用 top 命令查看进程状况 [oratest@ebsdb~]$top top - 15:58:59 up 8 days, 22:04, 1 user, load ...
- Scikit-learn:scikit-learn快速教程及实例
http://blog.csdn.net/pipisorry/article/details/52251305 :7] y = dataset[:,8] 我们将在下面所有的例子里使用这个数据组,换言之 ...
- 通过一个例子了解MapReduce
写MapReduce程序的步骤: 把问题转化为MapReduce模型: 设置运行参数: 写map类: 写reduce类: 例子:统计单词个数 Map的任务是将内容用" "分开,然后 ...
- FFmpeg的H.264解码器源代码简单分析:宏块解码(Decode)部分-帧间宏块(Inter)
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码
原文出处: 平凡之路的博客 欢迎分享原创到伯乐头条 伯乐在线注:『阅读优秀代码是提高开发人员修为的一种捷径』http://t.cn/S4RGEz .之前@伯乐头条 曾发过一条微博:『C 语言进阶有 ...
- ROS_Kinetic_x 基於ROS和Gazebo的RoboCup中型組仿真系統(多機器人協作)
國防科學技術大學發布了RoboCup中型組仿真平臺,基於ROS和Gazebo設計. 該平臺可以用於多機器人協作研究.參考資料如下: ROS新聞:1 http://www.ros.org/news ...
- Android系统剪切板
Android系统剪贴板 复制字符串到系统剪切板 /** * 复制文本到剪切板 * * @param text * @return */ @SuppressWarnings("depreca ...