LWR 局部加权线性回归算法

http://blog.csdn.net/tianguokaka/article/details/14227187
分类: 统计学习方法   1649人阅读  评论(3)  收藏  举报
看了机器学习的第三课,实现了一下LWR算法。
[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. const int Number = 6;  
  5. const int Dimesion = 3;  
  6. const float learningRate=0.001;       
  7. const float errorThr=1; //variance threshold  
  8. const int MAX=1000;     //Max times of iteration  
  9.   
  10. typedef struct Data{  
  11.     float vectorComponent[Dimesion];  
  12. }vectorData;  
  13.   
  14. vectorData x[Number] = {  
  15.    /* {1,1,4}, 
  16.     {1,2,5}, 
  17.     {1,5,1}, 
  18.     {1,4,2},*/  
  19.     {1,1,1},  
  20.     {1,1,3},  
  21.     {1,1,2},  
  22.     {1,2,3},  
  23.     {1,2,1},  
  24.     {1,2,2},  
  25. };  
  26. float y[Number]={2,10,5,13,5,8};  
  27. /lwr(局部线性回归)  
  28. float weightValue(vectorData xi,vectorData x){  
  29.     float weight = 0.0;  
  30.     for(int i=0;i<Dimesion;i++){  
  31.         weight+=pow(xi.vectorComponent[i]-x.vectorComponent[i],2);  
  32.     }  
  33.     float tempWeight = exp(-(weight/(2*36)));  
  34.     if(tempWeight<0.02)  
  35.         tempWeight = 0.0;  
  36.     return tempWeight;  
  37. }  
  38.   
  39. float multiPly(vectorData x1,vectorData x2){  
  40.     float temp = 0.0;  
  41.     for(int i=0;i<Dimesion;i++){  
  42.         temp += x1.vectorComponent[i]*x2.vectorComponent[i];  
  43.     }  
  44.     return temp;  
  45. }  
  46.   
  47. vectorData addVectorData(vectorData x1,vectorData x2){  
  48.     vectorData temp;  
  49.     for(int i=0;i<Dimesion;i++)  
  50.         temp.vectorComponent[i] = x1.vectorComponent[i]+x2.vectorComponent[i];  
  51.     return temp;  
  52. }  
  53.   
  54. vectorData minusVectorData(vectorData x1,vectorData x2){  
  55.     vectorData temp;  
  56.     for(int i=0;i<Dimesion;i++)  
  57.         temp.vectorComponent[i] = x1.vectorComponent[i]-x2.vectorComponent[i];  
  58.     return temp;  
  59. }  
  60.   
  61. vectorData numberMultiVectorData(float para,vectorData x1){  
  62.     vectorData temp;  
  63.     for(int i=0;i<Dimesion;i++)  
  64.         temp.vectorComponent[i] = x1.vectorComponent[i]*para;  
  65.     return temp;  
  66. }  
  67. float costFunction(vectorData parameter[],vectorData inputData[],float inputResultData[],vectorData object){  
  68.     float costValue = 0.0;  
  69.     float tempValue = 0.0;  
  70.     float weightedValue = 0.0;  
  71.     for(int i=0;i<Number;i++){  
  72.         tempValue = 0.0;  
  73.           
  74.         //consider all the parameters although most of them is zero  
  75.         for(int j=0;j<Number;j++)  
  76.             tempValue += multiPly(parameter[j],inputData[i]);  
  77.         costValue += weightValue(inputData[i],object)*pow((inputResultData[i]-tempValue),2);      
  78.     }  
  79.   
  80.     return (costValue/2*4);  
  81. }  
  82.   
  83.   
  84. int LocallyWeightedAgression(vectorData parameter[],vectorData inputData[],float resultData[],vectorData objectVector){  
  85.     float tempValue = 0.0;  
  86.     float errorCost = 0.0;  
  87.     float weightedValue = 0.0;  
  88.     errorCost=costFunction(parameter,inputData,resultData,objectVector);  
  89.     if(errorCost<errorThr)  
  90.         return 1;  
  91.     for(int iteration=0;iteration<MAX;iteration++){  
  92.   
  93.         //stochastic  
  94.         for(int i=0;i<Number;i++){  
  95.             //calculate the h(x)  
  96.             weightedValue = weightValue(inputData[i],objectVector);  
  97.             tempValue=0.0;  
  98.             for(int j=0;j<Number;j++)  
  99.                 tempValue+=multiPly(parameter[j],inputData[i]);  
  100.             //update the parameter by stochastic(随机梯度下降)  
  101.             printf("the next parameter is ");  
  102.             for(int ii=0;ii<Number;ii++){  
  103.                 parameter[ii] = addVectorData(parameter[ii],numberMultiVectorData(weightedValue*learningRate*(resultData[i]-tempValue),inputData[i]));  
  104.                 if(multiPly(parameter[ii],parameter[ii])!=0){  
  105.                     for(int jj=0;jj<Dimesion;jj++){  
  106.                         printf("%f ",parameter[ii].vectorComponent[jj]);  
  107.                     }  
  108.                 }  
  109.             }  
  110.             printf("\n");  
  111.             errorCost=costFunction(parameter,inputData,resultData,objectVector);  
  112.             printf("error cost is %f\n",errorCost);  
  113.             if(errorCost<errorThr)  
  114.                 break;  
  115.         }//end stochastic one time  
  116.   
  117.     }//end when the iteration becomes MAX   
  118.   
  119.     //calculate the object vector  
  120.     float resultValue = 0.0;  
  121.     for(int i=0;i<Number;i++){  
  122.         resultValue += weightValue(inputData[i],objectVector)*multiPly(parameter[i],objectVector);  
  123.     }  
  124.     printf("result value is %f \n",resultValue);  
  125.     return 1;  
  126. }  
  127.   
  128. int testLWA(){  
  129.     vectorData objectData = {1,1.5,1.5};  
  130.     vectorData localParameter[Number] = {0.0};  
  131.     LocallyWeightedAgression(localParameter,x,y,objectData);  
  132.     return 1;  
  133. }  
  134. int main(){  
  135.   //  DescendAlgorithm(parameter,x,y);  
  136. //  system("pause");  
  137.     //clearParameter(parameter);  
  138.     //Stochastic(parameter,x,y);  
  139.     //float ForTestData[] = {1,10,20};  
  140.     //testData(ForTestData);  
  141.     testLWA();  
  142.     system("pause");  
  143.     return 1;  
  144. }  

设定的目标函数是平面方程 z=x^2+y^2 
实验数据是1.5,1.5 
计算结果是 5.124
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值