java 聚类_Java实现聚类算法k-means

这是一个使用Java实现K-means聚类算法的代码示例。程序首先随机生成点集,然后通过用户输入确定聚类中心的数量,进行初始化。接着,算法不断迭代更新每个点的归属聚类和聚类中心的位置,直到聚类中心的移动距离小于某个阈值(0.01)为止。
摘要由CSDN通过智能技术生成

1 importjava.sql.Array;2 importjava.util.ArrayList;3 importjava.util.Random;4 importjava.util.Scanner;5

6 classpoint {7 public float x = 0;8 public float y = 0;9 public int flage = -1;10

11 public floatgetX() {12 returnx;13 }14

15 public void setX(floatx) {16 this.x =x;17 }18

19 public floatgetY() {20 returny;21 }22

23 public void setY(floaty) {24 this.y =y;25 }26 }27

28 public classKcluster {29

30 point[] ypo;//点集

31 point[] pacore = null;//old聚类中心

32 point[] pacoren = null;//new聚类中心33

34 //初试聚类中心,点集

35 public voidproductpoint() {36 Scanner cina = newScanner(System.in);37 System.out.print("请输入聚类中点的个数(随机产生):");38 int num =cina.nextInt();39

40 ypo = newpoint[num];41 //随机产生点

42 for (int i = 0; i < num; i++) {43

44 float x = (int) (new Random().nextInt(10));45 float y = (int) (new Random().nextInt(10));46

47 ypo[i] = new point();//对象创建

48 ypo[i].setX(x);49 ypo[i].setY(y);50

51 }52

53 //初始化聚类中心位置

54 System.out.print("请输入初始化聚类中心个数(随机产生):");55 int core =cina.nextInt();56 this.pacore = new point[core];//存放聚类中心

57 this.pacoren = newpoint[core];58

59 Random rand = newRandom();60 int temp[] = new int[core];61 temp[0] =rand.nextInt(num);62 pacore[0] = newpoint();63 pacore[0].x = ypo[temp[0]].x;64 pacore[0].y = ypo[temp[0]].y;65 pacore[0].flage=0;66 //避免产生重复的中心

67 for (int i = 1; i < core; i++) {68 int flage = 0;69 int thistemp =rand.nextInt(num);70 for (int j = 0; j < i; j++) {71 if (temp[j] ==thistemp) {72 flage = 1;//有重复

73 break;74

75 }76 }77 if (flage == 1) {78 i--;79 } else{80 pacore[i] = newpoint();81 pacore[i].x=ypo[thistemp].x;82 pacore[i].y =ypo[thistemp].y;83 pacore[i].flage = 0;//0表示聚类中心

84 }85

86 }87 System.out.println("初始聚类中心:");88 for (int i = 0; i < pacore.length; i++) {89 System.out.println(pacore[i].x + " " +pacore[i].y);90 }91

92 }93

94 // ///找出每个点属于哪个聚类中心

95 public void searchbelong()//找出每个点属于哪个聚类中心

96 {97

98 for (int i = 0; i < ypo.length; i++) {99 double dist = 999;100 int lable = -1;101 for (int j = 0; j < pacore.length; j++) {102

103 double distance =distpoint(ypo[i], pacore[j]);104 if (distance

108

109 }110 }111 ypo[i].flage = lable + 1;112

113 }114

115 }116

117 //更新聚类中心

118 public voidcalaverage() {119

120 for (int i = 0; i < pacore.length; i++) {121 System.out.println("以为中心的点:");123 int numc = 0;124 point newcore = newpoint();125 for (int j = 0; j < ypo.length; j++) {126

127 if (ypo[j].flage == (i + 1)) {128 System.out.println(ypo[j].x + "," +ypo[j].y);129 numc += 1;130 newcore.x +=ypo[j].x;131 newcore.y +=ypo[j].y;132

133 }134 }135 //新的聚类中心

136 pacoren[i] = newpoint();137 pacoren[i].x = newcore.x /numc;138 pacoren[i].y = newcore.y /numc;139 pacoren[i].flage = 0;140 System.out.println("新的聚类中心:" + pacoren[i].x + "," +pacoren[i].y);141

142 }143 }144

145 public doubledistpoint(point px, point py) {146

147 return Math.sqrt(Math.pow((px.x - py.x), 2)148 + Math.pow((px.y - py.y), 2));149

150 }151

152 public voidchange_oldtonew(point[] old, point[] news) {153 for (int i = 0; i < old.length; i++) {154 old[i].x =news[i].x;155 old[i].y =news[i].y;156 old[i].flage = 0;//表示为聚类中心的标志。

157 }158 }159

160 public voidmovecore() {161 //this.productpoint();//初始化,样本集,聚类中心,

162 this.searchbelong();163 this.calaverage();//164 double movedistance = 0;165 int biao = -1;//标志,聚类中心点的移动是否符合最小距离

166 for (int i = 0; i < pacore.length; i++) {167 movedistance =distpoint(pacore[i], pacoren[i]);168 System.out.println("distcore:" + movedistance);//聚类中心的移动距离

169 if (movedistance < 0.01) {170 biao = 0;171

172 } else{173

174 biao=1;//需要继续迭代,

175 break;176

177 }178 }179 if (biao == 0) {180 System.out.print("迭代完毕!!!!!");181 } else{182 change_oldtonew(pacore, pacoren);183 movecore();184 }185

186 }187

188 public static voidmain(String[] args) {189 //TODO Auto-generated method stub

190

191 Kcluster kmean = newKcluster();192 kmean.productpoint();193 kmean.movecore();194 }195

196 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值