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

 

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

原文地址:https://www.cnblogs.com/cl1024cl/p/6205279.html