• Java中实现PCA降维


    package com.excellence.splitsentence;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import org.jblas.ComplexDoubleMatrix;
    import org.jblas.ComplexFloatMatrix;
    import org.jblas.DoubleMatrix;
    import org.jblas.Eigen;
    import org.jblas.FloatMatrix;
    
    import com.mongodb.BasicDBList;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBCursor;
    import com.mongodb.MongoClient;
    import com.mongodb.MongoCredential;
    import com.mongodb.ServerAddress;
    public class PCA {
         /**
          * Reduce matrix dimension     减少矩阵维度
          * @param source         源矩阵
          * @param dimension       目标维度
          * @return Target matrix     返回目标矩阵
          */ 
    
        public static FloatMatrix dimensionReduction(FloatMatrix source, int dimension) {
            //C=X*X^t/m     矩阵*矩阵^异或/列数
            FloatMatrix covMatrix = source.mmul(source.transpose()).div(source.columns);
            ComplexFloatMatrix eigVal = Eigen.eigenvalues(covMatrix);
            ComplexFloatMatrix[] eigVectorsVal = Eigen.eigenvectors(covMatrix);
            ComplexFloatMatrix eigVectors = eigVectorsVal[0];
            //通过特征值将符号向量从大到小排序
            List<PCABean> beans = new ArrayList<PCA.PCABean>();
            for (int i = 0; i < eigVectors.columns; i++) {
                beans.add(new PCABean(eigVal.get(i).real(), eigVectors.getColumn(i)));
            }
            Collections.sort(beans);
            FloatMatrix newVec = new FloatMatrix(dimension, beans.get(0).vector.rows);
            for (int i = 0; i < dimension; i++) {
                ComplexFloatMatrix dm = beans.get(i).vector;
                FloatMatrix real = dm.getReal();
                newVec.putRow(i, real);
            }
            return newVec.mmul(source);
        }
        static class PCABean implements Comparable<PCABean> {
            float eigenValue;
            ComplexFloatMatrix vector;
            public PCABean(Float eigenValue, ComplexFloatMatrix vector) {
                super();
                this.eigenValue = eigenValue;
                this.vector = vector;
            }
            @Override
            public int compareTo(PCABean o) {
                return Float.compare(o.eigenValue, eigenValue);
            }
            @Override
            public String toString() {
                return "PCABean [eigenValue=" + eigenValue + ", vector=" + vector + "]";
            }
        }
    }


    如何调用?

    float[] vector = docvector.getElementArray();
    FloatMatrix d = new FloatMatrix(vector);

    FloatMatrix result = PCA.dimensionReduction(d, 10);

     
  • 相关阅读:
    如何查看ubuntu版本
    基于Python与命令行人脸识别项目(系列一)
    问题 B: Curriculum Vitae
    问题 M: 克隆玩具
    1906: 鹊桥相会
    3265: 聪明的矿工
    2363: 完美旗手队列
    2545: 内部收益率
    2544: 台球碰撞
    3272: 公民身份号码
  • 原文地址:https://www.cnblogs.com/yiduobaozhiblog1/p/9908898.html
Copyright © 2020-2023  润新知