Java实现聚类算法k-means
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的更多相关文章
- ML: 聚类算法-K均值聚类
基于划分方法聚类算法R包: K-均值聚类(K-means) stats::kmeans().fpc::kmeansruns() K-中心点聚类(K-Medoids) ...
- 聚类算法:K均值、凝聚层次聚类和DBSCAN
聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...
- 常见聚类算法——K均值、凝聚层次聚类和DBSCAN比较
聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...
- 软件——机器学习与Python,聚类,K——means
K-means是一种聚类算法: 这里运用k-means进行31个城市的分类 城市的数据保存在city.txt文件中,内容如下: BJ,2959.19,730.79,749.41,513.34,467. ...
- 【机器学习】聚类算法——K均值算法(k-means)
一.聚类 1.基于划分的聚类:k-means.k-medoids(每个类别找一个样本来代表).Clarans 2.基于层次的聚类:(1)自底向上的凝聚方法,比如Agnes (2)自上而下的分裂方法,比 ...
- 数据挖掘十大算法--K-均值聚类算法
一.相异度计算 在正式讨论聚类前,我们要先弄清楚一个问题:怎样定量计算两个可比較元素间的相异度.用通俗的话说.相异度就是两个东西区别有多大.比如人类与章鱼的相异度明显大于人类与黑猩猩的相异度,这是能 ...
- 第十三篇:K-Means 聚类算法原理分析与代码实现
前言 在前面的文章中,涉及到的机器学习算法均为监督学习算法. 所谓监督学习,就是有训练过程的学习.再确切点,就是有 "分类标签集" 的学习. 现在开始,将进入到非监督学习领域.从经 ...
- 机器学习六--K-means聚类算法
机器学习六--K-means聚类算法 想想常见的分类算法有决策树.Logistic回归.SVM.贝叶斯等.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别 ...
- k-means均值聚类算法(转)
4.1.摘要 在前面的文章中,介绍了三种常见的分类算法.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是很多时候上述条件得不到满足,尤其是在 ...
随机推荐
- gradle使用心得
gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...
- js之ActiveX控件使用说明 new ActiveXObject()
什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...
- MYSQL中只知表名查询属于哪个SCHEMA
只知道表名XXX查该表属于哪个schema.以及该表有哪些列等信息 SELECT * from information_schema.columns WHERE table_name = 'xxx'; ...
- IT职业发展攻略(技术仅是工具而已)
时光飞逝,我事业中第一个十年就快结束了.在这十年里,让我收获了很多,今天想与大家分享一下,我在 IT 职场方面的一些个人经验,不一定对每个人都实用,请大家仅作参考吧. 大家既然都是做技术的,那我们不妨 ...
- php array_flip() 删除数组重复元素——大彻大悟
1. php array_flip() 删除数组重复元素,如果用于一维索引数组,好理解. [root@BG-DB:~]$more arr.php <?php $arr = ar ...
- hint之qb_name
http://www.thinkindata.com/?p=34 该hint用于子查询(query_block) 很多的情况下,如果子查询共用相同的别名(alias), 可以通过设定不同的qb_n ...
- 腾讯优图联手Science发布主题报告:计算机视觉的研发和应用
近日,腾讯优图与<科学>(Science)杂志共同发布<Seeing is believing: R&D applications of computer vision> ...
- Java面向对象之抽象类
内容: 1.抽象类的产生 2.抽象类和抽象方法的定义与使用 3.抽象类和抽象方法的注意事项 4.实例分析 1.抽象类的产生 当编写一个类时,我们往往会为该类定义一些方法,这些方法是用来描述该类的功能具 ...
- django中使用Ajax
内容: 1.Ajax原理与基本使用 2.Ajax发送get请求 3.Ajax发送post请求 4.Ajax上传文件 5.Ajax设置csrf_token 6.django序列化 参考:https:// ...
- tornado-通过判断后台数据限制登陆--简单的
import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...