KNN(K nearest neighbor Algorithm)
原理:取距离上训练数据上最近的K个点的均值作为估计值。
简单实现:
//A general realization of K nearest neighbors Algorithm
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct ob
{
double dis,val;
};
bool cmp(ob a,ob b)
{
return a.dis<b.dis;
}
int main()
{
int K,n,x;
cout<<"Please put in the value for parameter K:";
cin>>K;
cout<<"\nPlease put in the value for attribute amount x:";
cin>>x;
cout<<"\nPlease put in the value for data amount n:";
cin>>n;
cout<<"Please put in the data in the form of x1,...,xi,...xn,value repeatly\n";
vector <vector<double> > data;
for(int i=0;i<n;i++)
{
vector <double> v;
double tmp;
for(int j=0;j<=x;j++)
{
cin>>tmp;
v.push_back(tmp);
}
data.push_back(v);
}
cout<<"Please put in the data to be predicted in the form of x1,...,xi,...xn\n";
while(1)
{
vector <double> v;
double tmp,pred_val=0;
for(int i=0;i<x;i++)
{
cin>>tmp;
v.push_back(tmp);
}
vector <ob> q;
ob apiece;
for(int i=0;i<n;i++)
{
apiece.dis=0;
for(int j=0;j<x;j++)
{
apiece.dis+=(v[j]-data[i][j])*(v[j]-data[i][j]);
}
apiece.dis=sqrt(apiece.dis);
apiece.val=data[i][x];
q.push_back(apiece);
}
sort(q.begin(),q.end(),cmp);
for(int i=0;i<K;i++)
{
pred_val+=q[i].val;
}
pred_val/=K;
cout<<"The predicted value is :"<<pred_val<<endl;
}
return 0;
}