Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可视化Kmean聚类_Java_K Means - Fatal编程技术网

Java 可视化Kmean聚类

Java 可视化Kmean聚类,java,k-means,Java,K Means,我在12维矩阵上做KMean聚类。我设法在K组聚类中得到结果。我想通过将结果绘制成2D图形来显示结果,但我不知道如何将12维数据转换为二维数据 关于如何进行转换或可视化结果的其他方法有何建议?我试过了,但没有成功 我使用的KMean算法来自于。上也讨论过类似的问题。基本思想是找到一个合适的投影来分隔这些簇(例如,使用R中的discproj),然后在新空间上绘制簇上的投影。我会这样做(可能是算法中最简单的算法)。(顺便说一句,PCA与KMeans无关,它是降维的通用方法) 我假设变量在列中,观察值

我在12维矩阵上做KMean聚类。我设法在K组聚类中得到结果。我想通过将结果绘制成2D图形来显示结果,但我不知道如何将12维数据转换为二维数据

关于如何进行转换或可视化结果的其他方法有何建议?我试过了,但没有成功


我使用的KMean算法来自于。

上也讨论过类似的问题。基本思想是找到一个合适的投影来分隔这些簇(例如,使用
R
中的
discproj
),然后在新空间上绘制簇上的投影。

我会这样做(可能是算法中最简单的算法)。(顺便说一句,PCA与KMeans无关,它是降维的通用方法)

  • 我假设变量在列中,观察值在行中

  • 标准化数据-将变量转换为z分数。这意味着:从每个单元格中减去该列的平均值,然后用该列的标准偏差对结果进行偏差。这样你就得到了零均值和单位方差。前者是强制性的,后者,我想说,是好事。如果方差为零,则根据协方差矩阵计算特征向量,否则必须使用相关矩阵自动标准化数据。见)

  • 计算协方差矩阵的特征向量和特征值。根据特征值对特征向量进行排序。(许多库已经提供了按这种方式排序的特征向量)

  • 使用特征向量矩阵的前两列并乘以原始矩阵(转换为z分数),可视化此数据

  • 使用该库,您可以执行以下操作。它将与其他矩阵库类似:

        import cern.colt.matrix.DoubleMatrix1D;
        import cern.colt.matrix.DoubleMatrix2D;
        import cern.colt.matrix.doublealgo.Statistic;
        import cern.colt.matrix.impl.SparseDoubleMatrix2D;
        import cern.colt.matrix.linalg.Algebra;
        import cern.colt.matrix.linalg.EigenvalueDecomposition;
        import hep.aida.bin.DynamicBin1D;
    
        public class Pca {
            // to show matrix creation, it does not make much sense to calculate PCA on random data
            public static void main(String[] x) {
                double[][] data = {
                    {2.0,4.0,1.0,4.0,4.0,1.0,5.0,5.0,5.0,2.0,1.0,4.0}, 
                    {2.0,6.0,3.0,1.0,1.0,2.0,6.0,4.0,4.0,4.0,1.0,5.0},
                    {3.0,4.0,4.0,4.0,2.0,3.0,5.0,6.0,3.0,1.0,1.0,1.0},
                    {3.0,6.0,3.0,3.0,1.0,2.0,4.0,6.0,1.0,2.0,4.0,4.0}, 
                    {1.0,6.0,4.0,2.0,2.0,2.0,3.0,4.0,6.0,3.0,4.0,1.0}, 
                    {2.0,5.0,5.0,3.0,1.0,1.0,6.0,6.0,3.0,2.0,6.0,1.0}
                };
    
                DoubleMatrix2D matrix = new DenseDoubleMatrix2D(data);
    
                DoubleMatrix2D pm = pcaTransform(matrix);
    
                // print the first two dimensions of the transformed matrix - they capture most of the variance of the original data
                System.out.println(pm.viewPart(0, 0, pm.rows(), 2).toString());
            }
    
            /** Returns a matrix in the space of principal components, take the first n columns  */
            public static DoubleMatrix2D pcaTransform(DoubleMatrix2D matrix) {
                DoubleMatrix2D zScoresMatrix = toZScores(matrix);
                final DoubleMatrix2D covarianceMatrix = Statistic.covariance(zScoresMatrix);
    
                // compute eigenvalues and eigenvectors of the covariance matrix (flip needed since it is sorted by ascending).
                final EigenvalueDecomposition decomp = new EigenvalueDecomposition(covarianceMatrix);
    
                // Columns of Vs are eigenvectors = principal components = base of the new space; ordered by decreasing variance
                final DoubleMatrix2D Vs = decomp.getV().viewColumnFlip(); 
    
                // eigenvalues: ev(i) / sum(ev) is the percentage of variance captured by i-th column of Vs
                // final DoubleMatrix1D ev = decomp.getRealEigenvalues().viewFlip();
    
                // project the original matrix to the pca space
                return Algebra.DEFAULT.mult(zScoresMatrix, Vs);
            }
    
    
            /**
             * Converts matrix to a matrix of z-scores (by columns)
             */
            public static DoubleMatrix2D toZScores(final DoubleMatrix2D matrix) {
                final DoubleMatrix2D zMatrix = new SparseDoubleMatrix2D(matrix.rows(), matrix.columns());
                for (int c = 0; c < matrix.columns(); c++) {
                    final DoubleMatrix1D column = matrix.viewColumn(c);
                    final DynamicBin1D bin = Statistic.bin(column);
    
                    if (bin.standardDeviation() == 0) {   // use epsilon
                        for (int r = 0; r < matrix.rows(); r++) {
                            zMatrix.set(r, c, 0.0);
                        }
                    } else {
                        for (int r = 0; r < matrix.rows(); r++) {
                            double zScore = (column.get(r) - bin.mean()) / bin.standardDeviation();
                            zMatrix.set(r, c, zScore);
                        }
                    }
                }
    
                return zMatrix;
            }
        }
    
    导入cern.colt.matrix.DoubleMatrix1D;
    导入cern.colt.matrix.DoubleMatrix2D;
    导入cern.colt.matrix.doublealgo.Statistic;
    导入cern.colt.matrix.impl.sparsedDoubleMatrix 2d;
    导入cern.colt.matrix.linalg.Algebra;
    导入cern.colt.matrix.linalg.decomposition;
    进口hep.aida.bin.dynamicbinad;
    公共类主成分分析{
    //为了显示矩阵创建,在随机数据上计算PCA没有多大意义
    公共静态void main(字符串[]x){
    双[][]数据={
    {2.0,4.0,1.0,4.0,4.0,1.0,5.0,5.0,5.0,2.0,1.0,4.0}, 
    {2.0,6.0,3.0,1.0,1.0,2.0,6.0,4.0,4.0,4.0,1.0,5.0},
    {3.0,4.0,4.0,4.0,2.0,3.0,5.0,6.0,3.0,1.0,1.0,1.0},
    {3.0,6.0,3.0,3.0,1.0,2.0,4.0,6.0,1.0,2.0,4.0,4.0}, 
    {1.0,6.0,4.0,2.0,2.0,2.0,3.0,4.0,6.0,3.0,4.0,1.0}, 
    {2.0,5.0,5.0,3.0,1.0,1.0,6.0,6.0,3.0,2.0,6.0,1.0}
    };
    DoubleMatrix2D矩阵=新密度双矩阵x2d(数据);
    DoubleMatrix2D pm=pcaTransform(矩阵);
    //打印转换矩阵的前两个维度-它们捕获原始数据的大部分方差
    System.out.println(pm.viewPart(0,0,pm.rows(),2.toString());
    }
    /**返回主成分空间中的矩阵,取前n列*/
    公共静态DoubleMatrix2D pcaTransform(DoubleMatrix2D矩阵){
    DoubleMatrix2D zScoresMatrix=toZScores(矩阵);
    最终双矩阵2d协方差矩阵=统计协方差(zScoresMatrix);
    //计算协方差矩阵的特征值和特征向量(需要翻转,因为它是按升序排序的)。
    最终特征分解decomp=新特征分解(协方差矩阵);
    //v的列是特征向量=主成分=新空间的基;按方差递减排序
    最终DoubleMatrix2D Vs=decomp.getV().viewColumnFlip();
    //特征值:ev(i)/sum(ev)是由Vs的第i列捕获的方差百分比
    //最终DoubleMatrix1D ev=decomp.getrealCharacterizers().viewFlip();
    //将原始矩阵投影到pca空间
    返回代数.DEFAULT.mult(zScoresMatrix,Vs);
    }
    /**
    *将矩阵转换为z分数矩阵(按列)
    */
    公共静态DoubleMatrix2D TozCores(最终DoubleMatrix2D矩阵){
    final DoubleMatrix2D zMatrix=新的稀疏双矩阵x2d(matrix.rows(),matrix.columns());
    对于(int c=0;c

    你也可以使用weka。我首先将数据加载到weka中,然后使用GUI(在属性选择下)运行PCA。您将看到使用哪些参数调用哪些类,然后从代码中执行相同的操作。问题是,您需要将矩阵转换/包装为weka使用的数据格式。

    此外,其他答案建议您也可以查看。

    您好,感谢您的回复。我这里有两个问题:->如何实例化DoubleMatrix2D?->方法“pcaTransForm”和“toZScore”的区别是什么?很抱歉,我对KMean有点陌生,所以有很多事情我都不知道。实例化:根据矩阵是密集的还是稀疏的(有许多零),您可以创建密集的问题矩阵2