数据挖掘-基于Kmeans算法、MBSAS算法及DBSCAN算法的newsgroup18828文本聚类器的JAVA实现(上)

(update 2012.12.28 关于本项目下载及运行的常见问题 FAQ见 newsgroup18828文本分类器、文本聚类器、关联分析频繁模式挖掘算法的Java实现工程下载及运行FAQ )

本文要点如下:

对newsgroup文档集进行预处理,按照DF法及SVD分解法抽取特征词,实现降维

实现了K-Means,MBSAS,DBSCAN三种聚类算法
用weka工具进行newsgroup文档聚类

计算各种算法聚类的熵,进行算法评价

1、newsgroup文档集预处理

      newsgroup是常用的数据挖掘实验数据。文本预处理主要包括单词分片、去除标点等无关符号、去停用词等等,相关详细介绍见我的另一篇博文数据挖掘-基于贝叶斯算法及KNN算法的newsgroup18828文本分类器的JAVA实现(上),此处只给出文本预处理和向量化不同的部分代码。

文本预处理类DataPreProcess.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.pku.yangliu;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.util.ArrayList;  
  9.   
  10. /**  
  11.  * Newsgroups文档集预处理类 
  12.  */  
  13. public class DataPreProcess {  
  14.       
  15.     /**输入文件调用处理数据函数 
  16.      * @param strDir newsgroup文件目录的绝对路径 
  17.      * @throws IOException  
  18.      */  
  19.     public void doProcess(String strDir) throws IOException{  
  20.         File fileDir = new File(strDir);  
  21.         if(!fileDir.exists()){  
  22.             System.out.println("File not exist:" + strDir);  
  23.             return;  
  24.         }  
  25.         String subStrDir = strDir.substring(strDir.lastIndexOf('/'));  
  26.         String dirTarget = strDir + "/../../processedSample_includeNotSpecial"+subStrDir;  
  27.         File fileTarget = new File(dirTarget);  
  28.         if(!fileTarget.exists()){//注意processedSample需要先建立目录建出来,否则会报错,因为母目录不存在  
  29.             fileTarget.mkdir();  
  30.         }  
  31.         File[] srcFiles = fileDir.listFiles();  
  32.         String[] stemFileNames = new String[srcFiles.length];  
  33.         for(int i = 0; i < srcFiles.length; i++){  
  34.             String fileFullName = srcFiles[i].getCanonicalPath();  
  35.             String fileShortName = srcFiles[i].getName();  
  36.             if(!new File(fileFullName).isDirectory()){//确认子文件名不是目录如果是可以再次递归调用  
  37.                 System.out.println("Begin preprocess:"+fileFullName);  
  38.                 StringBuilder stringBuilder = new StringBuilder();  
  39.                 stringBuilder.append(dirTarget + "/" + fileShortName);  
  40.                 createProcessFile(fileFullName, stringBuilder.toString());  
  41.                 stemFileNames[i] = stringBuilder.toString();  
  42.             }  
  43.             else {  
  44.                 fileFullName = fileFullName.replace("\\","/");  
  45.                 doProcess(fileFullName);  
  46.             }  
  47.         }  
  48.         //下面调用stem算法  
  49.         if(stemFileNames.length > 0 && stemFileNames[0] != null){  
  50.             Stemmer.porterMain(stemFileNames);  
  51.         }  
  52.     }  
  53.       
  54.     /**进行文本预处理生成目标文件 
  55.      * @param srcDir 源文件文件目录的绝对路径 
  56.      * @param targetDir 生成的目标文件的绝对路径 
  57.      * @throws IOException  
  58.      */  
  59.     private static void createProcessFile(String srcDir, String targetDir) throws IOException {  
  60.         // TODO Auto-generated method stub  
  61.         FileReader srcFileReader = new FileReader(srcDir);  
  62.         FileReader stopWordsReader = new FileReader("F:/DataMiningSample/stopwords.txt");  
  63.         FileWriter targetFileWriter = new FileWriter(targetDir);      
  64.         BufferedReader srcFileBR = new BufferedReader(srcFileReader);//装饰模式  
  65.         BufferedReader stopWordsBR = new BufferedReader(stopWordsReader);  
  66.         String line, resLine, stopWordsLine;  
  67.         //用stopWordsBR够着停用词的ArrayList容器  
  68.         ArrayList<String> stopWordsArray = new ArrayList<String>();  
  69.         while((stopWordsLine = stopWordsBR.readLine()) != null){  
  70.             if(!stopWordsLine.isEmpty()){  
  71.                 stopWordsArray.add(stopWordsLine);  
  72.             }  
  73.         }  
  74.         while((line = srcFileBR.readLine()) != null){  
  75.             resLine = lineProcess(line,stopWordsArray);  
  76.             if(!resLine.isEmpty()){  
  77.                 //按行写,一行写一个单词  
  78.                 String[] tempStr = resLine.split(" ");//\s  
  79.                 for(int i = 0; i < tempStr.length; i++){  
  80.                     if(!tempStr[i].isEmpty()){  
  81.                         targetFileWriter.append(tempStr[i]+"\n");  
  82.                     }  
  83.                 }  
  84.             }  
  85.         }  
  86.         targetFileWriter.flush();  
  87.         targetFileWriter.close();  
  88.         srcFileReader.close();  
  89.         stopWordsReader.close();  
  90.         srcFileBR.close();  
  91.         stopWordsBR.close();      
  92.     }  
  93.       
  94.     /**对每行字符串进行处理,主要是词法分析、去停用词和stemming 
  95.      * @param line 待处理的一行字符串 
  96.      * @param ArrayList<String> 停用词数组 
  97.      * @return String 处理好的一行字符串,是由处理好的单词重新生成,以空格为分隔符 
  98.      * @throws IOException  
  99.      */  
  100.     private static String lineProcess(String line, ArrayList<String> stopWordsArray) throws IOException {  
  101.         // TODO Auto-generated method stub  
  102.         //step1 英文词法分析,去除数字、连字符、标点符号、特殊字符,所有大写字母转换成小写,可以考虑用正则表达式  
  103.         String res[] = line.split("[^a-zA-Z]");  
  104.         //这里要小心,防止把有单词中间有数字和连字符的单词 截断了,但是截断也没事  
  105.         String resString = new String();  
  106.         //step2去停用词  
  107.         //step3stemming,返回后一起做  
  108.         for(int i = 0; i < res.length; i++){  
  109.             if(!res[i].isEmpty() && !stopWordsArray.contains(res[i].toLowerCase())){  
  110.                 resString += " " + res[i].toLowerCase() + " ";  
  111.             }  
  112.         }  
  113.         return resString;  
  114.     }  
  115.   
  116.     /** 
  117.      * @param args 
  118.      * @throws IOException  
  119.      */  
  120.     public void BPPMain(String[] args) throws IOException {  
  121.         // TODO Auto-generated method stub  
  122.         DataPreProcess dataPrePro = new DataPreProcess();  
  123.         dataPrePro.doProcess("F:/DataMiningSample/orginSample");  
  124.   
  125.     }  
  126.   
  127. }  
