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. 从0到1:制作你的苹果podcast(播客)

    注意:本文不是教你如何录音.如何做后期的文章.而是聚焦于如何搭建播客(podcast)需要的环境. 本文科普类文章,干货少,湿货多. 先选一个主机吧 这步的初衷和你自己建站是一样的.你可以购买一个独立 ...

  2. Java程序员的Golang入门指南(下)

    Java程序员的Golang入门指南(下) 4.高级特性 上面介绍的只是Golang的基本语法和特性,尽管像控制语句的条件不用圆括号.函数多返回值.switch-case默认break.函数闭包.集合 ...

  3. RxJava(11-线程调度Scheduler)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51821940 本文出自:[openXu的博客] 目录: 使用示例 subscribeOn原理 ...

  4. post插件

    分享牛系列,分享牛专栏,分享牛.在项目开发中,http请求方式是最常见的了.怎么模拟http请求呢?方法有很多种,可以使用httpclient直接模拟请求,也可以使用火狐post插件方式,这个章节主要 ...

  5. Leetcode解题-链表(2.2.3)PartitionList

    题目:2.2.3 Partition List Given a linked list and a value x, partition it such that all nodes less tha ...

  6. iOS9中如何注册远程通知

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在以往的版本中,我们可以通过: [[UIApplication ...

  7. testng的使用

    TestNG教程 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG设计涵盖所有类型的测试:单元,功能,端到端,集成等, ...

  8. Android中Socket通信之TCP与UDP传输原理

    一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是"请求-响应方式",即在请求时 ...

  9. 2.5、Android Studio添加多适配的向量图片

    Android Studio包含一个Vector Asset Studio的工具,可以帮助你添加Material图标和导入SVG(Scalable Vector Graphic)文件到你的项目中作为向 ...

  10. Spark技术内幕:Master的故障恢复

    Spark技术内幕:Master基于ZooKeeper的High Availability(HA)源码实现  详细阐述了使用ZK实现的Master的HA,那么Master是如何快速故障恢复的呢? 处于 ...