没有数据标准化的版本,效率非常低,而且训练结果并不好。
#include <iostream>
#define maxn 105
#include <cstdio>
#include <cmath>
using namespace std;
int n,m; //n个特征,m个数据
double theta[maxn];//参数集
double temp[maxn];
double data[maxn][maxn];//数据集
double Y[maxn];//结果集
double hx[maxn];
const double eps=1e-9;
double alpha=0.00001;
double h(int x)//计算假设函数
{
double res=0;
for(int i=0;i<=n;++i)
{
res+=theta*data[x];
}
return res;
}
double J_theta()//计算cost function
{
double res=0;
for(int i=1;i<=m;++i)
{
res+=(h(i)-Y)*(h(i)-Y);
}
res=res/(2*m);
return res;
}
double f(int x)//求偏导数
{
double res=0;
for(int i=1;i<=m;++i)
{
res+=hx*data[x];
}
res/=m;
return res;
}
void Gradient_Descent()//梯度下降
{
for(int i=1;i<=m;++i)
{
data[0]=1;
}
for(int i=0;i<=n;++i)
{
theta=1;//初始化
}
double now,nex;
do
{
now=J_theta();
for(int i=1;i<=m;++i)
{
hx=h(i)-Y;
}
for(int i=0;i<=n;++i)
{
temp=theta-alpha*f(i);
}
for(int i=0;i<=n;++i)
{
theta=temp;
}
nex=J_theta();
//cout<<J_theta()<<endl;
}while (abs(nex-now)>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[j];
}
}
for(int i=1;i<=m;++i)
{
cin>>Y;
}
Gradient_Descent();
for(int i=0;i<=n;++i)
{
printf("%.2lf\n",theta);
}
return 0;
}
下面是将数据归一化之后的版本,效率较高:
#include <iostream>
#define maxn 105
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
int n,m; //n个特征,m个数据
double theta[maxn];//参数集
double temp[maxn];
double data[maxn][maxn];//数据集
double Y[maxn];//结果集
double hx[maxn];
const double eps=1e-9;
double alpha=0.001;
double ave[maxn];
void Mean_Normaliazation()
{
for(int i=0;i<=n;++i)
{
double maxim=-1e9;
double minum=1e9;
double tmp=0;
for(int j=1;j<=m;++j)
{
tmp+=data[j];
}
tmp/=m;
double mb=0;
for (int j=1;j<=m;++j)
{
mb+=(data[j]-tmp)*(data[j]-tmp);
}
mb/=m;
mb=sqrt(mb);
for(int j=1;j<=m;++j)
{
data[j]=(data[j]-tmp)/mb;
}
}
double maxim=-1e9;
/*double tmp=0;
for(int i=1;i<=m;++i)
{
maxim=max(Y,maxim);
tmp+=Y;
}
tmp/=m;
for(int i=1;i<=m;++i)
{
Y=(Y-tmp)/maxim;
}*/
}
double h(int x)//计算假设函数
{
double res=0;
for(int i=0;i<=n;++i)
{
res+=theta*data[x];
}
return res;
}
double J_theta()//计算cost function
{
double res=0;
for(int i=1;i<=m;++i)
{
res+=(h(i)-Y)*(h(i)-Y);
}
res=res/(2*m);
return res;
}
double f(int x)//求偏导数
{
double res=0;
for(int i=1;i<=m;++i)
{
res+=hx*data[x];
}
res/=m;
return res;
}
void Gradient_Descent()//梯度下降
{
for(int i=1;i<=m;++i)
{
data[0]=1;
}
for(int i=0;i<=n;++i)
{
theta=1;//初始化
}
double now,nex;
do
{
now=J_theta();
for(int i=1;i<=m;++i)
{
hx=h(i)-Y;
}
for(int i=0;i<=n;++i)
{
temp=theta-alpha*f(i);
}
for(int i=0;i<=n;++i)
{
theta=temp;
}
nex=J_theta();
//cout<<J_theta()<<endl;
}while (abs(nex-now)>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[j];
}
}
for(int i=1;i<=m;++i)
{
cin>>Y;
}
Mean_Normaliazation();
Gradient_Descent();
for(int i=0;i<=n;++i)
{
printf("%.2lf\n",theta);
}
return 0;
}
训练数据在这里:
2 10
100 4
50 3
100 4
100 2
50 2
80 2
75 3
65 4
90 3
90 2
9.3 4.8 8.9 6.5 4.2 6.2 7.4 6.0 7.6 9.3 4.8 8.9 6.5
|