java简单实现聚类算法

第一个版本有一些问题,,(一段废话biubiu。。。),,我其实每次迭代之后(就是达不到收敛标准之前,聚类中心的误差达不到指定小的时候),虽然重新算了聚类中心,但是其实我的那些点并没有变,可是这个程序不知道咋回事每次都把我原先随机指定的聚类中心给变成了我算的聚类中心;怎么用,按照指示来就行了,不用读文件(源码全都是可以运行,不足之处还望批评指正)输出的结果有一堆小数的那是新聚类中心和老的的误差值,在没有达到指定小的时候,是不会停的。

////////////////////

重新看看。。终于改好了。。。。。。Java对象直接赋值属于浅拷贝

修改后为创建一个对象,值来源于随机点,但是跟随机点已经没有任何关系了。。。。。

A a=b;浅拷贝

..............................

A a=new A();

a.x=b.x;

a.y=b.y;

a并没有引用b

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

题目如下:、

初始,有问题版本:

 import java.sql.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner; class point {
public float x = 0;
public float y = 0;
public int flage = -1; public float getX() {
return x;
} public void setX(float x) {
this.x = x;
} public float getY() {
return y;
} public void setY(float y) {
this.y = y;
}
} public class Kcluster { point[] ypo;// 点集
point[] pacore = null;// old聚类中心
point[] pacoren = null;// new聚类中心 // 初试聚类中心,点集
public void productpoint() {
Scanner cina = new Scanner(System.in);
System.out.print("请输入聚类中点的个数(随机产生):");
int num = cina.nextInt(); ypo = new point[num];
// 随机产生点
for (int i = 0; i < num; i++) { float x = (int) (new Random().nextInt(10));
float y = (int) (new Random().nextInt(10)); ypo[i] = new point();// 对象创建
ypo[i].setX(x);
ypo[i].setY(y); } // 初始化聚类中心位置
System.out.print("请输入初始化聚类中心个数(随机产生):");
int core = cina.nextInt();
this.pacore = new point[core];// 存放聚类中心
this.pacoren = new point[core]; Random rand = new Random();
int temp[] = new int[core];
temp[0] = rand.nextInt(num);
pacore[0] = new point();
pacore[0] = ypo[temp[0]];
// 避免产生重复的中心
for (int i = 1; i < core; i++) {
int flage = 0;
int thistemp = rand.nextInt(num);
for (int j = 0; j < i; j++) {
if (temp[j] == thistemp) {
flage = 1;// 有重复
break; }
}
if (flage == 1) {
i--;
} else {
pacore[i] = new point();
pacore[i] = ypo[thistemp];
pacore[i].flage = 0;// 0表示聚类中心
} }
System.out.println("初始聚类中心:");
for (int i = 0; i < pacore.length; i++) {
System.out.println(pacore[i].x + " " + pacore[i].y);
} } // ///找出每个点属于哪个聚类中心
public void searchbelong()// 找出每个点属于哪个聚类中心
{ for (int i = 0; i < ypo.length; i++) {
double dist = 999;
int lable = -1;
for (int j = 0; j < pacore.length; j++) { double distance = distpoint(ypo[i], pacore[j]);
if (distance < dist) {
dist = distance;
lable = j;
// po[i].flage = j + 1;// 1,2,3...... }
}
ypo[i].flage = lable + 1; } } // 更新聚类中心
public void calaverage() { for (int i = 0; i < pacore.length; i++) {
System.out.println("以<" + pacore[i].x + "," + pacore[i].y
+ ">为中心的点:");
int numc = 0;
point newcore = new point();
for (int j = 0; j < ypo.length; j++) { if (ypo[j].flage == (i + 1)) {
numc += 1;
newcore.x += ypo[j].x;
newcore.y += ypo[j].y;
System.out.println(ypo[j].x + "," + ypo[j].y);
}
}
// 新的聚类中心
pacoren[i] = new point();
pacoren[i].x = newcore.x / numc;
pacoren[i].y = newcore.y / numc;
pacoren[i].flage = 0;
System.out.println("新的聚类中心:" + pacoren[i].x + "," + pacoren[i].y); }
} public double distpoint(point px, point py) { return Math.sqrt(Math.pow((px.x - py.x), 2)
+ Math.pow((px.y - py.y), 2)); } public void change_oldtonew(point[] old, point[] news) {
for (int i = 0; i < old.length; i++) {
old[i].x = news[i].x;
old[i].y = news[i].y;
old[i].flage = 0;// 表示为聚类中心的标志。
}
} public void movecore() {
// this.productpoint();//初始化,样本集,聚类中心,
this.searchbelong();
this.calaverage();//
double movedistance = 0;
int biao = -1;//标志,聚类中心点的移动是否符合最小距离
for (int i = 0; i < pacore.length; i++) {
movedistance = distpoint(pacore[i], pacoren[i]);
System.out.println("distcore:" + movedistance);//聚类中心的移动距离
if (movedistance < 0.01) {
biao = 0; } else { biao=1;
break; }
}
if (biao == 0) {
System.out.print("迭代完毕!!!!!");
} else {
change_oldtonew(pacore, pacoren);
movecore();
} } public static void main(String[] args) {
// TODO Auto-generated method stub Kcluster kmean = new Kcluster();
kmean.productpoint();
kmean.movecore();
} }

修稿版:在初始化聚类中心那里。有一些改动。。。。。。。。。。。嘤嘤嘤

 import java.sql.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner; class point {
public float x = 0;
public float y = 0;
public int flage = -1; public float getX() {
return x;
} public void setX(float x) {
this.x = x;
} public float getY() {
return y;
} public void setY(float y) {
this.y = y;
}
} public class Kcluster { point[] ypo;// 点集
point[] pacore = null;// old聚类中心
point[] pacoren = null;// new聚类中心 // 初试聚类中心,点集
public void productpoint() {
Scanner cina = new Scanner(System.in);
System.out.print("请输入聚类中点的个数(随机产生):");
int num = cina.nextInt(); ypo = new point[num];
// 随机产生点
for (int i = 0; i < num; i++) { float x = (int) (new Random().nextInt(10));
float y = (int) (new Random().nextInt(10)); ypo[i] = new point();// 对象创建
ypo[i].setX(x);
ypo[i].setY(y); } // 初始化聚类中心位置
System.out.print("请输入初始化聚类中心个数(随机产生):");
int core = cina.nextInt();
this.pacore = new point[core];// 存放聚类中心
this.pacoren = new point[core]; Random rand = new Random();
int temp[] = new int[core];
temp[0] = rand.nextInt(num);
pacore[0] = new point();
pacore[0].x = ypo[temp[0]].x;
pacore[0].y = ypo[temp[0]].y;
pacore[0].flage=0 ;
// 避免产生重复的中心
for (int i = 1; i < core; i++) {
int flage = 0;
int thistemp = rand.nextInt(num);
for (int j = 0; j < i; j++) {
if (temp[j] == thistemp) {
flage = 1;// 有重复
break; }
}
if (flage == 1) {
i--;
} else {
pacore[i] = new point();
pacore[i].x= ypo[thistemp].x;
pacore[i].y = ypo[thistemp].y;
pacore[i].flage = 0;// 0表示聚类中心
} }
System.out.println("初始聚类中心:");
for (int i = 0; i < pacore.length; i++) {
System.out.println(pacore[i].x + " " + pacore[i].y);
} } // ///找出每个点属于哪个聚类中心
public void searchbelong()// 找出每个点属于哪个聚类中心
{ for (int i = 0; i < ypo.length; i++) {
double dist = 999;
int lable = -1;
for (int j = 0; j < pacore.length; j++) { double distance = distpoint(ypo[i], pacore[j]);
if (distance < dist) {
dist = distance;
lable = j;
// po[i].flage = j + 1;// 1,2,3...... }
}
ypo[i].flage = lable + 1; } } // 更新聚类中心
public void calaverage() { for (int i = 0; i < pacore.length; i++) {
System.out.println("以<" + pacore[i].x + "," + pacore[i].y
+ ">为中心的点:");
int numc = 0;
point newcore = new point();
for (int j = 0; j < ypo.length; j++) { if (ypo[j].flage == (i + 1)) {
System.out.println(ypo[j].x + "," + ypo[j].y);
numc += 1;
newcore.x += ypo[j].x;
newcore.y += ypo[j].y; }
}
// 新的聚类中心
pacoren[i] = new point();
pacoren[i].x = newcore.x / numc;
pacoren[i].y = newcore.y / numc;
pacoren[i].flage = 0;
System.out.println("新的聚类中心:" + pacoren[i].x + "," + pacoren[i].y); }
} public double distpoint(point px, point py) { return Math.sqrt(Math.pow((px.x - py.x), 2)
+ Math.pow((px.y - py.y), 2)); } public void change_oldtonew(point[] old, point[] news) {
for (int i = 0; i < old.length; i++) {
old[i].x = news[i].x;
old[i].y = news[i].y;
old[i].flage = 0;// 表示为聚类中心的标志。
}
} public void movecore() {
// this.productpoint();//初始化,样本集,聚类中心,
this.searchbelong();
this.calaverage();//
double movedistance = 0;
int biao = -1;//标志,聚类中心点的移动是否符合最小距离
for (int i = 0; i < pacore.length; i++) {
movedistance = distpoint(pacore[i], pacoren[i]);
System.out.println("distcore:" + movedistance);//聚类中心的移动距离
if (movedistance < 0.01) {
biao = 0; } else { biao=1;//需要继续迭代,
break; }
}
if (biao == 0) {
System.out.print("迭代完毕!!!!!");
} else {
change_oldtonew(pacore, pacoren);
movecore();
} } public static void main(String[] args) {
// TODO Auto-generated method stub Kcluster kmean = new Kcluster();
kmean.productpoint();
kmean.movecore();
} }

Java实现聚类算法k-means的更多相关文章

  1. ML: 聚类算法-K均值聚类

    基于划分方法聚类算法R包: K-均值聚类(K-means)                   stats::kmeans().fpc::kmeansruns() K-中心点聚类(K-Medoids) ...

  2. 聚类算法:K均值、凝聚层次聚类和DBSCAN

    聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...

  3. 常见聚类算法——K均值、凝聚层次聚类和DBSCAN比较

    聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...

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

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

  5. 【机器学习】聚类算法——K均值算法(k-means)

    一.聚类 1.基于划分的聚类:k-means.k-medoids(每个类别找一个样本来代表).Clarans 2.基于层次的聚类:(1)自底向上的凝聚方法,比如Agnes (2)自上而下的分裂方法,比 ...

  6. 数据挖掘十大算法--K-均值聚类算法

    一.相异度计算  在正式讨论聚类前,我们要先弄清楚一个问题:怎样定量计算两个可比較元素间的相异度.用通俗的话说.相异度就是两个东西区别有多大.比如人类与章鱼的相异度明显大于人类与黑猩猩的相异度,这是能 ...

  7. 第十三篇:K-Means 聚类算法原理分析与代码实现

    前言 在前面的文章中,涉及到的机器学习算法均为监督学习算法. 所谓监督学习,就是有训练过程的学习.再确切点,就是有 "分类标签集" 的学习. 现在开始,将进入到非监督学习领域.从经 ...

  8. 机器学习六--K-means聚类算法

    机器学习六--K-means聚类算法 想想常见的分类算法有决策树.Logistic回归.SVM.贝叶斯等.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别 ...

  9. k-means均值聚类算法(转)

    4.1.摘要 在前面的文章中,介绍了三种常见的分类算法.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是很多时候上述条件得不到满足,尤其是在 ...

随机推荐

  1. java-appium-527 WebDriver协议&针对控件的操作

    1.WebDriver协议 https://www.w3.org/TR/webdriver/#list-of-endpoints 1.1查看当前所有的session情况 http://127.0.0. ...

  2. Missing Number @leetcode

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. python中将HTTP头部中的GMT时间转换成datetime时间格式

    原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...

  4. [UE4]动画事件

    在动画中添加事件通知,在动画蓝图中就可以使用这个事件通知: 在动画蓝图中可以使用“Try Get Pawn Owner”取得控制的角色实例 在Controller中,可以使用“Get Controll ...

  5. 理解 tornado.gen

    转自:http://blog.xiaogaozi.org/2012/09/21/understanding-tornado-dot-gen/ 理解 tornado.gen SEP 21ST, 2012 ...

  6. 由一条普通的link引用引发的无数问号,大家能回答的帮忙回答回答吧.

    <link type="text/css" rel="stylesheet" href="1.css" /> 对于前台工作者来说 ...

  7. OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo)

    OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo) 1.问题描述 [oracle@dou_ra ...

  8. Hadoop(1.2.1)安装

    背景知识: 1.数据分布存储,不是复制存储 2.数据不动,代码动,由于分布式存储,所以把代码移动到数据的地方计算. 3.数据如何分割,hadoop提供的分割文件的编程接口 安装: 1.安装JDK 1. ...

  9. web service初探

    概述:Web service是一个平台独立.低耦合的.自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发 ...

  10. sqlserver主从复制

    参考网站: http://www.178linux.com/9079 https://www.cnblogs.com/tatsuya/p/5025583.html windows系统环境进行主从复制操 ...