当前位置: 首页>>代码示例>>Java>>正文


Java Matrix.numCols方法代码示例

本文整理汇总了Java中org.apache.mahout.math.Matrix.numCols方法的典型用法代码示例。如果您正苦于以下问题:Java Matrix.numCols方法的具体用法?Java Matrix.numCols怎么用?Java Matrix.numCols使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mahout.math.Matrix的用法示例。


在下文中一共展示了Matrix.numCols方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setCovarianceMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * Computes the inverse covariance from the input covariance matrix given in input.
 *
 * @param m A covariance matrix.
 * @throws IllegalArgumentException if <tt>eigen values equal to 0 found</tt>.
 */
public void setCovarianceMatrix(Matrix m) {
  if (m.numRows() != m.numCols()) {
    throw new CardinalityException(m.numRows(), m.numCols());
  }
  // See http://www.mlahanas.de/Math/svd.htm for details,
  // which specifically details the case of covariance matrix inversion
  // Complexity: O(min(nm2,mn2))
  SingularValueDecomposition svd = new SingularValueDecomposition(m);
  Matrix sInv = svd.getS();
  // Inverse Diagonal Elems
  for (int i = 0; i < sInv.numRows(); i++) {
    double diagElem = sInv.get(i, i);
    if (diagElem > 0.0) {
      sInv.set(i, i, 1 / diagElem);
    } else {
      throw new IllegalStateException("Eigen Value equals to 0 found.");
    }
  }
  inverseCovarianceMatrix = svd.getU().times(sInv.times(svd.getU().transpose()));
  Preconditions.checkArgument(inverseCovarianceMatrix != null, "inverseCovarianceMatrix not initialized");
}
 
开发者ID:saradelrio,项目名称:Chi-FRBCS-BigDataCS,代码行数:28,代码来源:MahalanobisDistanceMeasure.java

示例2: writeMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
static void writeMatrix(Matrix origMatrix,
     Path outPath, Path tmpPath, String label) throws IOException {
   Configuration conf = new Configuration();
   Path outputDir = new Path(outPath, label + origMatrix.numRows() + "x"
       + origMatrix.numCols());
   FileSystem fs = FileSystem.get(outputDir.toUri(), conf);
   if (!fs.exists(outputDir)) {
     Path outputFile = new Path(outputDir, "singleSliceMatrix");
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
         outputFile, IntWritable.class, VectorWritable.class);
     VectorWritable vectorWritable = new VectorWritable();
     try {
       for (int r = 0; r < origMatrix.numRows(); r++) {
         Vector vector = origMatrix.viewRow(r);
         vectorWritable.set(vector);
         writer.append(new IntWritable(r), vectorWritable);
       }
     } finally {
       writer.close();
     }
   } else {
     log.warn("----------- Skip matrix " + outputDir + " - already exists");
   }
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:25,代码来源:SPCADriver.java

示例3: sparseVectorTimesMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * multiply a sparse vector by a matrix
 * @param sparseVector
 * @param matrix
 * @param resArray
 */

