資源簡介
機器學習,非參數學習算法,局部加權線性回歸,c++實現
代碼片段和文件信息
#include???
#include
using?namespace?std;??
??
const?int?Number?=?6;??
const?int?Dimesion?=?3;??
const?float?learningRate=0.001;???????
const?float?errorThr=1;?//variance?threshold???
const?int?MAX=1000;?????//Max?times?of?iteration???
??
typedef?struct?Data{??
????float?vectorComponent[Dimesion];??
}vectorData;??
??
vectorData?x[Number]?=?{??
???/*?{114}?
????{125}?
????{151}?
????{142}*/??
????{111}??
????{113}??
????{112}??
????{123}??
????{121}??
????{122}??
};??
float?y[Number]={21051358};??
//lwr(局部線性回歸)??
float?weightValue(vectorData?xivectorData?x){??
????float?weight?=?0.0;??
????for(int?i=0;i ????????weight+=pow(xi.vectorComponent[i]-x.vectorComponent[i]2);??
????}??
????float?tempWeight?=?exp(-(weight/(2*36)));??
????if(tempWeight<0.02)??
????????tempWeight?=?0.0;??
????return?tempWeight;??
}??
??
float?multiPly(vectorData?x1vectorData?x2){??
????float?temp?=?0.0;??
???for(int?i=0;i ????????temp?+=?x1.vectorComponent[i]*x2.vectorComponent[i];??
????}??
????return?temp;??
}??
??
vectorData?addVectorData(vectorData?x1vectorData?x2){??
????vectorData?temp;??
????for(int?i=0;i ????????temp.vectorComponent[i]?=?x1.vectorComponent[i]+x2.vectorComponent[i];??
????return?temp;??
}??
??
vectorData?minusVectorData(vectorData?x1vectorData?x2){??
????vectorData?temp;??
????for(int?i=0;i ????????temp.vectorComponent[i]?=?x1.vectorComponent[i]-x2.vectorComponent[i];??
????return?temp;??
}??
??
vectorData?numberMultiVectorData(float?paravectorData?x1){??
????vectorData?temp;??
????for(int?i=0;i ????????temp.vectorComponent[i]?=?x1.vectorComponent[i]*para;??
????return?temp;??
}??
float?costFunction(vectorData?parameter[]vectorData?inputData[]float?inputResultData[]vectorData?object){??
????float?costValue?=?0.0;??
????float?tempValue?=?0.0;??
????float?weightedValue?=?0.0;??
????for(int?i=0;i ????????tempValue?=?0.0;??
??????????
????????//consider?all?the?parameters?although?most?of?them?is?zero???
????????for(int?j=0;j ????????????tempValue?+=?multiPly(parameter[j]inputData[i]);??
????????costValue?+=?weightValue(inputData[i]object)*pow((inputResultData[i]-tempValue)2);??????
????}??
??
????return?(costValue/2*4);?
- 上一篇:樸素貝葉斯算法 c++
- 下一篇:C++大作業_學生管理系統(含源代碼實驗報告)
評論
共有 條評論