• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java CardinalityException类的典型用法和代码示例

java 2次浏览

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

CardinalityException类属于org.apache.mahout.math包,在下文中一共展示了CardinalityException类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: setCovarianceMatrix

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的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: computeYtXandXtX

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * Refer to {@link CompositeJob} for a job description. In short, it does
 * 
 * X = Y * MEM
 * 
 * XtX = (X - Xm)' * (X - Xm)
 *  
 * YtX = (Y - Ym)' * (X - Xm)
 * 
 * @param distMatrixY the input matrix Y
 * @param inMemMatrix the in memory matrix MEM
 * @param ym the mean vector of Y
 * @param xm = ym * MEM
 * @param id the unique id for HDFS output directory
 * @return the XtX and YtX wrapped in a CompositeResult object
 * @throws IOException
 * @throws InterruptedException
 * @throws ClassNotFoundException
 */
public void computeYtXandXtX(
    DistributedRowMatrix distMatrixY, DistributedRowMatrix inMemMatrix,
    Vector ym, Vector xm, Path tmpPath, Configuration conf, String id) throws IOException,
    InterruptedException, ClassNotFoundException {
  if (distMatrixY.numCols() != inMemMatrix.numRows()) {
    throw new CardinalityException(distMatrixY.numCols(), inMemMatrix.numRows());
  }
  Path outPath = new Path(tmpPath, "Composite"+id);
  Path ymPath = PCACommon.toDistributedVector(ym,
      tmpPath, "ym-compositeJob" + id, conf);
  Path xmPath = PCACommon.toDistributedVector(xm,
      tmpPath, "xm-compositeJob" + id, conf);
  FileSystem fs = FileSystem.get(outPath.toUri(), conf);
  if (!fs.exists(outPath)) {
    run(conf, distMatrixY.getRowPath(), inMemMatrix.getRowPath()
        .toString(), inMemMatrix.numRows(), inMemMatrix.numCols(), ymPath
        .toString(), xmPath.toString(), outPath);
  } else {
    log.warn("----------- Skip Compositejob - already exists: " + outPath);
  }
  
  loadXtX(ymPath, inMemMatrix.numCols(), conf);
  loadYtX(outPath, tmpPath, inMemMatrix.numRows(), inMemMatrix.numCols(), conf);
}
 

开发者ID:SiddharthMalhotra,
项目名称:sPCA,
代码行数:44,
代码来源:CompositeJob.java

示例3: vectorAssign

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * This method overrides the Vector.assign method to allow optimization for
 * ZeroIndifferent functions
 * 
 * @param vector
 *          the vector to be updated
 * @param other
 *          the other vector
 * @param function
 *          the function that operates on elements of the two vectors
 * @return the modified vector
 */
static public Vector vectorAssign(Vector vector, Vector other, ZeroIndifferentFunc function) {
  if (vector.size() != other.size()) {
    throw new CardinalityException(vector.size(), other.size());
  }
  // special case: iterate only over the non-zero elements of the vector to
  // add
  Iterator<Element> it = other.nonZeroes().iterator();
  Element e;
  while (it.hasNext() && (e = it.next()) != null) {
    double val = vector.getQuick(e.index());
    double newVal = function.apply(val, e.get());
    vector.setQuick(e.index(), newVal);
  }
  return vector;
}
 

开发者ID:SiddharthMalhotra,
项目名称:sPCA,
代码行数:28,
代码来源:MeanAndSpanJob.java

示例4: distance

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
  if (v1.size() != v2.size()) {
    throw new CardinalityException(v1.size(), v2.size());
  }
  double result = 0.0;
  Vector vector = v1.minus(v2);
  Iterator<Vector.Element> iter = vector.iterateNonZero(); 
  while (iter.hasNext()) {
    Vector.Element e = iter.next();
    double d = Math.abs(e.get());
    if (d > result) {
      result = d;
    }
  }
  return result;
}
 

开发者ID:saradelrio,
项目名称:Chi-FRBCS-BigDataCS,
代码行数:18,
代码来源:ChebyshevDistanceMeasure.java

示例5: distance

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
  if (v1.size() != v2.size()) {
    throw new CardinalityException(v1.size(), v2.size());
  }
  double lengthSquaredv1 = v1.getLengthSquared();
  double lengthSquaredv2 = v2.getLengthSquared();
  
  double dotProduct = v2.dot(v1);
  double denominator = Math.sqrt(lengthSquaredv1) * Math.sqrt(lengthSquaredv2);
  
  // correct for floating-point rounding errors
  if (denominator < dotProduct) {
    denominator = dotProduct;
  }
  
  // correct for zero-vector corner case
  if (denominator == 0 && dotProduct == 0) {
    return 1;
  }
  
  return 1.0 - dotProduct / denominator;
}
 

