zoukankan      html  css  js  c++  java
  • 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);

     
  • 相关阅读:
    程序员跳槽到对手公司,被前老板设计陷害
    创业码农被公司套路最惨的一次经历
    记录一次由屁股决定研发的狗血经历
    不去培训班,你凭什么觉得自己能学会编程,嗯?!
    毕业生是怎么一步步给培训班骗去学编程的
    我的一次被骗去培训班狗血的经历
    不去培训班,你凭什么觉得自己能学会编程
    Phabricatori 之项目管理软件
    kvm之virt-manage 管理功能
    debian 10 安装apache 和php
  • 原文地址:https://www.cnblogs.com/yiduobaozhiblog1/p/9908898.html
Copyright © 2011-2022 走看看