文本向量化表示主要基于TF-IDF值  ComputeWordsVector.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.pku.yangliu;  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.util.HashSet;  
  8. import java.util.SortedMap;  
  9. import java.util.Map;  
  10. import java.util.Set;  
  11. import java.util.SortedSet;  
  12. import java.util.TreeMap;  
  13. import java.util.Iterator;  
  14. import java.util.TreeSet;  
  15.   
  16. /**计算文档的属性向量,将所有文档向量化 
  17.  * 
  18.  */  
  19. public class ComputeWordsVector {  
  20.       
  21.     /**计算文档的TF-IDF属性向量,返回Map<文件名,Map<特征词,TF-IDF值>> 
  22.      * @param testSampleDir 处理好的聚类样本测试样例集合 
  23.      * @return Map<String,Map<String,Double>> 所有测试样例的属性向量构成的map 
  24.      * @throws IOException  
  25.      */  
  26.     public Map<String,Map<String,Double>> computeTFMultiIDF(String testSampleDir) throws IOException{  
  27.         String word;  
  28.         Map<String,Map<String,Double>> allTestSampleMap = new TreeMap<String,Map<String,Double>>();  
  29.         Map<String, Double> idfPerWordMap = computeIDF(testSampleDir);  
  30.         Map<String,Double> TFPerDocMap = new TreeMap<String,Double>();//计算每篇文档中含有各特征词数量  
  31.         File[] samples = new File(testSampleDir).listFiles();  
  32.         System.out.println("the total number of test files is" + samples.length);  
  33.         for(int i = 0; i < samples.length; i++){  
  34.             TFPerDocMap.clear();  
  35.             FileReader samReader = new FileReader(samples[i]);  
  36.             BufferedReader samBR = new BufferedReader(samReader);  
  37.             Double wordSumPerDoc = 0.0;//计算每篇文档的总词数  
  38.             while((word = samBR.readLine()) != null){  
  39.                 if(!word.isEmpty()){  
  40.                     wordSumPerDoc++;  
  41.                     if(TFPerDocMap.containsKey(word)){  
  42.                         Double count =  TFPerDocMap.get(word);  
  43.                         TFPerDocMap.put(word, count + 1.0);  
  44.                     }  
  45.                     else {  
  46.                         TFPerDocMap.put(word, 1.0);  
  47.                     }  
  48.                 }  
  49.             }  
  50.   
  51.             Double maxCount = 0.0, wordWeight;//记录出现次数最多的词出现的次数,用做归一化  
  52.             Set<Map.Entry<String, Double>> tempTF = TFPerDocMap.entrySet();  
  53.             for(Iterator<Map.Entry<String, Double>> mt = tempTF.iterator(); mt.hasNext();){  
  54.                 Map.Entry<String, Double> me = mt.next();  
  55.                 if(me.getValue() > maxCount) maxCount = me.getValue();  
  56.             }  
  57.             for(Iterator<Map.Entry<String, Double>> mt = tempTF.iterator(); mt.hasNext();){  
  58.                 Map.Entry<String, Double> me = mt.next();  
  59.                 Double IDF = Math.log(samples.length / idfPerWordMap.get(me.getKey())) / Math.log(10);  
  60.                 wordWeight =  (me.getValue() / maxCount) * IDF;  
  61.                 TFPerDocMap.put(me.getKey(), wordWeight);  
  62.             }  
  63.             TreeMap<String,Double> tempMap = new TreeMap<String,Double>();  
  64.             tempMap.putAll(TFPerDocMap);  
  65.             allTestSampleMap.put(samples[i].getName(), tempMap);  
  66.         }  
  67.         //printTestSampleMap(allTestSampleMap);  
  68.         return allTestSampleMap;  
  69.     }  
  70.       
  71.     /**输出测试样例map内容,用于测试 
  72.      * @param SortedMap<String,Double> 属性词典 
  73.      * @throws IOException  
  74.      */  
  75.     void printTestSampleMap(Map<String,Map<String,Double>> allTestSampleMap) throws IOException {  
  76.         // TODO Auto-generated method stub  
  77.         File outPutFile = new File("F:/DataMiningSample/KmeansClusterResult/allTestSampleMap.txt");  
  78.         FileWriter outPutFileWriter = new FileWriter(outPutFile);  
  79.         Set<Map.Entry<String,Map<String,Double>>> allWords = allTestSampleMap.entrySet();  
  80.         for(Iterator<Map.Entry<String,Map<String,Double>>> it = allWords.iterator(); it.hasNext();){  
  81.             Map.Entry<String,Map<String,Double>> me = it.next();  
  82.             outPutFileWriter.append(me.getKey() + " ");  
  83.             Set<Map.Entry<String,Double>> vecSet = me.getValue().entrySet();  
  84.             for(Iterator<Map.Entry<String, Double>> jt = vecSet.iterator(); jt.hasNext();){  
  85.                 Map.Entry<String, Double> ne = jt.next();  
  86.                 outPutFileWriter.append(ne.getKey() + " "+ ne.getValue() + " ");  
  87.             }  
  88.             outPutFileWriter.append("\n");  
  89.             outPutFileWriter.flush();  
  90.         }  
  91.         outPutFileWriter.close();  
  92.     }  
  93.       
  94.     /**统计每个词的总的出现次数,返回出现次数大于n次的词汇构成最终的属性词典 
  95.      * @param strDir 处理好的newsgroup文件目录的绝对路径 
  96.      * @throws IOException  
  97.      */  
  98.     public SortedMap<String,Double> countWords(String strDir,Map<String, Double> wordMap) throws IOException{  
  99.         File sampleFile = new File(strDir);  
  100.         File [] sampleDir = sampleFile.listFiles();  
  101.         String word;  
  102.         for(int j = 0; j < sampleDir.length; j++){  
  103.             File[] sample = sampleDir[j].listFiles();  
  104.             for(int i = 0; i < sample.length; i++){  
  105.                 if(sample[i].getName().contains("stemed")){  
  106.                     FileReader samReader = new FileReader(sample[i]);  
  107.                     BufferedReader samBR = new BufferedReader(samReader);  
  108.                     while((word = samBR.readLine()) != null){  
  109.                         if(!word.isEmpty() && wordMap.containsKey(word)){  
  110.                             double count = wordMap.get(word) + 1;  
  111.                             wordMap.put(word, count);  
  112.                         }  
  113.                         else {  
  114.                             wordMap.put(word, 1.0);  
  115.                         }  
  116.                     }  
  117.                 }     
  118.             }  
  119.         }  
  120.       
  121.         //去除停用词后,先用DF法选取特征词,后面再加入特征词的选取算法  
  122.         SortedMap<String,Double> newWordMap = new TreeMap<String,Double>();  
  123.         Set<Map.Entry<String,Double>> allWords = wordMap.entrySet();  
  124.         for(Iterator<Map.Entry<String,Double>> it = allWords.iterator(); it.hasNext();){  
  125.             Map.Entry<String, Double> me = it.next();  
  126.             if(me.getValue() > 100){//DF法降维  
  127.                 newWordMap.put(me.getKey(),me.getValue());  
  128.             }  
  129.         }  
  130.         return newWordMap;    
  131.     }  
  132.   
  133.     /**计算IDF,即属性词典中每个词在多少个文档中出现过 
  134.      * @param testSampleDir 聚类算法测试样本所在目录 
  135.      * @return 单词的IDFmap 格式为SortedMap<String,Double> 即<单词,包含该单词的文档数> 
  136.      * @throws IOException  
  137.      */  
  138.     Map<String,Double> computeIDF(String testSampleDir) throws IOException {  
  139.         // TODO Auto-generated method stub  
  140.         Map<String,Double> IDFPerWordMap = new TreeMap<String,Double>();  
  141.         Set<String> alreadyCountWord = new HashSet<String>();//记下当前已经遇到过的该文档中的词  
  142.         String word;  
  143.         File[] samples = new File(testSampleDir).listFiles();  
  144.         for(int i = 0; i < samples.length; i++){  
  145.             alreadyCountWord.clear();  
  146.             FileReader tsReader = new FileReader(samples[i]);  
  147.             BufferedReader tsBR = new BufferedReader(tsReader);  
  148.             while((word = tsBR.readLine()) != null){  
  149.                 if(!alreadyCountWord.contains(word)){  
  150.                     if(IDFPerWordMap.containsKey(word)){  
  151.                         IDFPerWordMap.put(word, IDFPerWordMap.get(word) + 1.0);  
  152.                     }  
  153.                     else IDFPerWordMap.put(word, 1.0);  
  154.                     alreadyCountWord.add(word);                   
  155.                 }  
  156.             }  
  157.         }  
  158.         return IDFPerWordMap;  
  159.     }  
  160.       
  161.     /**创建聚类算法的测试样例集,主要是过滤出只含有特征词的文档写到一个目录下 
  162.      * @param String srcDir 源目录,已经经过预处理但还没有过滤非特征词的文档目录 
  163.      * @param String destDir 目的目录,聚类算法的测试样例目录 
  164.      * @return String[] 创建测试样例集中特征词数组 
  165.      * @throws IOException  
  166.      */  
  167.     String[] createTestSamples( String srcDir, String destDir) throws IOException {  
  168.         // TODO Auto-generated method stub  
  169.         SortedMap<String,Double> wordMap = new TreeMap<String,Double>();  
  170.         wordMap = countWords(srcDir, wordMap);  
  171.         System.out.println("special words map sizes:" + wordMap.size());  
  172.         String word, testSampleFile;  
  173.         File[] sampleDir = new File(srcDir).listFiles();  
  174.         for(int i = 0; i < sampleDir.length; i++){  
  175.             File[] sample = sampleDir[i].listFiles();  
  176.             for(int j = 0;j < sample.length; j++){     
  177.                 if(sample[j].getName().contains("stemed")){  
  178.                     testSampleFile = destDir + sampleDir[i].getName()+"_"+sample[j].getName();  
  179.                     FileReader samReader = new FileReader(sample[j]);  
  180.                     BufferedReader samBR = new BufferedReader(samReader);  
  181.                     FileWriter tsWriter = new FileWriter(new File(testSampleFile));  
  182.                     while((word = samBR.readLine()) != null){  
  183.                         if(wordMap.containsKey(word)){  
  184.                             tsWriter.append(word + "\n");  
  185.                         }  
  186.                     }  
  187.                     tsWriter.flush();  
  188.                     tsWriter.close();     
  189.                 }  
  190.             }  
  191.         }  
  192.         //返回属性词典  
  193.         String [] terms = new String[wordMap.size()];  
  194.         int i = 0;  
  195.         Set<Map.Entry<String,Double>> allWords = wordMap.entrySet();  
  196.         for(Iterator<Map.Entry<String,Double>> it = allWords.iterator(); it.hasNext();){  
  197.             Map.Entry<String, Double> me = it.next();  
  198.             terms[i] = me.getKey();  
  199.             i++;  
  200.         }  
  201.         return terms;  
  202.     }  
  203.       
  204.     /**评估函数根据聚类结果文件统计熵和混淆矩阵 
  205.      * @param clusterResultFile 聚类结果文件 
  206.      * @param K 聚类数目 
  207.      * @return double 聚类结果的熵值 
  208.      * @throws IOException  
  209.      */  
  210.     double evaluateClusterRes(String clusterResultFile, int K) throws IOException {  
  211.         // TODO Auto-generated method stub  
  212.         Map<String,String> rightCate = new TreeMap<String,String>();  
  213.         Map<String,String> resultCate = new TreeMap<String,String>();  
  214.         FileReader crReader = new FileReader(clusterResultFile);  
  215.         BufferedReader crBR = new BufferedReader(crReader);  
  216.         String[] s;  
  217.         String line;  
  218.         while((line = crBR.readLine()) != null){  
  219.             s = line.split(" ");  
  220.             resultCate.put(s[0], s[1]);   
  221.             //再把s[0]用_分片  
  222.             rightCate.put(s[0], s[0].split("_")[0]);  
  223.         }  
  224.         return computeEntropyAndConfuMatrix(rightCate,resultCate,K);//返回熵  
  225.     }  
  226.       
  227.     /**计算混淆矩阵并且输出,返回熵 
  228.      * @param rightCate 正确类目对应map 
  229.      * @param resultCate 聚类结果对应map 
  230.      * @return double 返回聚类的熵 
  231.      * @throws IOException  
  232.      */  
  233.     private double computeEntropyAndConfuMatrix(Map<String, String> rightCate,  
  234.             Map<String, String> resultCate, int K) {  
  235.         // TODO Auto-generated method stub    
  236.         int[][] confusionMatrix = new int[K][20];//K行20列,[i,j]表示聚类i中属于类目j的文件数  
  237.         //首先求出类目对应的数组索引  
  238.         SortedSet<String> cateNames = new TreeSet<String>();  
  239.         Set<Map.Entry<String, String>> rightCateSet = rightCate.entrySet();  
  240.         for(Iterator<Map.Entry<String, String>> it = rightCateSet.iterator(); it.hasNext();){  
  241.             Map.Entry<String, String> me = it.next();  
  242.             cateNames.add(me.getValue());  
  243.         }  
  244.         String[] cateNamesArray = cateNames.toArray(new String[0]);  
  245.         Map<String,Integer> cateNamesToIndex = new TreeMap<String,Integer>();  
  246.         for(int i = 0; i < cateNamesArray.length; i++){  
  247.             cateNamesToIndex.put(cateNamesArray[i],i);  
  248.         }  
  249.         for(Iterator<Map.Entry<String, String>> it = rightCateSet.iterator(); it.hasNext();){  
  250.             Map.Entry<String, String> me = it.next();  
  251.             confusionMatrix[Integer.parseInt(resultCate.get(me.getKey()))][cateNamesToIndex.get(me.getValue())]++;  
  252.         }  
  253.         //输出混淆矩阵  
  254.         double [] clusterSum = new double[K];//记录每个聚类的文件数  
  255.         double[] everyClusterEntropy = new double[K];//记录每个聚类的熵  
  256.         double clusterEntropy = 0;  
  257.         System.out.print("    ");  
  258.         for(int i = 0; i < 20; i++){  
  259.             System.out.print(i + "    ");  
  260.         }  
  261.         System.out.println();  
  262.         for(int i = 0; i < K; i++){  
  263.             System.out.print(i + "    ");  
  264.             for(int j = 0; j < 20; j++){  
  265.                 clusterSum[i] += confusionMatrix[i][j];  
  266.                 System.out.print(confusionMatrix[i][j]+"    ");  
  267.             }  
  268.             System.out.println();  
  269.         }  
  270.         System.out.println();  
  271.         for(int i = 0; i < K; i++){  
  272.             if(clusterSum[i] != 0){  
  273.                 for(int j = 0; j < 20; j++){  
  274.                      double p = (double)confusionMatrix[i][j]/clusterSum[i];  
  275.                      if(p != 0){  
  276.                          everyClusterEntropy[i] += -p * Math.log(p);  
  277.                      }  
  278.                 }  
  279.                 clusterEntropy += clusterSum[i]/(double)rightCate.size() * everyClusterEntropy[i];  
  280.             }  
  281.         }  
  282.         return clusterEntropy;  
  283.     }  
  284.   
  285. }  