开发者ID:saradelrio,
项目名称:Chi-FRBCS-BigDataCS,
代码行数:24,
代码来源:CosineDistanceMeasure.java

示例6: times

点赞 3

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * This implements matrix this.transpose().times(other)
 * 
 * @param other a DistributedRowMatrix
 * @param outPath path to write result to
 * @return a DistributedRowMatrix containing the product
 */
public DistributedRowMatrix times(DistributedRowMatrix other, Path outPath)
    throws IOException {
  if (numRows != other.numRows()) {
    throw new CardinalityException(numRows, other.numRows());
  }

  Configuration initialConf = getConf() == null ? new Configuration()
      : getConf();
  Configuration conf = MatrixMultiplicationJob.createMatrixMultiplyJobConf(
      initialConf, rowPath, other.rowPath, outPath, other.numCols);
  JobClient.runJob(new JobConf(conf));
  DistributedRowMatrix out = new DistributedRowMatrix(outPath, outputTmpPath,
      numCols, other.numCols());
  out.setConf(conf);
  return out;
}
 

开发者ID:millecker,
项目名称:applications,
代码行数:24,
代码来源:DistributedRowMatrix.java

示例7: distance

点赞 2

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
  if (v1.size() != v2.size()) {
    throw new CardinalityException(v1.size(), v2.size());
  }
  double result = 0;
  Vector vector = v1.minus(v2);
  Iterator<Vector.Element> iter = vector.iterateNonZero(); 
  // this contains all non zero elements between the two
  while (iter.hasNext()) {
    Vector.Element e = iter.next();
    result += Math.abs(e.get());
  }
  return result;
}
 

开发者ID:saradelrio,
项目名称:Chi-FRBCS-BigDataCS,
代码行数:16,
代码来源:ManhattanDistanceMeasure.java

示例8: distance

点赞 2

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
  if (v1.size() != v2.size()) {
    throw new CardinalityException(v1.size(), v2.size());
  }
  return Math.sqrt(v1.minus(v2).dot(Algebra.mult(inverseCovarianceMatrix, v1.minus(v2))));
}
 

开发者ID:saradelrio,
项目名称:Chi-FRBCS-BigDataCS,
代码行数:8,
代码来源:MahalanobisDistanceMeasure.java

示例9: multiplyJava

点赞 2

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * This implements matrix multiplication A * B in Java without using MapReduce
 * tasks
 * 
 * @param other a DistributedRowMatrix
 * @param outPath path to write result to
 * 
 * @return a DistributedRowMatrix containing the product
 */
public DistributedRowMatrix multiplyJava(DistributedRowMatrix other,
    Path outPath) throws IOException {
  // Check if cols of MatrixA = rows of MatrixB
  // (l x m) * (m x n) = (l x n)
  if (numCols != other.numRows()) {
    throw new CardinalityException(numCols, other.numRows());
  }

  // Multiply Matrix with transposed one without new MapReduce Job
  final double[][] matrixA = this.toDoubleArray();
  final double[][] matrixB = other.toDoubleArray();
  final double[][] matrixC = new double[this.numRows][other.numCols];

  int m = this.numRows;
  int n = this.numCols;
  int p = other.numCols;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < p; j++) {
        matrixC[i][j] = matrixC[i][j] + matrixA[i][k] * matrixB[k][j];
      }
    }
  }

  // Save resulting Matrix to HDFS
  try {
    writeDistributedRowMatrix(this.conf, matrixC, this.numRows,
        other.numCols, outPath, false);
  } catch (Exception e) {
    e.printStackTrace();
  }

  // Read resulting Matrix from HDFS
  DistributedRowMatrix out = new DistributedRowMatrix(outPath, outputTmpPath,
      numCols, other.numCols());
  out.setConf(conf);

  return out;
}
 

开发者ID:millecker,
项目名称:applications,
代码行数:49,
代码来源:DistributedRowMatrix.java

示例10: multiplyMapReduce

点赞 2

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * This implements matrix multiplication A * B using MapReduce tasks on CPU or
 * GPU
 * 
 * @param other a DistributedRowMatrix
 * @param outPath path to write result to
 * @param useGPU use GPU or CPU (default: false, use CPU)
 * @return a DistributedRowMatrix containing the product
 */
