-
大小: 5KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-05-26
- 語(yǔ)言: C/C++
- 標(biāo)簽:
資源簡(jiǎn)介
機(jī)器學(xué)習(xí),非參數(shù)學(xué)習(xí)算法,局部加權(quán)線性回歸,c++實(shí)現(xiàn)
代碼片段和文件信息
#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);?
評(píng)論
共有 條評(píng)論