一,k-means算法介绍:
k-means算法接受输入量 k ;然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高;而不同聚类中的对象相似度较小。聚类相似度是利用各聚类中对象的均值所获得一个“中心对象”(引力中心)来进行计算的。k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。 k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。
k-means算法的工作过程说明如下:首先从n个数据对象任意选择 k 个对象作为初始聚类中心;而对于所剩下其它对象,则根据它们与这些聚类中心的相似度(距离),分别将它们分配给与其最相似的(聚类中心所代表的)聚类;然后再计算每个所获新聚类的聚类中心(该聚类中所有对象的均值);不断重复这一过程直到标准测度函数开始收敛为止。一般都采用均方差作为标准测度函数。k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。
二,k-means算法基本步骤:
(1) 从 n个数据对象任意选择 k 个对象作为初始聚类中心;(2) 根据每个聚类对象的均值(中心对象),计算每个对象与这些中心对象的距离;并根据最小距离重新对相应对象进行划分;
(3) 重新计算每个(有变化)聚类的均值(中心对象);
(4) 计算标准测度函数,当满足一定条件,如函数收敛时,则算法终止;如果条件不满足则回到步骤(2),不断重复直到标准测度函数开始收敛为止。(一般都采用均方差作为标准测度函数。)
三,k-means算法的java实现:
一共有七个类,General.java代表武将对象, Distance.java距离类计算各个武将到中心武将之间的距离, Cluster.java聚类对象包含一个中心武将和该聚类中所有武将, Kmeans.java核心的聚类算法类, Tool.java工具类用于转换武将的星级为数字等操作, TestKmeans.java测试类即入口文件, DomParser.java用于读取xml中的681个武将。
具体思路:先从general.xml文件中读取681个武将,然后随机选取初始类中心,计算各个武将到中心武将的距离,根据最小的距离进行聚类,然后重新根据平均值新的聚类的类中心,重新计算各个武将到新的中心武将的距离,直到更新后的聚类与原来的聚类包含的武将不再改变,即收敛时结束。
具体代码如下:
1,General.java
package kmeans;
public class General {
private String name; // 姓名
private int render; // 星级
private int tongshai; // 统帅
private int wuli; // 武力
private int zhili; // 智力
private int polic; // 政治
private int qiangbin; // 枪兵
private int jibin; // 戟兵
private int nubin; // 弩兵
private int qibin; // 骑兵
private int binqi; // 兵器
private int tongwu; // 统武
private int tongzhi; // 统智
private int tongwuzhi; // 统武智
private int tongwuzhizheng; // 统武智政
private int salary; // 50级工资
public General(int render, String name, int tongshai, int wuli, int zhili,
int polic, int qiangbin, int jibin, int nubin, int qibin,
int binqi, int tongwu, int tongzhi, int tongwuzhi,
int tongwuzhizheng, int salary) {
super();
this.name = name;
this.render = render;
this.tongshai = tongshai;
this.wuli = wuli;
this.zhili = zhili;
this.polic = polic;
this.qiangbin = qiangbin;
this.jibin = jibin;
this.nubin = nubin;
this.qibin = qibin;
this.binqi = binqi;
this.tongwu = tongwu;
this.tongzhi = tongzhi;
this.tongwuzhi = tongwuzhi;
this.tongwuzhizheng = tongwuzhizheng;
this.salary = salary;
}
public General(int render, int tongshai, int wuli, int zhili, int polic,
int qiangbin, int jibin, int nubin, int qibin, int binqi,
int tongwu, int tongzhi, int tongwuzhi, int tongwuzhizheng,
int salary) {
super();
this.name = "聚类中心";
this.render = render;
this.tongshai = tongshai;
this.wuli = wuli;
this.zhili = zhili;
this.polic = polic;
this.qiangbin = qiangbin;
this.jibin = jibin;
this.nubin = nubin;
this.qibin = qibin;
this.binqi = binqi;
this.tongwu = tongwu;
this.tongzhi = tongzhi;
this.tongwuzhi = tongwuzhi;
this.tongwuzhizheng = tongwuzhizheng;
this.salary = salary;
}
public General() {
}
@Override
public String toString() {
return "武将 [name=" + name + ", render=" + Tool.dxingji(render)
+ ", tongshai=" + tongshai + ", wuli=" + wuli + ", zhili="
+ zhili + ", polic=" + polic + ", qiangbin="
+ Tool.dchange(qiangbin) + ", jibin=" + Tool.dchange(jibin)
+ ", nubin=" + Tool.dchange(nubin) + ", qibin="
+ Tool.dchange(qibin) + ", binqi=" + Tool.dchange(binqi)
+ ", tongwu=" + tongwu + ", tongzhi=" + tongzhi
+ ", tongwuzhi=" + tongwuzhi + ", tongwuzhizheng="
+ tongwuzhizheng + ", salary=" + salary + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRender() {
return render;
}
public void setRender(int render) {
this.render = render;
}
public int getTongshai() {
return tongshai;
}
public void setTongshai(int tongshai) {
this.tongshai = tongshai;
}
public int getWuli() {
return wuli;
}
public void setWuli(int wuli) {
this.wuli = wuli;
}
public int getZhili() {
return zhili;
}
public void setZhili(int zhili) {
this.zhili = zhili;
}
public int getPolic() {
return polic;
}
public void setPolic(int polic) {
this.polic = polic;
}
public int getQiangbin() {
return qiangbin;
}
public void setQiangbin(int qiangbin) {
this.qiangbin = qiangbin;
}
public int getJibin() {
return jibin;
}
public void setJibin(int jibin) {
this.jibin = jibin;
}
public int getNubin() {
return nubin;
}
public void setNubin(int nubin) {
this.nubin = nubin;
}
public int getQibin() {
return qibin;
}
public void setQibin(int qibin) {
this.qibin = qibin;
}
public int getBinqi() {
return binqi;
}
public void setBinqi(int binqi) {
this.binqi = binqi;
}
public int getTongwu() {
return tongwu;
}
public void setTongwu(int tongwu) {
this.tongwu = tongwu;
}
public int getTongzhi() {
return tongzhi;
}
public void setTongzhi(int tongzhi) {
this.tongzhi = tongzhi;
}
public int getTongwuzhi() {
return tongwuzhi;
}
public void setTongwuzhi(int tongwuzhi) {
this.tongwuzhi = tongwuzhi;
}
public int getTongwuzhizheng() {
return tongwuzhizheng;
}
public void setTongwuzhizheng(int tongwuzhizheng) {
this.tongwuzhizheng = tongwuzhizheng;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
2,Distance.java
package kmeans;
/**
* 这个类用于计算距离的。。
*
*/
public class Distance {
int dest;// 目的
int source;// 源
double dist;// 欧式距离
public int getDest() {
return dest;
}
public void setDest(int dest) {
this.dest = dest;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
public double getDist() {
return dist;
}
public void setDist(double dist) {
this.dist = dist;
}
/**
* 计算源和目的的距离
* @param dest 目的武将
* @param source 源武将
* @param dist 两者间的距离
*/
public Distance(int dest, int source, double dist) {
this.dest = dest;
this.source = source;
this.dist = dist;
}
public Distance() {
}
}
3,Cluster.java
package kmeans;
import java.util.ArrayList;
public class Cluster {
private int center;// 聚类中心武将的id
private ArrayList<General> ofCluster = new ArrayList<General>();// 属于这个聚类的武将的集合
public int getCenter() {
return center;
}
public void setCenter(int center) {
this.center = center;
}
public ArrayList<General> getOfCluster() {
return ofCluster;
}
public void setOfCluster(ArrayList<General> ofCluster) {
this.ofCluster = ofCluster;
}
public void addGeneral(General general) {
if (!(this.ofCluster.contains(general)))
this.ofCluster.add(general);
}
}
4,Kmeans.java
package kmeans;
import java.util.*;
public class Kmeans {
public ArrayList<General> allGenerals = null;
public in