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. Mac入门

    Mac入门 桌面 windows桌面有图标罗列 Mac桌面有Dock 菜单栏 感觉上和Windows系统的底部菜单栏有点像,但是却略有不同,Mac的菜单栏默认在顶部 左侧的一些功能是固定不变的,跟随当 ...

  2. Java多线程模型

    谈到Java多线程就涉及到多线程的模型及Java线程与底层操作系统之间的关系.正如我们熟知,现代机器可以分为硬件和软件两大块,如图2-5-1-1,硬件是基础,软件提供实现不同功能的手段.而且软件可以分 ...

  3. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

  4. 查看某一职责下对应的菜单&功能&请求

    查看菜单&功能 SELECT res.RESPONSIBILITY_NAME 职责名称, menu.MENU_NAME 菜单编码, menu.USER_MENU_NAME 菜单名称, func ...

  5. 由源代码编译SpriteBuilder最新版本1.5.0搭配最新的Cocos2D 3.4.9

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道SpriteBuilder版本停留在1.4.9已经很久 ...

  6. 剑指Offer——全排列递归思路

    剑指Offer--全排列递归思路 前言 全排列,full permutation, 可以利用二叉树的遍历实现.二叉树的递归遍历,前中后都简洁的难以置信,但是都有一个共同特点,那就是一个函数里包含两次自 ...

  7. parcel和parcelable

    Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思: 其二为动词,意为打包,扎包.邮寄快递中的包裹也用的是这个词.Android采用这个词来表示封装消息数据.这个是通过IBinder通信 ...

  8. oracle对大对象类型操作:blob,clob,nclob

     1.基本介绍 Oracle和plsql都支持lob(large object) 类型,用来存储大数量数据,如图像文件,声音文件等.Oracle 9i realse2支持存储最大为4g的数据,or ...

  9. Request中Attribute 和 Parameter 的区别

    Attribute 和 Parameter 的区别 (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为 ...

  10. 学习笔记6-Android查看应用输出的错误信息 如何部署应用到真实手机 发布软件

    查看应用输出的错误信息 1.      通过LogCat窗口查看信息 右上角图标可以筛选不同级别的信息(比如info等). 右上角的+可以进行信息筛选 把应用部署到真实手机 1.      要把手机的 ...