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]。

  1. #include "stdio.h"
  2. #include "math.h"
  3. double matrix[6][4]={{1,47,76,24}, //include x0=1
  4. {1,46,77,23},
  5. {1,48,74,22},
  6. {1,34,76,21},
  7. {1,35,75,24},
  8. {1,34,77,25},
  9. };
  10. double result[]={1,1,1,0,0,0};
  11. double theta[]={1,1,1,1}; // include theta0
  12. double function_g(double x)
  13. {
  14. double ex = pow(2.718281828,x);
  15. return ex/(1+ex);
  16. }
  17. int main(void)
  18. {
  19. double likelyhood = 0.0;
  20. float sum=0.0;
  21. for(int j = 0;j<6;++j)
  22. {
  23. double xi = 0.0;
  24. for(int k=0;k<4;++k)
  25. {
  26. xi += matrix[j][k]*theta[k];
  27. }
  28. printf("sample %d,%f\n",j,function_g(xi));
  29. sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;
  30. }
  31. printf("%f\n",sum);
  32. for(int i =0 ;i<1000;++i)
  33. {
  34. double error_sum=0.0;
  35. int j=i%6;
  36. {
  37. double h = 0.0;
  38. for(int k=0;k<4;++k)
  39. {
  40. h += matrix[j][k]*theta[k];
  41. }
  42. error_sum = result[j]-function_g(h);
  43. for(int k=0;k<4;++k)
  44. {
  45. theta[k] = theta[k]+0.001*(error_sum)*matrix[j][k];
  46. }
  47. }
  48. printf("theta now:%f,%f,%f,%f\n",theta[0],theta[1],theta[2],theta[3]);
  49. float sum=0.0;
  50. for(int j = 0;j<6;++j)
  51. {
  52. double xi = 0.0;
  53. for(int k=0;k<4;++k)
  54. {
  55. xi += matrix[j][k]*theta[k];
  56. }
  57. printf("sample output now: %d,%f\n",j,function_g(xi));
  58. sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;
  59. }
  60. printf("maximize the log likelihood now:%f\n",sum);
  61. printf("************************************\n");
  62. }
  63. return 0;
  64. }

Logistic Regression求解classification问题的更多相关文章

  1. Logistic Regression and Classification

    分类(Classification)与回归都属于监督学习,两者的唯一区别在于,前者要预测的输出变量\(y\)只能取离散值,而后者的输出变量是连续的.这些离散的输出变量在分类问题中通常称之为标签(Lab ...

  2. 使用sklearn和caffe进行逻辑回归 | Brewing Logistic Regression then Going Deeper

    原文首发于个人博客https://kezunlin.me/post/c50b0018/,欢迎阅读! Brewing Logistic Regression then Going Deeper. Bre ...

  3. 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 ...

  4. 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 ...

  5. 李宏毅机器学习笔记3:Classification、Logistic Regression

    李宏毅老师的机器学习课程和吴恩达老师的机器学习课程都是都是ML和DL非常好的入门资料,在YouTube.网易云课堂.B站都能观看到相应的课程视频,接下来这一系列的博客我都将记录老师上课的笔记以及自己对 ...

  6. 机器学习理论基础学习3.3--- Linear classification 线性分类之logistic regression(基于经验风险最小化)

    一.逻辑回归是什么? 1.逻辑回归 逻辑回归假设数据服从伯努利分布,通过极大化似然函数的方法,运用梯度下降来求解参数,来达到将数据二分类的目的. logistic回归也称为逻辑回归,与线性回归这样输出 ...

  7. Logistic Regression Using Gradient Descent -- Binary Classification 代码实现

    1. 原理 Cost function Theta 2. Python # -*- coding:utf8 -*- import numpy as np import matplotlib.pyplo ...

  8. Classification week2: logistic regression classifier 笔记

    华盛顿大学 machine learning: Classification 笔记. linear classifier 线性分类器 多项式: Logistic regression & 概率 ...

  9. Classification and logistic regression

    logistic 回归 1.问题: 在上面讨论回归问题时.讨论的结果都是连续类型.但假设要求做分类呢?即讨论结果为离散型的值. 2.解答: 假设: 当中: g(z)的图形例如以下: 由此可知:当hθ( ...

随机推荐

  1. Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果

    Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果 分享下我项目中用到的几种Button的效果,说实话,还真挺好看的 一.标准圆角 效果是这样的 他的实现很简单,我们只需要两个 ...

  2. [shiro学习笔记]第四节 使用源代码生成Shiro的CHM格式的API文档

    版本为1.2.3的shiro API chm个事故文档生成. 获取shiro源代码 编译生成API文档 转换成chm格式 API 获取shiro源代码 shiro官网: http://shiro.ap ...

  3. 如何使用Matlab产生对称矩阵

    有时候做实验需要使用对称矩阵,这里介绍如何使用Matlab产生随机的对称矩阵. 用例子说明一下:我要产生4X4的随机矩阵,要求是对称矩阵. 产生对称矩阵 A = rand(4); B = tril(A ...

  4. 集合框架之Map接口

    Map是将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. Map 接口提供三种collection视图,允许以键集.值集或键-值映射关系集的形式查看某个映射的内容.映射顺序定 ...

  5. Linux之read命令使用

    read命令: read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 1)read后面的变量var可以只有一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个 ...

  6. eclipse无法连接genymotion+Unable to start the Genymotion virtual device

    八月的开头,带着希望和期待,小编继续着实习之路,闭眼呼吸,阳光勾勒微笑,做Android项目,真心想吐槽一下eclipse中的虚拟机,那速度真叫一个慢啊,她肯定是属乌龟的,要不就是蜗牛,这个让小编很是 ...

  7. Java虚拟机定义

    虚拟机是一种抽象化的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机有自己完善的硬体架构,如处理器.堆栈.寄存器等,还具有相应的指令系统.JVM屏蔽了与具体操作系统平台相关的 ...

  8. UNIX网络编程——使用select 实现套接字I/O超时

    下面程序包含read_timeout.write_timeout.accept_timeout.connect_timeout 四个函数封装: /* read_timeout - 读超时检测函数,不含 ...

  9. Java-IO之CharArrayReader

    CharArrayReader是字符数组输入流,CharArrayReader用于读取字符数组,继承于Reader操作的数据是以字符为单位. (1)CharArrayReader实际上是通过字符数组去 ...

  10. linux命令指usermod(管理用户以及权限的命令)

    usermod命令:用来修改用户帐号的各项设定. 示例:usermod -a -G usergroupnewuser 或者usermod -aGusergroup newuser 语法:usermod ...