Example usage for org.apache.mahout.math Vector set

List of usage examples for org.apache.mahout.math Vector set

Introduction

In this page you can find the example usage for org.apache.mahout.math Vector set.

Prototype

void set(int index, double value);

Source Link

Document

Set the value at the given index

Usage

From source file:edu.snu.cms.reef.ml.kmeans.data.VectorSum.java

License:Apache License

/**
 * We may select whether to create a deep copy of @member sum, or just a reference.
 *//*from   w  w w  .  j av a2s.  c o m*/
public VectorSum(final Vector sum, final int count, final boolean isDeepCopy) {
    this.count = count;
    if (isDeepCopy) {
        final Vector newSum = new DenseVector(sum.size());
        for (int i = 0; i < newSum.size(); i++) {
            newSum.set(i, sum.get(i));
        }
        this.sum = newSum;
    } else {
        this.sum = sum;
    }
}

From source file:edu.snu.cms.reef.ml.kmeans.data.VectorSum.java

License:Apache License

public final Vector computeVectorMean() {
    final Vector mean = new DenseVector(sum.size());
    for (int i = 0; i < mean.size(); i++) {
        mean.set(i, sum.get(i) / count);
    }//  w ww  .j  a  v  a  2  s  .c o  m
    return mean;
}

From source file:edu.snu.cms.reef.ml.kmeans.groupcomm.subs.CentroidListCodec.java

License:Apache License

@Override
public final List<Centroid> decode(final byte[] data) {
    final ByteArrayInputStream bais = new ByteArrayInputStream(data);
    final List<Centroid> list = new ArrayList<>();

    try (final DataInputStream dais = new DataInputStream(bais)) {
        final int listSize = dais.readInt();

        for (int i = 0; i < listSize; i++) {
            final int clusterId = dais.readInt();
            final int vectorSize = dais.readInt();
            final Vector vector = new DenseVector(vectorSize);

            for (int j = 0; j < vectorSize; j++) {
                vector.set(j, dais.readDouble());
            }/*from   ww  w . j ava2  s  .  c om*/

            list.add(new Centroid(clusterId, vector));
        }

    } catch (final IOException e) {
        throw new RuntimeException(e.getCause());
    }

    return list;
}

From source file:edu.snu.cms.reef.ml.kmeans.groupcomm.subs.MapOfIntVSumCodec.java

License:Apache License

@Override
public final Map<Integer, VectorSum> decode(final byte[] data) {
    final ByteArrayInputStream bais = new ByteArrayInputStream(data);
    final Map<Integer, VectorSum> resultMap = new HashMap<>();

    try (final DataInputStream dais = new DataInputStream(bais)) {
        final int mapSize = dais.readInt();

        for (int i = 0; i < mapSize; i++) {
            final int mapInt = dais.readInt();
            final int vectorSize = dais.readInt();
            final Vector vector = new DenseVector(vectorSize);

            for (int j = 0; j < vectorSize; j++) {
                vector.set(j, dais.readDouble());
            }/*from  w  w w.j a v  a 2  s  .c om*/
            final int count = dais.readInt();

            resultMap.put(mapInt, new VectorSum(vector, count));
        }
    } catch (final IOException e) {
        throw new RuntimeException(e.getCause());
    }

    return resultMap;
}

From source file:edu.snu.cms.reef.ml.kmeans.groupcomm.subs.VectorListCodec.java

License:Apache License

public final List<Vector> decode(final byte[] data) {
    final ByteArrayInputStream bais = new ByteArrayInputStream(data);
    final List<Vector> resultList = new ArrayList<>();

    try (final DataInputStream dais = new DataInputStream(bais)) {
        final int listSize = dais.readInt();

        for (int i = 0; i < listSize; i++) {
            final Vector vector = new DenseVector(dais.readInt());

            for (int j = 0; j < vector.size(); j++) {
                vector.set(j, dais.readDouble());
            }/* ww w  . ja  v a 2  s  . c o m*/
            resultList.add(vector);
        }

    } catch (final IOException e) {
        throw new RuntimeException(e.getCause());
    }

    return resultList;
}

From source file:edu.snu.cms.reef.ml.kmeans.MapOfIntVSumCodecTest.java

License:Apache License