2、K-means算法

       K-means算法是非常经典的聚类算法。其算法思路是: 先选K个初始聚类点作为初始中心点,然后计算其他所有点到K个聚类点的距离做聚类,将点分到最近的聚类,聚完类后中心点发生变化了,于是更新中心点。然后再计算其他所有点到这K个中心点的距离重新聚类,中心点又会发生变化,如此迭代下去。其伪代码如下:

     K-means算法的实现有以下关键点:

    初始点的选择策略:随机选、均匀抽样、最大最小法等
    距离的度量  1-余弦相似度,欧式距离,1-向量内积,测试发现1-余弦相似度效果最好,而1-向量内积速度最快。
    中心点的计算  向量各维取评价
    算法停止条件   计算准则函数及设置最大迭代次数
    空聚类的处理  注意空聚类导致的程序bug

    K-means算法实现类KmeansCluster.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.pku.yangliu;  
  2. import java.io.FileWriter;  
  3. import java.io.IOException;  
  4. import java.util.Iterator;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7. import java.util.TreeMap;  
  8. import java.util.Vector;  
  9.   
  10. /**Kmeans聚类算法的实现类,将newsgroups文档集聚成10类、20类、30类 
  11.  * 算法结束条件:当每个点最近的聚类中心点就是它所属的聚类中心点时,算法结束 
  12.  * 
  13.  */  
  14.   
  15. public class KmeansCluster {  
  16.       
  17.     /**Kmeans算法主过程 
  18.      * @param Map<String, Map<String, Double>> allTestSampleMap 聚类算法测试样本map 
  19.      * @param int K 聚类的数量 
  20.      * @return Map<String,Integer> 聚类的结果  即<文件名,聚类完成后所属的类别标号> 
  21.      * @throws IOException  
  22.      */  
  23.     private Map<String, Integer> doProcess(  
  24.             Map<String, Map<String, Double>> allTestSampleMap, int K) {  
  25.         // TODO Auto-generated method stub  
  26.         //0、首先获取allTestSampleMap所有文件名顺序组成的数组  
  27.         String[] testSampleNames = new String[allTestSampleMap.size()];  
  28.         int count = 0, tsLength = allTestSampleMap.size();  
  29.         Set<Map.Entry<String, Map<String, Double>>> allTestSampeleMapSet = allTestSampleMap.entrySet();  
  30.         for(Iterator<Map.Entry<String, Map<String, Double>>> it = allTestSampeleMapSet.iterator(); it.hasNext(); ){  
  31.             Map.Entry<String, Map<String, Double>> me = it.next();  
  32.             testSampleNames[count++] = me.getKey();  
  33.         }  
  34.         //1、初始点的选择算法是随机选择或者是均匀分开选择,这里采用后者  
  35.         Map<Integer, Map<String, Double>> meansMap = getInitPoint(allTestSampleMap, K);//保存K个中心点  
  36.         double [][] distance = new double[tsLength][K];//distance[i][j]记录点i到聚类中心j的距离  
  37.         //2、初始化K个聚类  
  38.         int [] assignMeans = new int[tsLength];//记录所有点属于的聚类序号,初始化全部为0  
  39.         Map<Integer, Vector<Integer>> clusterMember = new TreeMap<Integer,Vector<Integer>>();//记录每个聚类的成员点序号  
  40.         Vector<Integer> mem = new Vector<Integer>();  
  41.         int iterNum = 0;//迭代次数  
  42.         while(true){  
  43.             System.out.println("Iteration No." + (iterNum++) + "----------------------");  
  44.             //3、计算每个点和每个聚类中心的距离  
  45.             for(int i = 0; i < tsLength; i++){  
  46.                 for(int j = 0; j < K; j++){  
  47.                     distance[i][j] = getDistance(allTestSampleMap.get(testSampleNames[i]),meansMap.get(j));  
  48.                 }  
  49.             }  
  50.             //4、找出每个点最近的聚类中心  
  51.             int[] nearestMeans = new int[tsLength];  
  52.             for(int i = 0; i < tsLength; i++){  
  53.                 nearestMeans[i] = findNearestMeans(distance, i);  
  54.             }  
  55.             //5、判断当前所有点属于的聚类序号是否已经全部是其离得最近的聚类,如果是或者达到最大的迭代次数,那么结束算法  
  56.             int okCount = 0;  
  57.             for(int i = 0; i <tsLength; i++){  
  58.                 if(nearestMeans[i] == assignMeans[i]) okCount++;  
  59.             }  
  60.             System.out.println("okCount = " + okCount);  
  61.             if(okCount == tsLength || iterNum >= 10break;  
  62.             //6、如果前面条件不满足,那么需要重新聚类再进行一次迭代,需要修改每个聚类的成员和每个点属于的聚类信息  
  63.             clusterMember.clear();  
  64.             for(int i = 0; i < tsLength; i++){  
  65.                 assignMeans[i] = nearestMeans[i];  
  66.                 if(clusterMember.containsKey(nearestMeans[i])){  
  67.                     clusterMember.get(nearestMeans[i]).add(i);    
  68.                 }  
  69.                 else {  
  70.                     mem.clear();  
  71.                     mem.add(i);  
  72.                     Vector<Integer> tempMem = new Vector<Integer>();  
  73.                     tempMem.addAll(mem);  
  74.                     clusterMember.put(nearestMeans[i], tempMem);  
  75.                 }  
  76.             }  
  77.             //7、重新计算每个聚类的中心点!  
  78.             for(int i = 0; i < K; i++){  
  79.                 if(!clusterMember.containsKey(i)){//注意kmeans可能产生空聚类  
  80.                     continue;  
  81.                 }  
  82.                 Map<String, Double> newMean = computeNewMean(clusterMember.get(i), allTestSampleMap, testSampleNames);  
  83.                 Map<String, Double> tempMean = new TreeMap<String, Double>();  
  84.                 tempMean.putAll(newMean);  
  85.                 meansMap.put(i, tempMean);  
  86.             }  
  87.         }  
  88.         //8、形成聚类结果并且返回  
  89.         Map<String, Integer> resMap = new TreeMap<String, Integer>();  
  90.         for(int i = 0; i < tsLength; i++){  
  91.             resMap.put(testSampleNames[i], assignMeans[i]);  
  92.         }  
  93.         return resMap;  
  94.     }  
  95.   
  96.     /**计算当前聚类新的中心,采用向量平均 
  97.      * @param clusterM 该点到所有聚类中心的距离 
  98.      * @param allTestSampleMap 所有测试样例的<文件名,向量>构成的map 
  99.      * @param testSampleNames 所有测试样例文件名构成的数组 
  100.      * @return Map<String, Double> 新的聚类中心的向量 
  101.      * @throws IOException  
  102.      */  
  103.     private Map<String, Double> computeNewMean(Vector<Integer> clusterM,  
  104.             Map<String, Map<String, Double>> allTestSampleMap,  
  105.             String[] testSampleNames) {  
  106.         // TODO Auto-generated method stub  
  107.         double memberNum = (double)clusterM.size();  
  108.         Map<String, Double> newMeanMap = new TreeMap<String,Double>();  
  109.         Map<String, Double> currentMemMap = new TreeMap<String,Double>();  
  110.         for(Iterator<Integer> it = clusterM.iterator(); it.hasNext();){  
  111.             int me = it.next();  
  112.             currentMemMap = allTestSampleMap.get(testSampleNames[me]);  
  113.             Set<Map.Entry<String, Double>> currentMemMapSet = currentMemMap.entrySet();  
  114.             for(Iterator<Map.Entry<String, Double>> jt = currentMemMapSet.iterator(); jt.hasNext();){  
  115.                 Map.Entry<String, Double> ne = jt.next();  
  116.                 if(newMeanMap.containsKey(ne.getKey())){  
  117.                     newMeanMap.put(ne.getKey(), newMeanMap.get(ne.getKey()) + ne.getValue());  
  118.                 }   
  119.                 else {  
  120.                     newMeanMap.put(ne.getKey(), ne.getValue());  
  121.                 }  
  122.             }  
  123.         }  
  124.           
  125.         Set<Map.Entry<String, Double>> newMeanMapSet = newMeanMap.entrySet();  
  126.             for(Iterator<Map.Entry<String, Double>> jt = newMeanMapSet.iterator(); jt.hasNext();){  
  127.                 Map.Entry<String, Double> ne = jt.next();  
  128.                 newMeanMap.put(ne.getKey(), newMeanMap.get(ne.getKey()) / memberNum);     
  129.         }  
  130.         return newMeanMap;  
  131.     }  
  132.   
  133.     /**找出距离当前点最近的聚类中心 
  134.      * @param double[][] 点到所有聚类中心的距离 
  135.      * @return i 最近的聚类中心的序 号 
  136.      * @throws IOException  
  137.      */  
  138.     private int findNearestMeans(double[][] distance,int m) {  
  139.         // TODO Auto-generated method stub  
  140.         double minDist = 10;  
  141.         int j = 0;  
  142.         for(int i = 0; i < distance[m].length; i++){  
  143.             if(distance[m][i] < minDist){  
  144.                 minDist = distance[m][i];  
  145.                 j = i;  
  146.             }  
  147.         }  
  148.         return j;  
  149.     }  
  150.   
  151.   
  152.     /**计算两个点的距离 
  153.      * @param map1 点1的向量map 
  154.      * @param map2 点2的向量map 
  155.      * @return double 两个点的欧式距离 
  156.      */  
  157.     private double getDistance(Map<String, Double> map1, Map<String, Double> map2) {  
  158.         // TODO Auto-generated method stub  
  159.         return 1 - computeSim(map1,map2);  
  160.     }  
  161.       
  162.     /**计算两个文本的相似度 
  163.      * @param testWordTFMap 文本1的<单词,词频>向量 
  164.      * @param trainWordTFMap 文本2<单词,词频>向量 
  165.      * @return Double 向量之间的相似度 以向量夹角余弦计算或者向量内积计算(效果相当而速度更快) 
  166.      * @throws IOException  
  167.      */  
  168.     private double computeSim(Map<String, Double> testWordTFMap,  
  169.             Map<String, Double> trainWordTFMap) {  
  170.         // TODO Auto-generated method stub  
  171.         double mul = 0;//, testAbs = 0, trainAbs = 0;  
  172.         Set<Map.Entry<String, Double>> testWordTFMapSet = testWordTFMap.entrySet();  
  173.         for(Iterator<Map.Entry<String, Double>> it = testWordTFMapSet.iterator(); it.hasNext();){  
  174.             Map.Entry<String, Double> me = it.next();  
  175.             if(trainWordTFMap.containsKey(me.getKey())){  
  176.                 mul += me.getValue()*trainWordTFMap.get(me.getKey());  
  177.             }  
  178.             //testAbs += me.getValue() * me.getValue();  
  179.         }  
  180.         //testAbs = Math.sqrt(testAbs);  
  181.           
  182.         /*Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet(); 
  183.         for(Iterator<Map.Entry<String, Double>> it = trainWordTFMapSet.iterator(); it.hasNext();){ 
  184.             Map.Entry<String, Double> me = it.next(); 
  185.             trainAbs += me.getValue()*me.getValue(); 
  186.         } 
  187.         trainAbs = Math.sqrt(trainAbs);*/  
  188.         return mul ;/// (testAbs * trainAbs);  
  189.     }  
  190.   
  191.     /**获取kmeans算法迭代的初始点 
  192.      * @param k 聚类的数量 
  193.      * @param Map<String, Map<String, Double>> allTestSampleMap 所有测试样例的<文件名,向量>构成的map 
  194.      * @return Map<Integer, Map<String, Double>> 初始中心点的Map 
  195.      * @throws IOException  
  196.      */  
  197.     private Map<Integer, Map<String, Double>> getInitPoint(Map<String, Map<String, Double>> allTestSampleMap, int K) {  
  198.         // TODO Auto-generated method stub  
  199.         int count = 0, i = 0;  
  200.         Map<Integer, Map<String, Double>> meansMap = new TreeMap<Integer, Map<String, Double>>();//保存K个聚类中心点向量  
  201.         System.out.println("本次聚类的初始点对应的文件为:");  
  202.         Set<Map.Entry<String, Map<String,Double>>> allTestSampleMapSet = allTestSampleMap.entrySet();  
  203.         for(Iterator<Map.Entry<String, Map<String,Double>>> it = allTestSampleMapSet.iterator();it.hasNext();){  
  204.             Map.Entry<String, Map<String,Double>> me = it.next();  
  205.             if(count == i * allTestSampleMapSet.size() / K){  
  206.                 meansMap.put(i, me.getValue());  
  207.                 System.out.println(me.getKey() + " map size is " + me.getValue().size());  
  208.                 i++;  
  209.             }  
  210.             count++;  
  211.         }  
  212.         return meansMap;  
  213.     }  
  214.   
  215.     /**输出聚类结果到文件中 
  216.      * @param kmeansClusterResultFile 输出文件目录 
  217.      * @param kmeansClusterResult 聚类结果 
  218.      * @throws IOException  
  219.      */  
  220.     private void printClusterResult(Map<String, Integer> kmeansClusterResult, String kmeansClusterResultFile) throws IOException {  
  221.         // TODO Auto-generated method stub  
  222.         FileWriter resWriter = new FileWriter(kmeansClusterResultFile);  
  223.         Set<Map.Entry<String,Integer>> kmeansClusterResultSet = kmeansClusterResult.entrySet();  
  224.         for(Iterator<Map.Entry<String,Integer>> it = kmeansClusterResultSet.iterator(); it.hasNext(); ){  
  225.             Map.Entry<String, Integer> me = it.next();  
  226.             resWriter.append(me.getKey() + " " + me.getValue() + "\n");  
  227.         }  
  228.         resWriter.flush();  
  229.         resWriter.close();  
  230.     }  
  231.       
  232.     public void KmeansClusterMain(String testSampleDir) throws IOException {  
  233.         //首先计算文档TF-IDF向量,保存为Map<String,Map<String,Double>> 即为Map<文件名,Map<特征词,TF-IDF值>>  
  234.         ComputeWordsVector computeV = new ComputeWordsVector();  
  235.         int[] K = {1020 ,30};  
  236.         Map<String,Map<String,Double>> allTestSampleMap = computeV.computeTFMultiIDF(testSampleDir);  
  237.         for(int i = 0; i < K.length; i++){  
  238.             System.out.println("开始聚类,聚成" + K[i] + "类");  
  239.             String KmeansClusterResultFile = "F:/DataMiningSample/KmeansClusterResult/";  
  240.             Map<String,Integer> KmeansClusterResult = new TreeMap<String, Integer>();  
  241.             KmeansClusterResult = doProcess(allTestSampleMap, K[i]);  
  242.             KmeansClusterResultFile += K[i];  
  243.             printClusterResult(KmeansClusterResult,KmeansClusterResultFile);  
  244.             System.out.println("The Entropy for this Cluster is " + computeV.evaluateClusterRes(KmeansClusterResultFile, K[i]));  
  245.         }  
  246.     }  
  247. }  
聚类器主类ClusterMain.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.pku.yangliu;  
  2.   
  3. import java.io.IOException;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6. /**聚类器主类,提供主函数入口 
  7.  * 
  8.  */  
  9. public class ClusterMain {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws IOException  
  14.      */  
  15.     public static void main(String[] args) throws IOException {  
  16.         // TODO Auto-generated method stub  
  17.         DataPreProcess DataPP = new DataPreProcess();  
  18.         ComputeWordsVector computeV = new ComputeWordsVector();  
  19.         //KmeansSVDCluster kmeansCluster1 = new KmeansSVDCluster();  
  20.         KmeansCluster kmeansCluster2 = new KmeansCluster();  
  21.         DataPP.BPPMain(args);//数据预处理,注意如果已经完成数据预处理,此函数可以不执行  
  22.         //下面创建聚类算法的测试样例集合  
  23.         String srcDir = "F:/DataMiningSample/processedSample_includeNotSpecial/";  
  24.         String destDir = "F:/DataMiningSample/clusterTestSample/";  
  25.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
  26.         String beginTime = sdf.format(new java.util.Date());    
  27.         System.out.println("程序开始执行时间:"+beginTime);    
  28.         String[] terms = computeV.createTestSamples(srcDir, destDir);  
  29.         //kmeansCluster1.KmeansClusterMain(destDir, terms);  
  30.         kmeansCluster2.KmeansClusterMain(destDir);  
  31.         String endTime = sdf.format(new java.util.Date());    
  32.         System.out.println("程序结束执行时间:"+endTime);    
  33.     }  
  34. }  
3、K-means算法聚类结果

      K-means算法对newsgroup文本聚类的结果用聚类结果的熵值来度量,熵值定义如下


对newsgroup文本聚类的结果混淆矩阵如下:


这是用DF法降维到6070词的聚类结果,熵值已经比较小了聚20类时只有1.144,特征词抽取降维是数据挖掘研究中的一个重要内容,我还尝试了用LSI中的SVD分解来进行特征降维,详细介绍实现和其他两种聚类算法的聚类结果对比见下一篇博文数据挖掘-基于Kmeans算法、MBSAS算法及DBSCAN算法的newsgroup18828文本聚类器的JAVA实现(下)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值