K-means算法Java实现
public class KMeansCluster {
private int k;//簇的个数 private int num = 100000;//迭代次数 private List<double> datas;//原始样本集 private String address;//样本集路径 private List<point> data = new ArrayList<point>(); private AbstractDistance distance = new AbstractDistance() { @Override public double getDis(Point p1, Point p2) { //欧几里德距离 return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(), 2)); } }; public KMeansCluster(int k, int num, String address) { this.k = k; this.num = num; this.address = address; } public KMeansCluster(int k, String address) { this.k = k; this.address = address; } public KMeansCluster(int k, List<double> datas) { this.k = k; this.datas = datas; } public KMeansCluster(int k, int num, List<double> datas) { this.k = k; this.num = num; this.datas = datas; } private void check() { if (k == 0) throw new IllegalArgumentException("k must be the number > 0"); if (address == null && datas == null) throw new IllegalArgumentException("program can't get real data"); } /** * 初始化数据 * * @throws java.io.FileNotFoundException */ public void init() throws FileNotFoundException { check(); //读取文件,init data //处理原始数据 for (int i = 0, j = datas.size(); i < j; i++) data.add(new Point(i, datas.get(i), 0)); } /** * 第一次随机选取中心点 * * @return */ public Set<point> chooseCenter() { Set<point> center = new HashSet<point>(); Random ran = new Random(); int roll = 0; while (center.size() < k) { roll = ran.nextInt(data.size()); center.add(data.get(roll)); } return center; } /** * @param center * @return */ public List<cluster> prepare(Set<point> center) { List<cluster> cluster = new ArrayList<cluster>(); Iterator<point> it = center.iterator(); int id = 0; while (it.hasNext()) { Point p = it.next(); if (p.isBeyond()) { Cluster c = new Cluster(id++, p); c.addPoint(p); cluster.add(c); } else cluster.add(new Cluster(id++, p)); } return cluster; } /** * 第一次运算,中心点为样本值 * * @param center * @param cluster * @return */ public List<cluster> clustering(Set<point> center, List<cluster> cluster) { Point[] p = center.toArray(new Point[0]); TreeSet<distence> distence = new TreeSet<distence>();//存放距离信息 Point source; Point dest; boolean flag = false; for (int i = 0, n = data.size(); i < n; i++) { distence.clear(); for (int j = 0; j < center.size(); j++) { if (center.contains(data.get(i))) break; flag = true; // 计算距离 source = data.get(i); dest = p[j]; distence.add(new Distence(source, dest, distance)); } if (flag == true) { Distence min = distence.first(); for (int m = 0, k = cluster.size(); m < k; m++) { if (cluster.get(m).getCenter().equals(min.getDest())) cluster.get(m).addPoint(min.getSource()); } } flag = false; } return cluster; } /** * 迭代运算,中心点为簇内样本均值 * * @param cluster * @return */ public List<cluster> cluster(List<cluster> cluster) { // double error; Set<point> lastCenter = new HashSet<point>(); for (int m = 0; m < num; m++) { // error = 0; Set<point> center = new HashSet<point>(); // 重新计算聚类中心 for (int j = 0; j < k; j++) { List<point> ps = cluster.get(j).getMembers(); int size = ps.size(); if (size < 3) { center.add(cluster.get(j).getCenter()); continue; } // 计算距离 double x = 0.0, y = 0.0; for (int k1 = 0; k1 < size; k1++) { x += ps.get(k1).getX(); y += ps.get(k1).getY(); } //得到新的中心点 Point nc = new Point(-1, x / size, y / size, false); center.add(nc); } if (lastCenter.containsAll(center))//中心点不在变化,退出迭代 break; lastCenter = center; // 迭代运算 cluster = clustering(center, prepare(center)); // for (int nz = 0; nz < k; nz++) { // error += cluster.get(nz).getError();//计算误差 // } } return cluster; } /** * 输出聚类信息到控制台 * * @param cs */ public void out2console(List<cluster> cs) { for (int i = 0; i < cs.size(); i++) { System.out.println("No." + (i + 1) + " cluster:"); Cluster c = cs.get(i); List<point> p = c.getMembers(); for (int j = 0; j < p.size(); j++) { System.out.println("\t" + p.get(j).getX() + " "); } System.out.println(); } } }K-means算法Java实现的更多相关文章
- k近邻算法-java实现
最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学python,就在朋友的推荐之下选择了这本书进行学习. 一 . K-近邻算法(KNN)概述 最简单最初级的分 ...
- KNN 与 K - Means 算法比较
KNN K-Means 1.分类算法 聚类算法 2.监督学习 非监督学习 3.数据类型:喂给它的数据集是带label的数据,已经是完全正确的数据 喂给它的数据集是无label的数据,是杂乱无章的,经过 ...
- K-means算法
K-means算法很简单,它属于无监督学习算法中的聚类算法中的一种方法吧,利用欧式距离进行聚合啦. 解决的问题如图所示哈:有一堆没有标签的训练样本,并且它们可以潜在地分为K类,我们怎么把它们划分呢? ...
- k近邻算法的Java实现
k近邻算法是机器学习算法中最简单的算法之一,工作原理是:存在一个样本数据集合,即训练样本集,并且样本集中的每个数据都存在标签,即我们知道样本集中每一数据和所属分类的对应关系.输入没有标签的新数据之后, ...
- KNN算法java实现代码注释
K近邻算法思想非常简单,总结起来就是根据某种距离度量检测未知数据与已知数据的距离,统计其中距离最近的k个已知数据的类别,以多数投票的形式确定未知数据的类别. 一直想自己实现knn的java实现,但限于 ...
- Floyd算法java实现demo
Floyd算法java实现,如下: https://www.cnblogs.com/Halburt/p/10756572.html package a; /** * ┏┓ ┏┓+ + * ┏┛┻━━━ ...
- k-means算法Java一维实现
这里的程序稍微有点变形.k_means方法返回K-means聚类的若干中心点.代码: import java.util.ArrayList; import java.util.Collections; ...
- 感知机学习算法Java实现
感知机学习算法Java实现. Perceptron类用于实现感知机, 其中的perceptronOriginal()方法用于实现感知机学习算法的原始形式: perceptronAnother()方法用 ...
- 一致哈希算法Java实现
一致哈希算法(Consistent Hashing Algorithms)是一个分布式系统中经常使用的算法. 传统的Hash算法当槽位(Slot)增减时,面临全部数据又一次部署的问题.而一致哈希算法确 ...
- 机器学习实战笔记--k近邻算法
#encoding:utf-8 from numpy import * import operator import matplotlib import matplotlib.pyplot as pl ...
随机推荐
- java android 将 List中元素互换位置
很多时候我要对List中的元素调换位置,这时候可以用如下代码,意思是将data中的index1与index2元素互换位置 //data 为List Collections.swap(data,inde ...
- 理解Objective-C Runtime(三)消息转发机制
消息转发机制概述 上一篇博客消息传递机制中讲解了Objective-C中对象的「消息传递机制」.本文需要讲解另外一个重要问题:当对象受到无法处理的消息之后会发生什么情况? 显然,若想令类能理解某条消息 ...
- 05:LGTB 与偶数
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 LGTB 有一个长度为 N 的序列.当序列中存在相邻的两个数的和为偶数的话,LGTB 就能把它 ...
- 【前端】Element-UI 省市县级联选择器 JSON数据
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/element_cascader.html 不想自己处理的就直接下载吧 http://shamoyuu.bj.bce ...
- pair运用
#include <iostream> #include <string> #include <map> #include <algorithm> us ...
- NOI前总结:点分治
点分治: 点分治的题目基本一样,都是路径计数. 其复杂度的保证是依靠 $O(n)$ 找重心的,每一次至少将问题规模减小为原先的$1/2$. 找重心我喜欢$BFS$防止爆栈. int Root(int ...
- 使用git rebase合并多次commit
使用git rebase合并多次commit 聊下 git rebase -i
- Vue scrollBehavior 滚动行为
使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...
- Integrate Your Code with the Frameworks---整合你的代码和框架
Back to Frameworks Integrate Your Code with the Frameworks When you develop an app for OS X or iOS, ...
- 洛谷 - P1012 - 拼数 - 排序
https://www.luogu.org/problemnew/show/P1012 这道水题居然翻车了,还发现不了bug,服气了.并不是空字符一定比不空要好,要取决于替代它的字符的大小.所以还是直 ...