コード例 #1
0
ファイル: kMeans.java プロジェクト: yjwudi/cluster
 public static void main(String[] args) {
   int i, j;
   int n, k, dim;
   kMeans cluster;
   Scanner sc = new Scanner(System.in);
   k = sc.nextInt();
   n = sc.nextInt();
   dim = sc.nextInt();
   Point[] tmpPoints = new Point[n];
   for (i = 0; i < n; i++) {
     double[] num = new double[dim];
     for (j = 0; j < dim; j++) num[j] = sc.nextDouble();
     tmpPoints[i] = new Point(dim, num);
   }
   cluster = new kMeans(k, tmpPoints);
   cluster.getCluster();
   // calculate the sse value after clustering
   cluster.sseValue = 0.0;
   for (i = 0; i < n; i++) {
     cluster.sseValue +=
         cluster.points[i].distEuclid(cluster.centroids[cluster.points[i].clusterIndex]);
   }
   System.out.println("The SSE of after clustering is: " + cluster.sseValue);
   /*
   int[] clusterCount = new int[k];
   for(i = 0; i < k; i++){
   	clusterCount[i] = 0;
   }
   for(i = 0; i < cluster.points.length; i++){
   	clusterCount[cluster.points[i].clusterIndex]++;
   	//System.out.print(cluster.points[i].clusterIndex);
   	//System.out.print("  ");
   }
   System.out.println("  ");
   for(i = 0; i < k; i++){
   	System.out.println(clusterCount[i]);
   }
        */
 }
コード例 #2
0
ファイル: KMeans.java プロジェクト: IrisLam9690/IrisStudy
  /**
   * Main method -- to test the kMeans class
   *
   * @param args command line arguments
   */
  public static void main(String[] args) {

    //		//对用户矩阵,从3到100个聚类数目,分别计算这个数目下的聚类结果,写入文件
    //		for(int z = 3; z <=100; z++){
    //
    //			//计算z个聚类的聚类结果
    //			kMeans km = new kMeans(z, "D:/pca/pcauserM0.85.txt");
    //
    //			try {
    //				km.readData();
    //			} catch (Exception e) {
    //				System.err.println(e);
    //				System.exit(-1);
    //			}
    //
    //			km.runKMeans();
    //			System.out.println(km.getDataPoints().size());
    //			System.out.println(km.getDataPoints().get(0));
    //			System.out.println(km.getDataPoints().get(km.getDataPoints().size()-1));
    //
    //	        //把结果写入文件
    ////			Iterator i = this.kMeansPoints.iterator();
    ////			while (i.hasNext()){
    ////				//设置一个String用来写入到文件
    ////				String strWr = new String();
    ////				strWr = "\n" + (kMeansPoint)(i.next());
    ////			}
    //
    //			//把结果写入文件
    //			for(int i = 0; i < km.getDataPoints().size(); i++){
    //				//设置一个String用来写入到文件
    //				String strWr = new String();
    //				kMeansPoint kmp = (kMeansPoint) km.getDataPoints().get(i);
    //				strWr = i+":"+ kmp.getClusterNumber();
    //
    //				//设置一个路径用来保存文件
    //				String pathZ = "D:/pca/difClusters/kM_" + z + "kind_0.85_0.3user.txt";
    //				write(pathZ, strWr);
    //			}
    //		}

    //		//给电影矩阵聚类,从3到100个聚类数目,分别计算这个数目下的聚类结果,写入文件
    //		for(int z = 3; z <=100; z++){
    //			//计算z个聚类的聚类结果
    //			kMeans km = new kMeans(z, "D:/pca/pcauserMovieM0.85.txt");
    //
    //			try {
    //				km.readData();
    //			} catch (Exception e) {
    //				System.err.println(e);
    //				System.exit(-1);
    //			}
    //
    //			km.runKMeans();
    //			System.out.println(km.getDataPoints().size());
    //			System.out.println(km.getDataPoints().get(0));
    //			System.out.println(km.getDataPoints().get(km.getDataPoints().size()-1));
    //
    //	        //把结果写入文件
    ////			Iterator i = this.kMeansPoints.iterator();
    ////			while (i.hasNext()){
    ////				//设置一个String用来写入到文件
    ////				String strWr = new String();
    ////				strWr = "\n" + (kMeansPoint)(i.next());
    ////			}
    //
    //			//把结果写入文件
    //			for(int i = 0; i < km.getDataPoints().size(); i++){
    //				//设置一个String用来写入到文件
    //				String strWr = new String();
    //				kMeansPoint kmp = (kMeansPoint) km.getDataPoints().get(i);
    //				strWr = i+":"+ kmp.getClusterNumber();
    //
    //				//设置一个路径用来保存文件
    //				String pathZ = "D:/pca/difMovieClusters/kM_" + z + "kind_0.85_0.3movie.txt";
    //				write(pathZ, strWr);
    //			}
    //    	}

    // 给用户-电影类别矩阵聚类,从3到100个聚类数目,分别计算这个数目下的聚类结果,写入文件
    for (int z = 238; z <= 300; z++) {
      // 计算z个聚类的聚类结果
      kMeans km = new kMeans(z, "D:/pca/recommendWork/pcamovieByAdd1Matrix0.85.txt");

      try {
        km.readData();
      } catch (Exception e) {
        System.err.println(e);
        System.exit(-1);
      }

      km.runKMeans();
      System.out.println(km.getDataPoints().size());
      System.out.println(km.getDataPoints().get(0));
      System.out.println(km.getDataPoints().get(km.getDataPoints().size() - 1));

      // 把结果写入文件
      //			Iterator i = this.kMeansPoints.iterator();
      //			while (i.hasNext()){
      //				//设置一个String用来写入到文件
      //				String strWr = new String();
      //				strWr = "\n" + (kMeansPoint)(i.next());
      //			}s

      // 把结果写入文件
      for (int i = 0; i < km.getDataPoints().size(); i++) {
        // 设置一个String用来写入到文件
        String strWr = new String();
        kMeansPoint kmp = (kMeansPoint) km.getDataPoints().get(i);
        strWr = i + ":" + kmp.getClusterNumber();

        // 设置一个路径用来保存文件
        String pathZ = "D:/pca/recommendWork/difMovClusters/kM_" + z + "kind_0.85_clu_movie.txt";
        write(pathZ, strWr);
      }
    }
  } // end of main()