免费预览已结束,剩余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.getNumDataPoints(); 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 new 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 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; /will 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 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.elementAt(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 getName() 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; 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().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; public 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 = new 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; /assign DataPoint to clusters loop1: while (true) for (int l = 0; l = mDataPoints.size() break loop1; /calculate E for all the clusters calcSWCSS(); /recalculate Cluster centroids - Start of Step 2 for (int i = 0; i clusters.length; i+) clustersi.getCentroid().calcCentroid(); /recalculate E for all the clusters calcSWCSS(); for (int i = 0; i miter; i+) /enter the loop for cluster 1 for (int j = 0; j clusters.length; j+) for (int k = 0; k clustersj.getNumDataPoints(); k+) /pick the first element of the first cluster /get the current Euclidean distance double tempEuDt = clustersj.getDataPoint(k).getCurrentEuDt(); Cluster tempCluster = null; boolean matchFoundFlag = false; /call testEuclidean distance for all clusters for (int l = 0; l clusters.length; l+) /if testEuclidean clustersj.getDataPoint(k).testEuclideanDistance(clustersl.getCentroid() tempEuDt = clustersj.getDataPoint(k).testEuclideanDistance(clustersl.getCentroid(); tempCluster = clustersl; matchFoundFlag = true; /if statement - Check whether the Last EuDt is Present EuDt /for variable l - Looping between different Clusters for matching a Data Point./add DataPoint to the cluster and calcSWCSS if (matchFoundFlag) tempCluster.addDataPoint(clustersj.getDataPoint(k); clustersj.removeDataPoint(clustersj.getDataPoint(k); for (int m = 0; m clusters.length; m+) clustersm.getCentroid().calcCentroid(); /for variable m - Recalculating centroids for all Clusters calcSWCSS(); /if statement - A Data Point is eligible for transfer between Clusters. /for variable k - Looping through all Data Points of the current Cluster. /for variable j - Looping through all the Clusters. /for variable i - Number of iterations. public Vector getClusterOutput() Vector v = new Vectorclusters.length; for (int i = 0; i clusters.length; i+) vi = clustersi.getDataPoints(); return v; private void setInitialCentroids() /kn = (round(max-min)/k)*n)+min where n is from 0 to (k-1). double cx = 0, cy = 0; for (int n = 1; n = clusters.length; n+) cx = (getMaxXValue() - getMinXValue() / (clusters.length + 1) * n) + getMinXValue(); cy = (getMaxYValue() - getMinYValue() / (clusters.length + 1) * n) + getMinYValue(); Centroid c1 = new Centroid(cx, cy); clusters
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人工智能产品经理招聘面试参考题库及答案
- 2025年信息技术分析师招聘面试题库及参考答案
- 2025年收藏品评估师招聘面试题库及参考答案
- 2025年风电项目经理招聘面试参考题库及答案
- 2025年月度营销经理招聘面试题库及参考答案
- 中国铁路单招题库及答案
- 2025年信贷审核专员招聘面试题库及参考答案
- 考护士执业证题库及答案
- 2025年网页运营专员招聘面试题库及参考答案
- 2025年企业财务专员招聘面试题库及参考答案
- 公证财产协议书范本
- 2024年锦州辅警招聘考试真题附答案详解(综合卷)
- 2025年高校教师资格证之高等教育学测试卷附答案
- 2025-2026学年高二上学期《如何引导高中生“碳索绿色未来”培养环保意识》主题班会课件
- 北师大版数学七年级上册期中综合能力测评卷(含解析)
- 农业经理人考试题库四级及答案
- 门面反恐应急预案
- 《移动电源车运维管理技术规范(柴油机式)》
- 出租注册地址合同范本
- DB32∕T 4700-2024 蓄热式焚烧炉系统安全技术要求
- 2025年陕西省行政执法证考试题库附答案
评论
0/150
提交评论