kmeans算法Java实现.doc

上传人:汽*** 文档编号:562704220 上传时间:2022-10-22 格式:DOC 页数:6 大小:37.01KB
返回 下载 相关 举报
kmeans算法Java实现.doc_第1页
第1页 / 共6页
kmeans算法Java实现.doc_第2页
第2页 / 共6页
kmeans算法Java实现.doc_第3页
第3页 / 共6页
kmeans算法Java实现.doc_第4页
第4页 / 共6页
kmeans算法Java实现.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《kmeans算法Java实现.doc》由会员分享,可在线阅读,更多相关《kmeans算法Java实现.doc(6页珍藏版)》请在金锄头文库上搜索。

1、给点一组 c 点资料 X = x1, ., xc ,每一点都有 d 维;给定一个群聚的数目 k, 求其最好的聚类结果。 (1)package cluster.kmeans;class Centroid private double mCx, mCy; private Cluster mCluster; public Centroid(double cx, double cy) this.mCx = cx; this.mCy = cy; public void calcCentroid() /only called by CAInstance int numDP = mCluster.getNu

2、mDataPoints(); double tempX = 0, tempY = 0; int i; /caluclating the new Centroid for (i = 0; i numDP; i+) tempX = tempX + mCluster.getDataPoint(i).getX(); /total for x tempY = tempY + mCluster.getDataPoint(i).getY(); /total for y this.mCx = tempX / numDP; this.mCy = tempY / numDP; /calculating the n

3、ew Euclidean Distance for each Data Point tempX = 0; tempY = 0; for (i = 0; i numDP; i+) mCluster.getDataPoint(i).calcEuclideanDistance(); /calculate the new Sum of Squares for the Cluster mCluster.calcSumOfSquares(); public void setCluster(Cluster c) this.mCluster = c; public double getCx() return

4、mCx; public double getCy() return mCy; public Cluster getCluster() return mCluster; 2)import java.util.Vector;class Cluster private String mName; private Centroid mCentroid; private double mSumSqr; private Vector mDataPoints; public Cluster(String name) this.mName = name; this.mCentroid = null; /wil

5、l be set by calling setCentroid() mDataPoints = new Vector(); public void setCentroid(Centroid c) mCentroid = c; public Centroid getCentroid() return mCentroid; public void addDataPoint(DataPoint dp) /called from CAInstance dp.setCluster(this); /initiates a inner call to calc EuclideanDistance() in

6、DP. this.mDataPoints.addElement(dp); calcSumOfSquares(); public void removeDataPoint(DataPoint dp) this.mDataPoints.removeElement(dp); calcSumOfSquares(); public int getNumDataPoints() return this.mDataPoints.size(); public DataPoint getDataPoint(int pos) return (DataPoint) this.mDataPoints.elementA

7、t(pos); public void calcSumOfSquares() /called from Centroid int size = this.mDataPoints.size(); double temp = 0; for (int i = 0; i size; i+) temp = temp + (DataPoint)this.mDataPoints.elementAt(i).getCurrentEuDt(); this.mSumSqr = temp; public double getSumSqr() return this.mSumSqr; public String get

8、Name() return this.mName; public Vector getDataPoints() return this.mDataPoints; (3)package cluster.kmeans;public class DataPoint private double mX,mY; private String mObjName; private Cluster mCluster; private double mEuDt; public DataPoint(double x, double y, String name) this.mX = x; this.mY = y;

9、 this.mObjName = name; this.mCluster = null; public void setCluster(Cluster cluster) this.mCluster = cluster; calcEuclideanDistance(); public void calcEuclideanDistance() /called when DP is added to a cluster or when a Centroid is recalculated. mEuDt = Math.sqrt(Math.pow(mX - mCluster.getCentroid().

10、getCx(), 2) + Math.pow(mY - mCluster.getCentroid().getCy(), 2); public double testEuclideanDistance(Centroid c) return Math.sqrt(Math.pow(mX - c.getCx(), 2) + Math.pow(mY - c.getCy(), 2); public double getX() return mX; public double getY() return mY; public Cluster getCluster() return mCluster; pub

11、lic double getCurrentEuDt() return mEuDt; public String getObjName() return mObjName; (4)import java.util.Vector;public class JCA private Cluster clusters; private int miter; private Vector mDataPoints = new Vector(); private double mSWCSS; public JCA(int k, int iter, Vector dataPoints) clusters = n

12、ew Clusterk; for (int i = 0; i k; i+) clustersi = new Cluster(Cluster + i); this.miter = iter; this.mDataPoints = dataPoints; private void calcSWCSS() double temp = 0; for (int i = 0; i clusters.length; i+) temp = temp + clustersi.getSumSqr(); mSWCSS = temp; public void startAnalysis() /set Starting centroid positions - Start of Step 1 setInitialCentroids(); int n = 0; /

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号