您的位置:首页 > 其它

batch gradient method

2014-12-19 22:17 323 查看
#include #include #define M 100#define N 2#define Max_it 1500int main(void){ int i, j, count; double x[M], y[M], theta
, alpha; FILE *ifp; void stochastic(double *x, double *y, int m, double *theta, int n, double alpha, int tt); //read data from file count
= 0; if((ifp = fopen("ex1data1.txt", "r")) == NULL) { printf("open error, check!\n"); exit(1); } else { for(i=0;!feof(ifp);i++) { fscanf(ifp,"%lf,%lf", &x[i], &y[i]); count++; } } fclose(ifp); --count; for(j=0; j<N; j++) theta[j] = 0.0; alpha = 0.02; stochastic(x,
y, count,theta, N, alpha, Max_it); printf("The regression parameters are:\n"); for(i=0; i<N; i++) { printf("%10.2f \n", theta[i]); } exit(0);}//============================================================================================void stochastic(double
*x, double *y, int m, double *theta, int n, double alpha, int max_it){ int i, j, k, l; double sum1; for(k=0; k<max_it; k++)//for max iteration numbers { for(i=0; i<m; i++) { for(j=0; j<n; j++) { sum1 = 0.0; sum1 = (theta[0]+theta[1]*x[i]); if(j == 0) theta[j]
= theta[j] + alpha*(y[i] - sum1)/m; else theta[j] = theta[j] + alpha*(y[i] - sum1)*x[i]/m; } } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  batch
相关文章推荐