K-means (PRML) in C++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <limits>
template <class T>
void ReadDataFromFile(const std::string &filename, std::vector<std::vector<T> > &vv_data) {
std::ifstream vm_info(filename.c_str());
T x, y;
std::vector<T> v_data;
while(!vm_info.eof()) {
v_data.clear();
vm_info >> x >> y;
v_data.push_back(x);
v_data.push_back(y);
vv_data.push_back(v_data);
}
vm_info.close();
}
template <class T>
void Display2DVector(std::vector<std::vector<T> > &vv) {
for(size_t i=0;i<vv.size();++i) {
for(typename::std::vector<T>::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) {
std::cout<<*it<<" ";
}
std::cout<<"\n";
}
std::cout<<"--------the total of the 2DVector is "<<vv.size()<<std::endl;
}
template <class T>
void AddIndicator(std::vector<std::vector<T> > &vv, const int &k) {
for(size_t i=0; i<vv.size(); ++i) {
for(size_t j=0; j<k; ++j) {
vv.at(i).push_back(0);
}
}
}
template <class T1, class T2>
void UpdateIndicator(std::vector<std::vector<T1> > &vv, const std::vector<T2> &u, const int &k) {
for(size_t i=0; i<vv.size(); ++i) {
double dis=std::numeric_limits<double>::max(), dis_min, cluster;
for(size_t j=0; j<k; ++j) {
dis_min=pow(vv.at(i).at(0)-u.at(j*2), 2.0)+pow(vv.at(i).at(1)-u.at(j*2+1), 2.0);
if(dis_min < dis) {
dis=dis_min;
cluster=j;
}
}
vv.at(i).at(cluster+2)=1;
}
}
template <class T1, class T2>
void UpdateMeans(const std::vector<std::vector<T1> > &vv, std::vector<T2> &u, const int &k) {
std::vector<T2> sum_set(u.size(), 0);
for(size_t i=0; i<k; ++i) {
double sum_indi=0.0;
for(size_t j=0; j<vv.size(); ++j) {
sum_indi+=vv.at(j).at(i+2);
sum_set.at(i*2)+=vv.at(j).at(i+2)*vv.at(j).at(0);
sum_set.at(i*2+1)+=vv.at(j).at(i+2)*vv.at(j).at(1);
}
sum_set.at(i*2)/=sum_indi;
sum_set.at(i*2+1)/=sum_indi;
}
u=sum_set;
}
template <class T1, class T2>
double DistortionMeasure(const std::vector<std::vector<T1> > &vv, const std::vector<T2> &u, const int &k) {
double cost=0.0;
for(size_t i=0; i<vv.size(); ++i) {
for(size_t j=0; j<k; ++j) {
cost+=vv.at(i).at(j+2)*(pow(vv.at(i).at(0)-u.at(j*2), 2.0)+pow(vv.at(i).at(1)-u.at(j*2+1), 2.0));
}
}
return cost;
}
int main() {
int k=4;
double mean[]={39, 42, 70, 2, 230, 10, 190, 85};
std::vector<double> u(mean, mean+sizeof(mean)/sizeof(mean[0]));
std::string oridata="kmeans.dat";
std::vector<std::vector<double> > vv_data;
ReadDataFromFile(oridata, vv_data);
AddIndicator(vv_data, k);
std::cout<<"the original mean: \n";
for(std::vector<double>::const_iterator it=u.begin(); it!=u.end(); ++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
double cost_old=std::numeric_limits<double>::max();
while(true) {
double cost_new=DistortionMeasure(vv_data, u, k);
if(std::abs(cost_new-cost_old)<0.0000001)
break;
UpdateIndicator(vv_data, u, k);
UpdateMeans(vv_data, u, k);
cost_old=cost_new;
}
std::cout<<"the new mean: \n";
for(std::vector<double>::const_iterator it=u.begin(); it!=u.end(); ++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
return 0;
}
The two phases of re-assigning data points to clusters and re-computing the cluster means are repeated in turn until there is no further change in the assignments(or until some maximum number of iterations is exceeded).
K-means (PRML) in C++的更多相关文章
- KNN 与 K - Means 算法比较
KNN K-Means 1.分类算法 聚类算法 2.监督学习 非监督学习 3.数据类型:喂给它的数据集是带label的数据,已经是完全正确的数据 喂给它的数据集是无label的数据,是杂乱无章的,经过 ...
- 软件——机器学习与Python,聚类,K——means
K-means是一种聚类算法: 这里运用k-means进行31个城市的分类 城市的数据保存在city.txt文件中,内容如下: BJ,2959.19,730.79,749.41,513.34,467. ...
- 快速查找无序数组中的第K大数?
1.题目分析: 查找无序数组中的第K大数,直观感觉便是先排好序再找到下标为K-1的元素,时间复杂度O(NlgN).在此,我们想探索是否存在时间复杂度 < O(NlgN),而且近似等于O(N)的高 ...
- 网络费用流-最小k路径覆盖
多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- numpy.ones_like(a, dtype=None, order='K', subok=True)返回和原矩阵一样形状的1矩阵
Return an array of ones with the same shape and type as a given array. Parameters: a : array_like Th ...
- 当我们在谈论kmeans(2)
本稿为初稿,后续可能还会修改:如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/ 其他:建设中- 当我们在谈论kmeans(2 ...
- scikit-learn包的学习资料
http://scikit-learn.org/stable/modules/clustering.html#k-means http://my.oschina.net/u/175377/blog/8 ...
- HDU 3584 Cube (三维 树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3584 Cube Problem Description Given an N*N*N cube A, ...
- Torch7学习笔记(二)nn Package
神经网络Package [目前还属于草稿版,等我整个学习玩以后会重新整理] 模块Module module定义了训练神经网络需要的所有基础方法,并且是可以序列化的抽象类. module有两种状态变量: ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- 【技术累积】【点】【java】【22】UUID
基础概念&使用 UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符. 说白了就是个唯一键,只不过到处 ...
- crontab错误处理
crontab任务跑着跑着突然停了,莫名奇妙,查看日志,发现以下错误: 网上搜了一下报错,提示说是调整打开最大进程数,设置如下
- eclipse的任务列表
如上图所示,备注加 TODO ,可以在tasks列表中显示,提示你还有哪些工作需要完善 昨天遇到一个问题,加了 TODO 任务列表里却不显示,后来发现是因为任务列表只显示了前100条,而我的项 ...
- illumina测序原理
一些常用基本概念的介绍: flowcell流动池 是指Illumina测序时,测序反应发生的位置,1个flowcell含有8条lane lane通道 每一个flowcell上都有8条泳道,用于测序反应 ...
- Python之进程 基础知识 上
阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 在python程序中的进程操作 multiprocess模块 进程的创建和mu ...
- openstack——neutron网络服务
一.neutron 介绍: Neutron 概述 传统的网络管理方式很大程度上依赖于管理员手工配置和维护各种网络硬件设备:而云环境下的网络已经变得非常复杂,特别是在多租户场景里,用户随时都可能需要 ...
- python包与模块
Python基础-包与模块 摘要 为重用以及更好的维护代码,Python使用了模块与包:一个Python文件就是一个模块,包是组织模块的特殊目录(包含__init__.py文件). 模块搜索路径,Py ...
- 1 JSONP
一.什么是跨域访问举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请求 ...
- JAVA学习总结-基础语法
/** * 这篇文章供自己学习JAVA总结回顾使用 * 主要借鉴了马士兵老师的视频进行总结 * @author Kingram */ 标识符的概念和命名规则 JAVA常量---不可变的变量 程序的执行 ...
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XXX”的默认终结点元素
一.问题 在调用远程web services接口时出现了以下问题: 二.可能的原因和解决方法 网站根目录里的web.config文件缺少了相应的配置信息 <?xml version=" ...