逻辑回归的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/water_93/article/details/50859292
逻辑回归:0-1分类
通过学习求出参数theta,从而求出sigmoid函数

1.求theta[j]*x[i][j]
2.根据sigmoid函数求出h
3.求出theta[j]= theta[j]+0.01*(y[i]-h)*x[i][j]
4.求出假设函数(拟合直线)

代码如下:
#include"math.h"
#include "stdio.h"

#define M 110
#define N 3
double sigmoidFunction(double x)
{
      double h;
      h=1/(1+exp(-x));
      return h;
}

int main()
{
      double x[M][N];
      int i,j,k;
      double theta[3]={0.0,0.0,0.0};
      double h=0.0;
      FILE *fp;
      fp=fopen("ex2data1.txt","r");
      if(fp==NULL)//打开文件成功返回文件指针fp,失败返回NULL
      {
            return -1;
      }
      for(i=0;i
     {
            fscanf(fp,"%lf,%lf,%lf",&x[i][0],&x[i][1],&x[i][2]);
            fscanf(fp,"\n");
     }
     fclose(fp);
     printf("%lf",x[1][2]);
     for(k=0;k<1000;k++)
     {
           for(i=0;i
           {
                 for(j=0;j<3;j++)
                {
                      h=theta[j]*x[i][j];
                }
                h= sigmoidFunction(h);
                for(j=0;j<3;j++)
                {
                       theta[j]+=0.01*(x[i][j]-h)*x[i][j];
                }

           }
      }

}

猜你喜欢

转载自blog.csdn.net/water_93/article/details/50859292
今日推荐