k-均值算法的java实现

 

  1. import java.io.BufferedReader;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.FileReader;   
  4. import java.io.IOException;   
  5.   
  6. public class KAverage {   
  7.     private int sampleCount = 0;   
  8.     private int dimensionCount = 0;   
  9.     private int centerCount = 0;   
  10.     private double[][] sampleValues;   
  11.     private double[][] centers;   
  12.     private double[][] tmpCenters;   
  13.     private String dataFile = "";   
  14.   
  15.     /**  
  16.      * 通过构造器传人数据文件  
  17.      */  
  18.     public KAverage(String dataFile) throws NumberInvalieException {   
  19.         this.dataFile = dataFile;   
  20.     }   
  21.   
  22.     /**  
  23.      * 第一行为s;d;c含义分别为样例的数目,每个样例特征的维数,聚类中心个数 文件格式为d[,d]...;d[,d]... 如:1,2;2,3;1,5  
  24.      * 每一维之间用,隔开,每个样例间用;隔开。结尾没有';' 可以有多行  
  25.      */  
  26.   
  27.     private int initData(String fileName) {   
  28.         String line;   
  29.         String samplesValue[];   
  30.         String dimensionsValue[] = new String[dimensionCount];   
  31.         BufferedReader in;   
  32.         try {   
  33.             in = new BufferedReader(new FileReader(fileName));   
  34.         } catch (FileNotFoundException e) {   
  35.             e.printStackTrace();   
  36.             return -1;   
  37.         }   
  38.         /*  
  39.          * 预处理样本,允许后面几维为0时,不写入文件  
  40.          */  
  41.         for (int i = 0; i < sampleCount; i++) {   
  42.             for (int j = 0; j < dimensionCount; j++) {   
  43.                 sampleValues[i][j] = 0;   
  44.             }   
  45.         }   
  46.   
  47.         int i = 0;   
  48.         double tmpValue = 0.0;   
  49.         try {   
  50.             line = in.readLine();   
  51.             String params[] = line.split(";");   
  52.             if (params.length != 3) { // 必须为3个参数,否则错误   
  53.                 return -1;   
  54.             }   
  55.             /**  
  56.              * 获取参数  
  57.              */  
  58.             this.sampleCount = Integer.parseInt(params[0]);   
  59.             this.dimensionCount = Integer.parseInt(params[1]);   
  60.             this.centerCount = Integer.parseInt(params[2]);   
  61.             if (sampleCount <= 0 || dimensionCount <= 0 || centerCount <= 0) {   
  62.                 throw new NumberInvalieException("input number <= 0.");   
  63.             }   
  64.             if (sampleCount < centerCount) {   
  65.                 throw new NumberInvalieException(   
  66.                         "sample number < center number");   
  67.             }   
  68.   
  69.             sampleValues = new double[sampleCount][dimensionCount + 1];   
  70.             centers = new double[centerCount][dimensionCount];   
  71.             tmpCenters = new double[centerCount][dimensionCount];   
  72.   
  73.             while ((line = in.readLine()) != 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值