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θ( ...
随机推荐
- 六星经典CSAPP-笔记(11)网络编程
六星经典CSAPP-笔记(11)网络编程 参照<深入理解计算机系统>简单学习了下Unix/Linux的网络编程基础知识,进一步深入学习Linux网络编程和TCP/IP协议还得参考Steve ...
- Programming In Scala笔记-第七章、Scala中的控制结构
所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...
- SQLite 创建数据库(http://www.w3cschool.cc/sqlite/sqlite-create-database.html)
SQLite 创建数据库 SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库.您不需要任何特殊的权限即可创建一个数据. 语法 sqlite3 命令的基本语法如下: $sqlit ...
- emysql add_poop() 超时出错
emysql add_poop() 超时出错(金庆的专栏)sample/a_hello.erl 连接本机更改为连接局域网内的MySql服务器: emysql:add_pool(hello_poo ...
- 6.2、Android Studio内存
Android Monitor提供了一个Memory Monitor,所以你可以非常容易的监测应用性能和内存使用,可以发现无用的对象,本地内存泄漏和连接设备的内存使用.Memory Monitor显示 ...
- (SQL Server)有关T-SQL的10个好习惯
转自 http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html 1.在生产环境中不要出现Select * 这一点我想大家已经是比较 ...
- (一一四)使用FMDB操作SQLite数据库
上节介绍了用系统自带的C语言库操作SQLite的方法,比较繁琐,使用FMDB会大幅度简化,并且是面向对象的,使用十分方便. 使用步骤如下: 先从github下载FMDB框架,然后把它导入工程. ①导入 ...
- UNIX网络编程——非阻塞connect: Web客户程序
非阻塞的connect的实现例子出自Netscape的Web客户程序.客户先建立一个与某个Web服务器的HTTP连接,再获取一个主页.该主页往往含有多个对于其他网页的引用.客户可以使用非阻塞conne ...
- 【Android 应用开发】 Android APK 反编译 混淆 反编译后重编译
反编译工具 : 总结了一下 linux, windows, mac 上的版本, 一起放到 CSDN 上下载; -- CSDN 下载地址 : http://download.csdn.net/detai ...
- C#弹出对话框
一.基于WINFORM下的选择对话框 在WINFORM下,我们可以利用系统的对话框(MessageBox)来实现,具体思路是读取MessageBox的返回值(YES或NO)来达到对操作的控制.下面是一 ...