No data standardized version, the efficiency is very high, and the training results are not good.
#include <the iostream> #define MAXN 105 #include <cstdio> the using namespace STD; int n-, m; // n-th feature, m data double theta [maxn]; // parameter set Double TEMP [MAXN]; Double Data [maxn] [maxn]; // data set double Y [maxn]; // result set Double HX [MAXN]; const Double EPS = 0.37; Double Alpha = 0.00001; Double H (int X) // calculations assume functions { RES = 0 Double; for (int I = 0; I <= n-; I ++) { RES Theta + = [I] * Data [X] [I]; } return RES; } Double J_theta () // calculate the cost of function { Double RES = 0; for (int I =. 1; I <= m; I ++) { res+=(h(i)-Y[i])*(h(i)-Y[i]); } res=res/(2*m); return res; } double f(int x)//求偏导数 { double res=0; for(int i=1;i<=m;++i) { res+=hx[i]*data[i][x]; } res/=m; return res; } void Gradient_Descent()//梯度下降 { for(int i=1;i<=m;++i) { data[i][0]=1; } for(int i=0;i<=n;++i) { theta[i]=1;//初始化 } double now,nex; do { now=J_theta(); for(int i=1;i<=m;++i) { hx[i]=h(i)-Y[i]; } for(int i=0;i<=n;++i) { temp[i]=theta[i]-alpha*f(i); } for(int i=0;i<=n;++i) { theta[i]=temp[i]; } nex=J_theta(); //cout<<J_theta()<<endl; }while (J_theta()>eps); } int main() { freopen("in.txt","r",stdin); cin>>n>>m; for(int i=1;i<=m;++i) { for(int j=1;j<=n;++j) { cin>>data[i][j]; } } for(int i=1;i<=m;++i) { cin>>Y[i]; } Gradient_Descent(); for(int i=0;i<=n;++i) { printf("%.2lf\n",theta[i]); } return 0; }