Package org.apache.mahout.math

Examples of org.apache.mahout.math.MatrixWritable


  public void computeV(File tmpDir, int ncols) throws IOException {
    // step 5, compute pieces of V
    for (int j = 0; j < ncols; j += columnsPerSlice) {
      final File bPath = bFile(tmpDir, j);
      if (bPath.exists()) {
        MatrixWritable m = new MatrixWritable();
        final DataInputStream in = new DataInputStream(new FileInputStream(bPath));
        try {
          m.readFields(in);
        } finally {
          in.close();
        }
        m.set(l2.solveRight(m.get().transpose()).times(svd.getV()));
        final DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(tmpDir, String.format("V-%s", bPath.getName().replaceAll(".*-", "")))));
        try {
          m.write(out);
        } finally {
          out.close();
        }
      }
    }
View Full Code Here


      ParameteredGeneralizations.configureParameters(this, jobConf);
    }
    try {
      if (inverseCovarianceFile.get() != null) {
        FileSystem fs = FileSystem.get(inverseCovarianceFile.get().toUri(), jobConf);
        MatrixWritable inverseCovarianceMatrix =
            ClassUtils.instantiateAs((Class<? extends MatrixWritable>) matrixClass.get(), MatrixWritable.class);
        if (!fs.exists(inverseCovarianceFile.get())) {
          throw new FileNotFoundException(inverseCovarianceFile.get().toString());
        }
        DataInputStream in = fs.open(inverseCovarianceFile.get());
        try {
          inverseCovarianceMatrix.readFields(in);
        } finally {
          Closeables.closeQuietly(in);
        }
        this.inverseCovarianceMatrix = inverseCovarianceMatrix.get();
        Preconditions.checkArgument(this.inverseCovarianceMatrix != null, "inverseCovarianceMatrix not initialized");
      }

      if (meanVectorFile.get() != null) {
        FileSystem fs = FileSystem.get(meanVectorFile.get().toUri(), jobConf);
View Full Code Here

   
    Path inverseCovarianceFile = new Path(getTestTempDirPath("mahalanobis"),
        "MahalanobisDistanceMeasureInverseCovarianceFile");
    conf.set("MahalanobisDistanceMeasure.inverseCovarianceFile", inverseCovarianceFile.toString());
    FileSystem fs = FileSystem.get(inverseCovarianceFile.toUri(), conf);
    MatrixWritable inverseCovarianceMatrix = new MatrixWritable(measure.getInverseCovarianceMatrix());
    DataOutputStream out = fs.create(inverseCovarianceFile);
    try {
      inverseCovarianceMatrix.write(out);
    } finally {
      Closeables.closeQuietly(out);
    }
   
    Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
View Full Code Here

   
    Path inverseCovarianceFile = new Path(getTestTempDirPath("mahalanobis"),
        "MahalanobisDistanceMeasureInverseCovarianceFile");
    conf.set("MahalanobisDistanceMeasure.inverseCovarianceFile", inverseCovarianceFile.toString());
    FileSystem fs = FileSystem.get(inverseCovarianceFile.toUri(), conf);
    MatrixWritable inverseCovarianceMatrix = new MatrixWritable(measure.getInverseCovarianceMatrix());
    DataOutputStream out = fs.create(inverseCovarianceFile);
    try {
      inverseCovarianceMatrix.write(out);
    } finally {
      Closeables.closeQuietly(out);
    }
   
    Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
View Full Code Here

    // initially, we don't know what size buffer to hold
    int nrows = -1;
    int ncols = -1;
    Matrix r = null;

    MatrixWritable m = new MatrixWritable();

    int row = 0;
    for (File file : files) {
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      m.readFields(in);
      in.close();
      if (nrows == -1) {
        // now we can set an upper bound on how large our result will be
        nrows = m.get().rowSize() * files.size();
        ncols = m.get().columnSize();
        r = new DenseMatrix(nrows, ncols);
      }
      r.viewPart(row, m.get().rowSize(), 0, r.columnSize()).assign(m.get());
      row += m.get().rowSize();
    }
    // at the end, row will have the true size of the result
    if (row != nrows && r != null) {
      // and if that isn't the size of the buffer, we need to crop the result a bit
      r = r.viewPart(0, row, 0, ncols);
View Full Code Here

    Matrix v = new RandomTrinaryMatrix(2, columns, rank, false);
    Matrix a = u.times(d).times(v.transpose());

    if (tmpDir != null) {
      for (int i = 0; i < a.rowSize(); i += rowsPerSlice) {
        MatrixWritable m = new MatrixWritable(a.viewPart(i, Math.min(a.rowSize() - i, rowsPerSlice), 0, a.columnSize()));
        DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(tmpDir, String.format("%s-%09d", aBase, i))));
        try {
          m.write(out);
        } finally {
          out.close();
        }
      }
    }
View Full Code Here

    return 0;
  }
 
  private static void exportText(Path inputPath, PrintStream out) throws IOException {
    String TAB_SEPARATOR = "|";
    MatrixWritable mw = new MatrixWritable();
    Text key = new Text();
    readSeqFile(inputPath, key, mw);
    Matrix m = mw.get();
    ConfusionMatrix cm = new ConfusionMatrix(m);
    out.println(String.format("%-40s", "Label") + TAB_SEPARATOR + String.format("%-10s", "Total")
                + TAB_SEPARATOR + String.format("%-10s", "Correct") + TAB_SEPARATOR
                + String.format("%-6s", "%") + TAB_SEPARATOR);
    out.println(String.format("%-70s", "-").replace(' ', '-'));
View Full Code Here

    out.println(String.format("%-70s", "-").replace(' ', '-'));
    out.println(cm.toString());
  }
 
  private static void exportTable(Path inputPath, PrintStream out, boolean wrapHtml) throws IOException {
    MatrixWritable mw = new MatrixWritable();
    Text key = new Text();
    readSeqFile(inputPath, key, mw);
    String fileName = inputPath.getName();
    fileName = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length());
    Matrix m = mw.get();
    ConfusionMatrix cm = new ConfusionMatrix(m);
    if (wrapHtml) {
      printHeader(out, fileName);
    }
    out.println("<p/>");
View Full Code Here

   */
  public H2OBCast(T o) {
    obj = o;

    if (o instanceof Matrix) {
      buf = serialize(new MatrixWritable((Matrix)o));
      isMatrix = true;
    } else if (o instanceof Vector) {
      buf = serialize(new VectorWritable((Vector)o));
      isMatrix = false;
    } else {
View Full Code Here

    T ret = null;
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    try {
      ObjectInputStream ois = new ObjectInputStream(bis);
      if (isMatrix) {
        MatrixWritable w = new MatrixWritable();
        w.readFields(ois);
        ret = (T) w.get();
      } else {
        VectorWritable w = new VectorWritable();
        w.readFields(ois);
        ret = (T) w.get();
      }
    } catch (java.io.IOException e) {
      System.out.println("Caught exception: " + e);
    }
    return ret;
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.MatrixWritable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.