C++实现的简单k近邻算法(K-Nearest-Neighbour,K-NN)
C++实现的简单的K近邻算法(K-Nearest Neighbor,K-NN)
K近邻算法中的分类决策规则往往是多数表决,即由输入示例的K个邻近的训练实例中的多数类决定输入实例的类。
</#include<map>
#include<vector>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<fstream>using namespace std;typedef char tLabel;
typedef double tData;
typedef pair<int, double> PAIR;
const int colLen = 2;
const int rowLen = 12;
ifstream fin;
ofstream fout;class KNN
{
private:tData dataSet[rowLen][colLen];tLabel labels[rowLen];tData testData[colLen];int k;map<int, double> map_index_dis;map<tLabel, int> map_label_freq;double get_distance(tData *d1, tData *d2);
public:KNN(int k);void get_all_distance();void get_max_freq_label();struct CmpByValue{bool operator() (const PAIR& lhs, const PAIR& rhs){return lhs.second < rhs.second;}};};KNN::KNN(int k)
{this->k = k;fin.open("C:\\Users\\zws\\Desktop\\K近邻\\data.txt");if (!fin){cout << "can not open the file data.txt" << endl;exit(1);}/* input the dataSet */for (int i = 0; i<rowLen; i++){for (int j = 0; j<colLen; j++){fin >> dataSet[i][j];}fin >> labels[i];}cout << "please input the test data :" << endl;/* inuput the test data */for (int i = 0; i<colLen; i++)cin >> testData[i];}/*
* calculate the distance between test data and dataSet[i]
*/
double KNN::get_distance(tData *d1, tData *d2)
{double sum = 0;for (int i = 0; i<colLen; i++){sum += pow((d1[i] - d2[i]), 2);}// cout<<"the sum is = "<<sum<<endl;return sqrt(sum);
}/*
* calculate all the distance between test data and each training data
*/
void KNN::get_all_distance()
{double distance;int i;for (i = 0; i<rowLen; i++){distance = get_distance(dataSet[i], testData);//<key,value> => <i,distance>map_index_dis[i] = distance;}//traverse the map to print the index and distancemap<int, double>::const_iterator it = map_index_dis.begin();while (it != map_index_dis.end()){cout << "index = " << it->first << " distance = " << it->second << endl;it++;}
}/*
* check which label the test data belongs to to classify the test data
*/
void KNN::get_max_freq_label()
{//transform the map_index_dis to vec_index_disvector<PAIR> vec_index_dis(map_index_dis.begin(), map_index_dis.end());//sort the vec_index_dis by distance from low to high to get the nearest datasort(vec_index_dis.begin(), vec_index_dis.end(), CmpByValue());for (int i = 0; i<k; i++){cout << "the index = " << vec_index_dis[i].first << " the distance = " << vec_index_dis[i].second << " the label = " << labels[vec_index_dis[i].first] << " the coordinate ( " << dataSet[vec_index_dis[i].first][0] << "," << dataSet[vec_index_dis[i].first][1] << " )" << endl;//calculate the count of each labelmap_label_freq[labels[vec_index_dis[i].first]]++;}map<tLabel, int>::const_iterator map_it = map_label_freq.begin();tLabel label;int max_freq = 0;//find the most frequent labelwhile (map_it != map_label_freq.end()){if (map_it->second > max_freq){max_freq = map_it->second;label = map_it->first;}map_it++;}cout << "The test data belongs to the " << label << " label" << endl;
}int main()
{int k;cout << "please input the k value : " << endl;cin >> k;KNN knn(k);knn.get_all_distance();knn.get_max_freq_label();system("pause");return 0;
}
测试结果:
K=5时,待分类点为(5.0 ,5.0):
C++实现的简单k近邻算法(K-Nearest-Neighbour,K-NN)相关推荐
- python k近邻算法_python中的k最近邻居算法示例
python k近邻算法 K最近邻居(KNN) (K-Nearest Neighbors (KNN)) KNN is a supervised machine learning algorithm t ...
- k近邻算法_图穷匕见:K近邻算法与手写数字识别
机器学习算法是从数据中产生模型,也就是进行学习的算法.我们把经验提供给算法,它就能够根据经验数据产生模型.在面对新的情况时,模型就会为我们提供判断(预测)结果.例如,我们根据"个子高.腿长. ...
- k近邻算法_机器学习之K近邻分类算法的实现
K近邻算法(k-nearest neighbors, KNN)是最基本的机器学习算法之一.所谓的K,就是距离最近的K个邻居的意思.其实,KNN在我们平常的生活中也会不自主的应用,比如,俗语说的&quo ...
- 通过交叉验证寻找K近邻算法的最优K值
问题引出 之前我们使用K近邻算法尝试寻找用户年龄与预估薪资之间的某种相关性,以及他们是否有购买SUV的决定.主要代码如下: from sklearn.neighbors import KNeighbo ...
- SK-learn实现k近邻算法【准确率随k值的变化】-------莺尾花种类预测
代码详解: from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split fr ...
- 机器学习入门笔记(三):K近邻算法
文章目录 一.K近邻算法的基本概念 1.1 K近邻算法实现 二.K近邻分类三要素 2.1 距离度量 2.2 K值的选择 2.2.1 基于m-fold cross validation的 K值选择 2. ...
- 【机器学习】原理与实现k近邻算法
文章目录 系列文章目录 前言 一.k近邻算法是什么? 二.使用步骤 1.引入库 2.读入数据 总结 前言 随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了 ...
- k近邻算法(k-Nearest Neighbors,kNN)
k近邻算法 (k-Nearest Neighbors,kNN) 文章目录 k近邻算法 (k-Nearest Neighbors,kNN) 算法原理与思想 算法原理 手工实现 scikit−learns ...
- 机器学习第七章之K近邻算法
K近邻算法(了解) 7.1 K近邻算法 7.1.1 K近邻算法的原理介绍 7.1.2 K近邻算法的计算步骤及代码实现 7.2 数据预处理之数据归一化 7.2.1 min-max标准化 7.2.2 Z- ...
最新文章
- 你还在 select * 吗?
- 谈谈我的移动端rem适配方案
- nginx tcp代理_nginx——TCP/UDP Load Balancing
- Mysql字符串截取 mysql将字符串字段转为数字排序或比大小
- CentOS安装yum 镜像 举例阿里云镜像
- Mac下搭建Cocos2d-x-3.2的开发环境详解
- NiFi-面向流程的大数据处理框架
- HttpClient 忽略证书直接访问https站点
- rdlc报表的制作步骤
- 物联网专用卡的优势有哪些
- Qt 使用vs调试的方法
- python输入输出基本点
- 几种开源许可协议(转载)
- Atitit. js mvc 总结(2)----angular 跟 Knockout o99 最佳实践
- 数值计算之第二期:误差分析
- APP测试和web测试有什么区别?
- uni-app获取屏幕高度和宽度
- 中国目前拥有的物种和人造卫星的作用
- JAVA 获取今天、昨天、上周等日期
- SAP中利用标准成本报表计算成品人工成本及组成实例