原始数据

#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++的更多相关文章

  1. KNN 与 K - Means 算法比较

    KNN K-Means 1.分类算法 聚类算法 2.监督学习 非监督学习 3.数据类型:喂给它的数据集是带label的数据,已经是完全正确的数据 喂给它的数据集是无label的数据,是杂乱无章的,经过 ...

  2. 软件——机器学习与Python,聚类,K——means

    K-means是一种聚类算法: 这里运用k-means进行31个城市的分类 城市的数据保存在city.txt文件中,内容如下: BJ,2959.19,730.79,749.41,513.34,467. ...

  3. 快速查找无序数组中的第K大数?

    1.题目分析: 查找无序数组中的第K大数,直观感觉便是先排好序再找到下标为K-1的元素,时间复杂度O(NlgN).在此,我们想探索是否存在时间复杂度 < O(NlgN),而且近似等于O(N)的高 ...

  4. 网络费用流-最小k路径覆盖

    多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. 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 ...

  6. 当我们在谈论kmeans(2)

        本稿为初稿,后续可能还会修改:如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/ 其他:建设中- 当我们在谈论kmeans(2 ...

  7. scikit-learn包的学习资料

    http://scikit-learn.org/stable/modules/clustering.html#k-means http://my.oschina.net/u/175377/blog/8 ...

  8. HDU 3584 Cube (三维 树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3584 Cube Problem Description Given an N*N*N cube A,  ...

  9. Torch7学习笔记(二)nn Package

    神经网络Package [目前还属于草稿版,等我整个学习玩以后会重新整理] 模块Module module定义了训练神经网络需要的所有基础方法,并且是可以序列化的抽象类. module有两种状态变量: ...

  10. 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. http通信流程

    Host https://www.charlesproxy.com Path / Notes SSL Proxying not enabled for this host. Enable in the ...

  2. 解决Mysql Workbench的Error Code: 1175错误

    错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...

  3. sublime text3 verilog代码编写高级操作篇

    2018.10.21 好久没写博客了,这段时间一直在学习一直在沉淀,然而发现学的越多会的更少,只能快马加鞭吧! 博主从大一暑假接触FPGA,到现在快一年半了,时间恍逝.刚开始入门也是用的quartus ...

  4. auto类型推导

    引言 auto : 类型推导. 在使用c++的时候会经常使用, 就像在考虑STL时迭代器类型, 写模板的时候使用auto能少写代码, 也能帮助我们避免一些隐患的细节. auto初始化 使用auto型别 ...

  5. 啥是SQL?

    SQL:(Structured Query Language)是结构化查询语言缩写.是一门专门与数据库管理系统打交道的语言. SQL语言:是关系型数据库的标准语言,,其主要用于存取数据,查询数据,更新 ...

  6. Linux之强大的selinux

    简单点说,SELinux就是用来加强系统安全性的.它给一些特定程序(这些程序也在不断增加)做了一个沙箱,它将文件打上了一个安全标签,这些标签属于不同的类,也只能执行特定的操作,也就是规定了某个应用程序 ...

  7. array_map 等php回调函数使用问题(关联数组下标获取)

    前言:我自己用此类回调函数,来替代 foreach 纯粹是用为代码的简洁性,让代码更好看.(我有点代码小洁癖~) 1.array_reduce 当迭代处理一个一维索引数组时,在回调函数内是无法获取到当 ...

  8. 【codeforces 796C】Bank Hacking

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

  9. 混合了RBAC和ACL的权限系统(一) -- 用户组织结构

    最近的工作是一个基础设计,打造一个基于RBAC和ACL的权限基础组件. 这个基础组件的特点是:同时混合了RBAC和ACL的认证方式,也就是说同时提供系统级别的授权(RBAC)和对象级别的授权(ACL) ...

  10. hdu 3605 最大流sap+二进制思想(啊啊)

    /*因为n非常大如果正常建边的话会超内存,每种状态的数目共2--10种状状体记录起来,源点与状态建边权值为状态数,状态与星球建边,星球与汇点建边*/ #include<stdio.h> # ...