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.摘要 在前面的文章中,介绍了三种常见的分类算法.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是很多时候上述条件得不到满足,尤其是在 ...
随机推荐
- httpclient httpcore jar包及源码
HttpClient HttpCore HttpComponents jar src download httpclient home help
- gradle使用心得
gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...
- 1010 Radix (25 分)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 1 ...
- ubuntu 查看系统是32位还是64位
查看cpu信息 cat /proc/cpiinfo 查看ubuntu版本: cat /etc/issue 查看系统是32位还是64位 方法1: #查看long的位数,返回32或64 getconf L ...
- AVL树Python实现
# coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...
- Java读取文件方法大全
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读 ...
- SQL Server数据库定时备份解决方案
SQL Server数据库定时备份解决方案 1.本方案采用软件为:SQLBackupAndFTP 10.0.3 版本,压缩包自带注册机,请自行破解. 2.软件截图如下: 3.功能说明:自动定时备份相关 ...
- shell语法(二)
Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...
- 数据分析利器之hive优化十大原则
hive之于数据民工,就如同锄头之于农民伯伯.hive用的好,才能从地里(数据库)里挖出更多的数据来. 用过hive的朋友,我想或多或少都有类似的经历:一天下来,没跑几次hive,就到下班时间了. h ...
- JS 操作 file标签只上传照片
在当前高版本浏览器里 在标签里加这个属性就够用了 accept="image/*" $('input[type="file"]').live('change', ...