static void sparseVectorTimesMatrix(org.apache.spark.mllib.linalg.Vector sparseVector, Matrix matrix,
		double[] resArray) {
	int matrixCols = matrix.numCols();
	int[] indices;
	for (int col = 0; col < matrixCols; col++) 
	{
		indices=((SparseVector)sparseVector).indices();
		int index = 0, i=0;
		double value = 0;
		double dotRes = 0;
		for(i=0; i <indices.length; i++)
		{
			index=indices[i];
			value=sparseVector.apply(index);
			dotRes += matrix.getQuick(index,col) * value;
		}
		resArray[col] = dotRes;
	}
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:27,代码来源:PCAUtils.java

示例4: convertMahoutToSparkMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * Convert org.apache.mahout.math.Matrix object to org.apache.spark.mllib.linalg.Matrix object to be used in Spark Programs
 */
public static org.apache.spark.mllib.linalg.Matrix convertMahoutToSparkMatrix(Matrix mahoutMatrix)
{
	int rows=mahoutMatrix.numRows();
	int cols=mahoutMatrix.numCols();
	int arraySize= rows*cols;
	int arrayIndex=0;
	double[] colMajorArray= new double[arraySize];
	for(int i=0;i<cols; i++)
	{
		for(int j=0; j< rows; j++)
		{
			colMajorArray[arrayIndex] = mahoutMatrix.get(j, i);
			arrayIndex++;
		}
	}
	org.apache.spark.mllib.linalg.Matrix sparkMatrix = Matrices.dense(rows, cols, colMajorArray);
	return sparkMatrix;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:22,代码来源:PCAUtils.java

示例5: setMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
public void setMatrix(Matrix m) {
  int length = confusionMatrix.length;
  if (m.numRows() != m.numCols()) {
    throw new IllegalArgumentException(
        "ConfusionMatrix: matrix(" + m.numRows() + ',' + m.numCols() + ") must be square");
  }
  for (int r = 0; r < length; r++) {
    for (int c = 0; c < length; c++) {
      confusionMatrix[r][c] = (int) Math.round(m.get(r, c));
    }
  }
  Map<String,Integer> labels = m.getRowLabelBindings();
  if (labels == null) {
    labels = m.getColumnLabelBindings();
  }
  if (labels != null) {
    String[] sorted = sortLabels(labels);
    verifyLabels(length, sorted);
    labelMap.clear();
    for (int i = 0; i < length; i++) {
      labelMap.put(sorted[i], i);
    }
  }
}
 
开发者ID:saradelrio,项目名称:Chi-FRBCS-BigDataCS,代码行数:25,代码来源:ConfusionMatrix.java

示例6: TopicModel

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
public TopicModel(Matrix topicTermCounts, Vector topicSums, double eta, double alpha,
  String[] dictionary, int numThreads, double modelWeight) {
  this.dictionary = dictionary;
  this.topicTermCounts = topicTermCounts;
  this.topicSums = topicSums;
  this.numTopics = topicSums.size();
  this.numTerms = topicTermCounts.numCols();
  this.eta = eta;
  this.alpha = alpha;
  this.sampler = new Sampler(RandomUtils.getRandom());
  this.numThreads = numThreads;
  if (modelWeight != 1) {
    topicSums.assign(Functions.mult(modelWeight));
    for (int x = 0; x < numTopics; x++) {
      topicTermCounts.viewRow(x).assign(Functions.mult(modelWeight));
    }
  }
  initializeThreadPool();
}
 
开发者ID:saradelrio,项目名称:Chi-FRBCS-BigData-Ave,代码行数:20,代码来源:TopicModel.java

示例7: toDenseMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
private static DenseMatrix toDenseMatrix(Matrix origMtx) {
  DenseMatrix mtx = new DenseMatrix(origMtx.numRows(), origMtx.numCols());
  Iterator<MatrixSlice> sliceIterator = origMtx.iterateAll();
  while (sliceIterator.hasNext()) {
    MatrixSlice slice = sliceIterator.next();
    mtx.viewRow(slice.index()).assign(slice.vector());
  }
  return mtx;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:10,代码来源:SPCADriver.java

示例8: sparseVectorTimesMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
static Vector sparseVectorTimesMatrix(Vector vector, Matrix matrix,
    DenseVector resVector) {
  int nCols = matrix.numCols();
  for (int c = 0; c < nCols; c++) {
    Double resDouble = vector.dot(matrix.viewColumn(c));
    resVector.set(c, resDouble);
  }
  return resVector;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:10,代码来源:PCACommon.java

示例9: denseVectorTimesMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
static Vector denseVectorTimesMatrix(DenseVector vector, Matrix matrix,
    DenseVector resVector) {
  int nRows = matrix.numRows();
  int nCols = matrix.numCols();
  for (int c = 0; c < nCols; c++) {
    double dotres = 0;
    for (int r = 0; r < nRows; r++)
      dotres += vector.getQuick(r) * matrix.getQuick(r, c);
    resVector.set(c, dotres);
  }
  return resVector;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:13,代码来源:PCACommon.java

示例10: vectorTimesMatrixTranspose

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
static Vector vectorTimesMatrixTranspose(Vector vector,
    Matrix matrix, DenseVector resVector) {
  int nRows = matrix.numRows();
  int nCols = matrix.numCols();
  for (int r = 0; r < nRows; r++) {
    double dotres = 0;
    for (int c = 0; c < nCols; c++)
      dotres += vector.getQuick(c) * matrix.getQuick(r, c);
    resVector.set(r, dotres);
  }
  return resVector;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:13,代码来源:PCACommon.java

示例11: toDistributedRowMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * Convert an in-memory representation of a matrix to a distributed version It
 * then can be used in distributed jobs
 * 
 * @param oriMatrix
 * @return path that contains the matrix files
 * @throws IOException
 */
static DistributedRowMatrix toDistributedRowMatrix(Matrix origMatrix,
    Path outPath, Path tmpPath, String label) throws IOException {
  Configuration conf = new Configuration();
  Path outputDir = new Path(outPath, label + origMatrix.numRows() + "x"
      + origMatrix.numCols());
  FileSystem fs = FileSystem.get(outputDir.toUri(), conf);
  if (!fs.exists(outputDir)) {
    Path outputFile = new Path(outputDir, "singleSliceMatrix");
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
        outputFile, IntWritable.class, VectorWritable.class);
    VectorWritable vectorWritable = new VectorWritable();
    try {
      for (int r = 0; r < origMatrix.numRows(); r++) {
        Vector vector = origMatrix.viewRow(r);
        vectorWritable.set(vector);
        writer.append(new IntWritable(r), vectorWritable);
      }
    } finally {
      writer.close();
    }
  } else {
    log.warn("----------- Skip matrix " + outputDir + " - already exists");
  }
  DistributedRowMatrix dMatrix = new DistributedRowMatrix(outputDir, tmpPath,
      origMatrix.numRows(), origMatrix.numCols());
  dMatrix.setConf(conf);
  return dMatrix;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:37,代码来源:PCACommon.java

示例12: denseVectorTimesMatrix

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * multiply a dense vector by a matrix
 * @param xm_mahout: result vector
 * @return
 */
static Vector denseVectorTimesMatrix(Vector vector, Matrix matrix,
		Vector xm_mahout) {
	int nRows = matrix.numRows();
	int nCols = matrix.numCols();
	for (int c = 0; c < nCols; c++) {
		double dotres = 0;
		for (int r = 0; r < nRows; r++)
			dotres += vector.getQuick(r) * matrix.getQuick(r, c);
		xm_mahout.set(c, dotres);
	}
	return xm_mahout;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:18,代码来源:PCAUtils.java

示例13: vectorTimesMatrixTranspose

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
 * multiply a dense vector by the transpose of a matrix
 * @param resArray: result array
 * @return
 */
static double[] vectorTimesMatrixTranspose(Vector vector, Matrix matrix,
		double[] resArray) {
	int nRows = matrix.numRows();
	int nCols = matrix.numCols();
	for (int r = 0; r < nRows; r++) {
		double dotres = 0;
		for (int c = 0; c < nCols; c++)
			dotres += vector.getQuick(c) * matrix.getQuick(r, c);
		resArray[r] = dotres;
	}
	return resArray;
}
 
开发者ID:SiddharthMalhotra,项目名称:sPCA,代码行数:18,代码来源:PCAUtils.java

示例14: InMemoryCollapsedVariationalBayes0

import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
public InMemoryCollapsedVariationalBayes0(Matrix corpus,
                                          String[] terms,
                                          int numTopics,
                                          double alpha,
                                          double eta,
                                          int numTrainingThreads,
                                          int numUpdatingThreads,
                                          double modelCorpusFraction) {
  //this.seed = seed;
  this.numTopics = numTopics;
  this.alpha = alpha;
  this.eta = eta;
  //this.minDfCt = 0;
  //this.maxDfPct = 1.0f;
  corpusWeights = corpus;
  numDocuments = corpus.numRows();
  this.terms = terms;
  this.initialModelCorpusFraction = modelCorpusFraction;
  numTerms = terms != null ? terms.length : corpus.numCols();
  Map<String, Integer> termIdMap = Maps.newHashMap();
  if (terms != null) {
    for (int t=0; t<terms.length; t++) {
      termIdMap.put(terms[t], t);
    }
  }
  this.numTrainingThreads = numTrainingThreads;
  this.numUpdatingThreads = numUpdatingThreads;
  postInitCorpus();
  initializeModel();
}
 
开发者ID:saradelrio,项目名称:Chi-FRBCS-BigData-Max,代码行数:31,代码来源:InMemoryCollapsedVariationalBayes0.java


注:本文中的org.apache.mahout.math.Matrix.numCols方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。