List of usage examples for org.apache.mahout.math Vector set
void set(int index, double value);
From source file:edu.snu.dolphin.bsp.examples.ml.data.ClusteringDataParser.java
License:Apache License
@Override public void parse() { final List<Vector> points = new ArrayList<>(); for (final Pair<LongWritable, Text> keyValue : dataSet) { final String text = keyValue.getSecond().toString().trim(); if (text.startsWith("#") || text.length() == 0) { continue; }// w w w. j a v a 2 s .c om final String[] split = text.split("\\s+"); final Vector point = new DenseVector(split.length); try { for (int i = 0; i < split.length; i++) { point.set(i, Double.valueOf(split[i])); } points.add(point); } catch (final NumberFormatException e) { parseException = new ParseException("Parse failed: each field should be a number"); return; } } result = points; }
From source file:edu.snu.dolphin.bsp.examples.ml.data.ClusterStats.java
License:Apache License
/** * Compute mean from the statistics./* w w w . j a v a 2 s . c o m*/ * @return */ public Vector computeMean() { final Vector mean = new DenseVector(pointSum.size()); for (int i = 0; i < mean.size(); i++) { mean.set(i, pointSum.get(i) / probSum); } return mean; }
From source file:edu.snu.dolphin.bsp.examples.ml.data.RegressionDataParser.java
License:Apache License
@Override public void parse() { LOG.log(Level.INFO, "Trying to parse!"); result = new ArrayList<>(); for (final Pair<LongWritable, Text> keyValue : dataSet) { final String text = keyValue.getSecond().toString().trim(); if (text.startsWith("#") || text.length() == 0) { continue; }//from ww w . jav a 2 s .c o m final String[] split = text.split("\\s+"); if (split.length != dimension + 1) { parseException = new ParseException("Parse failed: the number of features is not " + dimension); return; } final double output; final Vector feature = new DenseVector(split.length); try { output = Double.valueOf(split[dimension]); for (int i = 0; i < dimension; i++) { feature.set(i, Double.valueOf(split[i])); } feature.set(dimension, 1); } catch (final NumberFormatException e) { parseException = new ParseException("Parse failed: numbers should be DOUBLE"); return; } result.add(new Row(output, feature)); } }
From source file:edu.snu.dolphin.bsp.examples.ml.data.VectorSum.java
License:Apache License
public Vector computeVectorMean() { final Vector mean = new DenseVector(sum.size()); for (int i = 0; i < mean.size(); i++) { mean.set(i, sum.get(i) / count); }/*from www . ja v a 2 s . c o m*/ return mean; }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.CentroidListCodec.java
License:Apache License
@Override public List<Vector> decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final List<Vector> list = new ArrayList<>(); int numClusters = 0; int dimension = 0; try (final DataInputStream dais = new DataInputStream(bais)) { numClusters = dais.readInt();/* www .j a v a 2 s. c o m*/ dimension = dais.readInt(); for (int clusterID = 0; clusterID < numClusters; clusterID++) { final Vector vector = new DenseVector(dimension); for (int i = 0; i < dimension; i++) { vector.set(i, dais.readDouble()); } list.add(vector); } } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return list; }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.ClusterSummaryListCodec.java
License:Apache License
@Override public List<ClusterSummary> decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final List<ClusterSummary> resultList = new ArrayList<>(); try (final DataInputStream dais = new DataInputStream(bais)) { final int numClusters = dais.readInt(); final int dimension = dais.readInt(); for (int i = 0; i < numClusters; i++) { final double prior = dais.readDouble(); final Vector vector = new DenseVector(dimension); for (int j = 0; j < dimension; j++) { vector.set(j, dais.readDouble()); }/*w w w . j a v a 2 s . c om*/ Matrix matrix = null; if (isDiagonalCovariance) { matrix = new SparseMatrix(dimension, dimension); for (int j = 0; j < dimension; j++) { matrix.set(j, j, dais.readDouble()); } } else { matrix = new DenseMatrix(dimension, dimension); for (int j = 0; j < dimension; j++) { for (int k = 0; k < dimension; k++) { matrix.set(j, k, dais.readDouble()); } } } resultList.add(new ClusterSummary(prior, vector, matrix)); } } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return resultList; }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.LinearModelCodec.java
License:Apache License
@Override public LinearModel decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final LinearModel model; try (final DataInputStream dais = new DataInputStream(bais)) { final int vecSize = dais.readInt(); final Vector v = new DenseVector(vecSize); for (int i = 0; i < vecSize; i++) { v.set(i, dais.readDouble()); }/*from w w w .j ava 2 s .com*/ model = new LinearModel(v); } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return model; }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.LinearRegSummaryCodec.java
License:Apache License
@Override public LinearRegSummary decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final LinearModel model; final int count; final double loss; try (final DataInputStream dais = new DataInputStream(bais)) { count = dais.readInt();//from www . ja va2 s. c o m loss = dais.readDouble(); final int vecSize = dais.readInt(); final Vector v = new DenseVector(vecSize); for (int i = 0; i < vecSize; i++) { v.set(i, dais.readDouble()); } model = new LinearModel(v); } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return new LinearRegSummary(model, count, loss); }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.LogisticRegSummaryCodec.java
License:Apache License
@Override public LogisticRegSummary decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final LinearModel model; final int count; final int posNum; final int negNum; try (final DataInputStream dais = new DataInputStream(bais)) { count = dais.readInt();/*from w w w .ja va 2 s .com*/ posNum = dais.readInt(); negNum = dais.readInt(); final int vecSize = dais.readInt(); final Vector v = new DenseVector(vecSize); for (int i = 0; i < vecSize; i++) { v.set(i, dais.readDouble()); } model = new LinearModel(v); } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return new LogisticRegSummary(model, count, posNum, negNum); }
From source file:edu.snu.dolphin.bsp.examples.ml.sub.MapOfIntClusterStatsCodec.java
License:Apache License
@Override public Map<Integer, ClusterStats> decode(final byte[] data) { final ByteArrayInputStream bais = new ByteArrayInputStream(data); final Map<Integer, ClusterStats> resultMap = new HashMap<>(); try (final DataInputStream dais = new DataInputStream(bais)) { final int mapSize = dais.readInt(); final int dimension = dais.readInt(); for (int i = 0; i < mapSize; i++) { final int id = dais.readInt(); final double probSum = dais.readDouble(); final Vector pointSum = new DenseVector(dimension); for (int j = 0; j < dimension; j++) { pointSum.set(j, dais.readDouble()); }/*from ww w . j a va 2 s . com*/ Matrix outProdSum = null; if (diagonalCovariance) { outProdSum = new SparseMatrix(dimension, dimension); for (int j = 0; j < dimension; j++) { outProdSum.set(j, j, dais.readDouble()); } } else { outProdSum = new DenseMatrix(dimension, dimension); for (int j = 0; j < dimension; j++) { for (int k = 0; k < dimension; k++) { outProdSum.set(j, k, dais.readDouble()); } } } resultMap.put(id, new ClusterStats(outProdSum, pointSum, probSum)); } } catch (final IOException e) { throw new RuntimeException(e.getCause()); } return resultMap; }