资源简介
机器学习,非参数学习算法,局部加权线性回归,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++大作业_学生管理系统(含源代码实验报告)
评论
共有 条评论