public DistributedRowMatrix multiplyMapReduce(DistributedRowMatrix other,
    Path outPath, boolean useGPU, boolean isMatrixATransposed, int tileWidth,
    boolean isDebugging) throws IOException {
  // Check if cols of MatrixA = rows of MatrixB
  // (l x m) * (m x n) = (l x n)
  if (numCols != other.numRows()) {
    throw new CardinalityException(numCols, other.numRows());
  }

  Configuration initialConf = (getConf() == null) ? new Configuration()
      : getConf();

  // Transpose Matrix within a new MapReduce Job
  DistributedRowMatrix transposed = this;
  if (!isMatrixATransposed) {
    transposed = transposed.transpose();
  }
  // Debug
  // System.out.println("DistributedRowMatrix transposed:");
  // transposed.printDistributedRowMatrix();

  // Build MatrixMultiplication job configuration
  Configuration conf = null;
  if (!useGPU) {
    conf = MatrixMultiplicationCpu.createMatrixMultiplicationCpuConf(
        initialConf, transposed.rowPath, other.rowPath, outPath,
        other.numCols, isDebugging);
  } else { // use GPU
    conf = MatrixMultiplicationGpu.createMatrixMultiplicationGpuConf(
        initialConf, transposed.rowPath, other.rowPath, outPath,
        other.numCols, tileWidth, isDebugging);
  }

  // Multiply Matrix with transposed one
  JobClient.runJob(new JobConf(conf));

  // Read resulting Matrix from HDFS
  DistributedRowMatrix out = new DistributedRowMatrix(outPath, outputTmpPath,
      this.numRows, other.numCols());
  out.setConf(conf);

  return out;
}
 

开发者ID:millecker,
项目名称:applications,
代码行数:53,
代码来源:DistributedRowMatrix.java

示例11: multiplyBSP

点赞 2

import org.apache.mahout.math.CardinalityException; //导入依赖的package包/类
/**
 * This implements matrix multiplication A * B using MapReduce tasks on CPU or
 * GPU
 * 
 * @param other a DistributedRowMatrix
 * @param outPath path to write result to
 * @param useGPU use GPU or CPU (default: false, use CPU)
 * @return a DistributedRowMatrix containing the product
 */
public DistributedRowMatrix multiplyBSP(DistributedRowMatrix other,
    Path outPath, boolean useGPU, boolean transposeMatrixA)
    throws IOException, ClassNotFoundException, InterruptedException {
  // Check if cols of MatrixA = rows of MatrixB
  // (l x m) * (m x n) = (l x n)
  if (numCols != other.numRows()) {
    throw new CardinalityException(numCols, other.numRows());
  }

  Configuration initialConf = (getConf() == null) ? new HamaConfiguration()
      : getConf();

  // Transpose Matrix within a new MapReduce Job
  DistributedRowMatrix transposed = this;
  if (transposeMatrixA) {
    transposed = transposed.transpose();
  }
  // Debug
  // System.out.println("DistributedRowMatrix transposed:");
  // transposed.printDistributedRowMatrix();

  // Build MatrixMultiplication job configuration
  BSPJob job = null;
  if (!useGPU) {
    job = MatrixMultiplicationBSPCpu.createMatrixMultiplicationBSPCpuConf(
        initialConf, transposed.rowPath, other.rowPath, outPath.getParent(),
        other.numCols);
  } else { // use GPU
    job = MatrixMultiplicationBSPGpu.createMatrixMultiplicationBSPGpuConf(
        initialConf, transposed.rowPath, other.rowPath, outPath.getParent(),
        other.numCols);
  }

  // Multiply Matrix with transposed one
  if (job.waitForCompletion(true)) {

    // Rename result file to output path
    Configuration conf = job.getConfiguration();
    FileSystem fs = outPath.getFileSystem(conf);
    FileStatus[] files = fs.listStatus(outPath.getParent());
    for (int i = 0; i < files.length; i++) {
      if ((files[i].getPath().getName().startsWith("part-"))
          && (files[i].getLen() > 97)) {
        fs.rename(files[i].getPath(), outPath);
        break;
      }
    }

    // Read resulting Matrix from HDFS
    DistributedRowMatrix out = new DistributedRowMatrix(outPath,
        outputTmpPath, this.numRows, other.numCols());
    out.setConf(conf);

    return out;
  }

  return null;
}
 

开发者ID:millecker,
项目名称:applications,
代码行数:68,
代码来源:DistributedRowMatrix.java


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)