模式识别----kmean算法(java实现)
来源:互联网 发布:汽车单片机是什么意思 编辑:程序博客网 时间:2024/06/14 10:43
import java.io.*;import java.text.DecimalFormat;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;public class TestKmean { public static void main(String[] args) throws IOException { Kmean kmean = new Kmean(6); kmean.addData(new File("wine.txt")); kmean.randomCenter(); kmean.calCenter(); kmean.classifier(); kmean.cal(); kmean.calCenter(); kmean.classifier(); kmean.print(); } public static void printD(double[] d){ for(int i=0;i<d.length;i++){ System.out.print(d[i]+" "); } }}class Kmean{ int k; LinkedList<Wine> all = new LinkedList(); LinkedList<Wine> center = new LinkedList<>(); HashMap<Integer,LinkedList<Wine>> result =new HashMap<>(); boolean stop=false; Kmean(int _k){ this.k=_k; } public void randomCenter(){ int index=0; Iterator it = all.iterator(); while(index<k&&it.hasNext()){ Wine w = (Wine) it.next(); if(Math.random()>0.5){ center.add(w); result.put(w.id,new LinkedList<>()); index++; } } } public void addData(File file) throws IOException { BufferedReader br = new BufferedReader(new FileReader(file)); String s; int index=1; while((s=br.readLine())!=null){ String[] c = s.split(","); double[] d = new double[13]; for(int i=0;i<c.length;i++){ if(i!=0){ d[i-1]= Double.parseDouble(c[i]); } } Wine wine = new Wine(d,index); all.add(wine); index++; } } public void print(){ Iterator<Wine> it = center.iterator(); Wine w; Wine w1; while(it.hasNext()){ w = it.next(); System.out.print("第"+w.id+"类:"); Iterator<Wine> its=result.get(w.id).iterator(); while(its.hasNext()){ w1=its.next(); System.out.print(w1.id+","); } System.out.println(); } } public void classifier(){ Iterator<Wine> it = center.iterator(); Wine w1; while(it.hasNext()){ Iterator<Wine> its = all.iterator(); int centerId=it.next().id; while(its.hasNext()){ w1 = its.next(); int allId=w1.center.id; if(centerId==allId){ result.get(centerId).add(w1); } } } } public void cal(){ Iterator<Wine> it = center.iterator(); int sum=0; while(it.hasNext()){ int index=0; Wine w1=it.next(); double[] d=calAvg(result.get(w1.id),result.get(w1.id).size()); for(int a=0;a<13;a++){ if(w1.data[a]==d[a]){ index++; }else{ w1.data[a]=d[a]; } } if(index==13){ sum++; } } if(sum==k){ stop=true; } } private double[] calAvg(LinkedList<Wine> wines,int a) { DecimalFormat df = new DecimalFormat("###0.00"); Iterator<Wine> it = wines.iterator(); double[] sum=new double[13]; while(it.hasNext()){ Wine w = it.next(); for(int i=0;i<w.data.length;i++){ sum[i]+=w.data[i]; } } double[] avg=new double[13]; for(int i=0;i<13;i++){ avg[i] = Double.parseDouble(df.format(((sum[i])/a))); } return avg; } public void calCenter(){ Iterator<Wine> its = all.iterator(); Wine ce=null; Wine wi; double min=0.0; int index=0; while(its.hasNext()){ wi=its.next(); Iterator<Wine> it = center.iterator(); while(it.hasNext()){ double distance=0.0; Wine wine1 = it.next(); for(int i=0;i<13;i++){ double temp = (wi.data[i]-wine1.data[i])*(wi.data[i]-wine1.data[i]); distance += temp; } double result = Math.sqrt(distance); if(index==0){ min=result; ce=wine1; }else if(result<min){ min =result; ce=wine1; } index++; } wi.setCenter(ce); index=0; } }}class Wine{ double[] data; int id; Wine center=null; Wine(double[] d,int i){ data=d; id=i; } public void setCenter(Wine wine){ center=wine; }}
阅读全文
0 0
- 模式识别----kmean算法(java实现)
- kmean算法python实现
- 模式识别-----knn算法(java实现)
- Kmean算法
- Kmean算法
- OpenCV中kmean算法的实现
- 模式识别-----贝叶斯算法(java实现)
- 模式识别ID3算法实现
- streaming kmean cluster 聚类算法实现和分析
- kmean算法理解
- java 编写mllib kmean算子
- canopy算法优化kmean聚类及在mahout上实现
- 模式识别中K-means算法实现
- 模式识别几何分类算法实现(一)
- 模式识别几何分类算法实现(二)
- 势函数算法实现模式识别算法实现C++
- KMean clustering算法获取图片主色调
- KMean聚类算法及其改进
- 腾讯地图的使用,添加多个标记和点击事件
- RHEL7及CentOS7中让指定用户才能su到root-系统管理(3)
- 排序二叉树的c++实现
- 《getting started with p5.js》(中文版) 第二章 开始写代码:创建你的第一个p5.js程序
- 揭秘深度学习成功的数学原因:从全局最优性到学习表征不变性
- 模式识别----kmean算法(java实现)
- ip地址、域名、DNS、URL(即网址)的区别与联系
- Windows下面搭建在线文档工具showdoc工具
- 进程控制总结
- Nginx源码安装(完结)
- 数据挖掘学习------------------4-分类方法-1-基本概念
- ubuntu14.04部署samba
- 大新闻!微软正考虑添加 Python 为官方的 Excel 脚本语言
- 趣图:我司的开发进程是这样子的