KNN简单实现

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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值