@Before
public final void setUp() throws Exception {
    for (int i = 0; i < 1000; i++) {
        final Vector vector = new DenseVector((int) (Math.random() * 1000));
        for (int j = 0; j < vector.size(); j++) {
            vector.set(j, Math.random());
        }//from   w w  w  .ja va 2s  .  c  o m

        map.put(i, new VectorSum(vector, (int) (Math.random() * 1000)));
    }
}

From source file:edu.snu.cms.reef.ml.kmeans.MapOfIntVSumReduceTest.java

License:Apache License

/**
 * Create a random List of Maps//from   w ww  . j a va2  s .  com
 */
@Before
public final void setUp() throws Exception {
    for (int i = 0; i < LIST_SIZE; i++) {
        final Map<Integer, VectorSum> map = new HashMap<>();

        for (int j = 0; j < MAP_SIZE; j++) {
            final Vector vector = new DenseVector(VEC_SIZE);
            for (int k = 0; k < VEC_SIZE; k++) {
                vector.set(k, Math.random());
            }

            map.put(j, new VectorSum(vector, (int) (Math.random() * 100)));

        }

        list.add(map);
    }
}

From source file:edu.snu.cms.reef.ml.kmeans.VectorSumTest.java

License:Apache License

/**
 * Add two random VectorSums with VectorSum.add(),
 * and then check if the result really is the addition of the two VectorSums.
 *//*from   ww w . jav  a2  s . com*/
@Test
public final void testAddSum() {
    final int vectorSize = (int) (Math.random() * 1000);
    final Vector vectorA = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        vectorA.set(i, Math.random());
    }
    final VectorSum vectorSumA = new VectorSum(vectorA, (int) (Math.random() * 1000));

    final Vector vectorB = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        vectorB.set(i, Math.random());
    }
    final VectorSum vectorSumB = new VectorSum(vectorB, (int) (Math.random() * 1000));

    final Vector expectedVector = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        expectedVector.set(i, vectorA.get(i) + vectorB.get(i));
    }
    final VectorSum expectedSum = new VectorSum(expectedVector, vectorSumA.count + vectorSumB.count);

    vectorSumA.add(vectorSumB);

    assertEquals(expectedSum.sum.size(), vectorSumA.sum.size());
    for (int i = 0; i < vectorSize; i++) {
        assertEquals(expectedSum.sum.get(i), vectorSumA.sum.get(i), 0.001);
    }
    assertEquals(expectedSum.count, vectorSumA.count);
}

From source file:edu.snu.cms.reef.ml.kmeans.VectorSumTest.java

License:Apache License

/**
 * Add a random VectorSum and random Vector with VectorSum.add(),
 * and then check if the result really is the addition of the VectorSum and Vector.
 *//*  ww  w .  j  av  a2s.  co  m*/
@Test
public final void testAddVector() {
    final int vectorSize = (int) (Math.random() * 1000);
    final Vector vectorA = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        vectorA.set(i, Math.random());
    }
    final VectorSum vectorSumA = new VectorSum(vectorA, (int) (Math.random() * 1000));

    final Vector vectorB = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        vectorB.set(i, Math.random());
    }

    final Vector expectedVector = new DenseVector(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        expectedVector.set(i, vectorA.get(i) + vectorB.get(i));
    }
    final VectorSum expectedSum = new VectorSum(expectedVector, vectorSumA.count + 1);

    vectorSumA.add(vectorB);

    assertEquals(expectedSum.sum.size(), vectorSumA.sum.size());
    for (int i = 0; i < vectorSize; i++) {
        assertEquals(expectedSum.sum.get(i), vectorSumA.sum.get(i), 0.001);
    }
    assertEquals(expectedSum.count, vectorSumA.count);
}

From source file:edu.snu.dolphin.bsp.examples.ml.data.ClassificationDataParser.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;
        }//  www .  jav a 2  s  .  c  om

        final String[] split = text.split("\\s+");
        if (split.length != dimension + 1) {
            parseException = new ParseException(
                    String.format("Parse failed: the number of features should be %d", dimension));
            return;
        }

        final int output;
        final Vector feature = new DenseVector(split.length);
        try {
            output = Integer.valueOf(split[dimension]);
            if (output != positiveLabel && output != negativeLabel) {
                throw new NumberFormatException();
            }
        } catch (final NumberFormatException e) {
            parseException = new ParseException(String.format(
                    "Parse failed: last column value should be either %d or %d", positiveLabel, negativeLabel));
            return;
        }
        try {
            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));
    }
}