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

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

Introduction

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

Prototype

Vector divide(double x);

Source Link

Document

Return a new vector containing the values of the recipient divided by the argument

Usage

From source file:DisplayClustering.java

License:Apache License

/**
 * Identical to plotRectangle(), but with the option of setting the color of
 * the rectangle's stroke./*  ww  w . ja v  a2 s .  com*/
 *
 * NOTE: This should probably be refactored with plotRectangle() since most of
 * the code here is direct copy/paste from that method.
 *
 * @param g2
 *          A Graphics2D context.
 * @param v
 *          A vector for the rectangle's center.
 * @param dv
 *          A vector for the rectangle's dimensions.
 * @param color
 *          The color of the rectangle's stroke.
 */
protected static void plotClusteredRectangle(Graphics2D g2, Vector v, Vector dv, Color color) {
    double[] flip = { 1, -1 };
    Vector v2 = v.times(new DenseVector(flip));
    v2 = v2.minus(dv.divide(2));
    int h = SIZE / 2;
    double x = v2.get(0) + h;
    double y = v2.get(1) + h;

    g2.setStroke(new BasicStroke(1));
    g2.setColor(color);
    g2.draw(new Rectangle2D.Double(x * DS, y * DS, dv.get(0) * DS, dv.get(1) * DS));
}

From source file:DisplayClustering.java

License:Apache License

/**
 * Draw a rectangle on the graphics context
 *
 * @param g2//from  w ww.j a  v a 2  s .c o m
 *          a Graphics2D context
 * @param v
 *          a Vector of rectangle center
 * @param dv
 *          a Vector of rectangle dimensions
 */
protected static void plotRectangle(Graphics2D g2, Vector v, Vector dv) {
    double[] flip = { 1, -1 };
    Vector v2 = v.times(new DenseVector(flip));
    v2 = v2.minus(dv.divide(2));
    int h = SIZE / 2;
    double x = v2.get(0) + h;
    double y = v2.get(1) + h;
    g2.draw(new Rectangle2D.Double(x * DS, y * DS, dv.get(0) * DS, dv.get(1) * DS));
}

From source file:DisplayClustering.java

License:Apache License

/**
 * Draw an ellipse on the graphics context
 *
 * @param g2/*from w w w. j  av  a2 s.com*/
 *          a Graphics2D context
 * @param v
 *          a Vector of ellipse center
 * @param dv
 *          a Vector of ellipse dimensions
 */
protected static void plotEllipse(Graphics2D g2, Vector v, Vector dv) {
    double[] flip = { 1, -1 };
    Vector v2 = v.times(new DenseVector(flip));
    v2 = v2.minus(dv.divide(2));
    int h = SIZE / 2;
    double x = v2.get(0) + h;
    double y = v2.get(1) + h;
    g2.draw(new Ellipse2D.Double(x * DS, y * DS, dv.get(0) * DS, dv.get(1) * DS));
}

From source file:com.cloudera.science.ml.kmeans.core.KMeans.java

License:Open Source License

/**
 * Compute the {@code Vector} that is the centroid of the given weighted points.
 * //w  ww .  jav a  2 s.c  om
 * @param points The weighted points
 * @return The centroid of the weighted points
 */
public <V extends Vector> Vector centroid(Collection<Weighted<V>> points) {
    Vector center = null;
    long sz = 0;
    for (Weighted<V> v : points) {
        Vector weighted = v.thing().times(v.weight());
        if (center == null) {
            center = weighted;
        } else {
            center = center.plus(weighted);
        }
        sz += v.weight();
    }
    return center.divide(sz);
}

From source file:com.cloudera.science.ml.kmeans.core.LloydsUpdateStrategy.java

License:Open Source License

/**
 * Compute the {@code Vector} that is the centroid of the given weighted points.
 * /*from   www  .  ja va 2 s  .  co m*/
 * @param points The weighted points
 * @return The centroid of the weighted points
 */
public <V extends Vector> Vector centroid(Collection<Weighted<V>> points) {
    Vector center = null;
    double sz = 0.0;
    for (Weighted<V> v : points) {
        Vector weighted = v.thing().times(v.weight());
        if (center == null) {
            center = weighted;
        } else {
            center = center.plus(weighted);
        }
        sz += v.weight();
    }
    return center.divide(sz);
}

From source file:org.trustedanalytics.atk.giraph.algorithms.cgd.ConjugateGradientDescentComputation.java

License:Apache License

/**
 * Compute gradient//from  w  w  w.  j a v  a 2s .c om
 *
 * @param bias of type double
 * @param value of type Vector
 * @param messages of type Iterable
 * @return gradient of type Vector
 */
private Vector computeGradient(double bias, Vector value, Iterable<MessageData4CFWritable> messages) {
    Vector xr = value.clone().assign(0d);
    int numTrain = 0;
    for (MessageData4CFWritable message : messages) {
        EdgeType et = message.getType();
        if (et == EdgeType.TRAIN) {
            double weight = message.getWeight();
            Vector vector = message.getVector();
            double otherBias = message.getBias();
            double predict = bias + otherBias + value.dot(vector);
            double e = predict - weight;
            xr = xr.plus(vector.times(e));
            numTrain++;
        }
    }
    Vector gradient = value.clone().assign(0d);
    if (numTrain > 0) {
        gradient = xr.divide(numTrain).plus(value.times(lambda));
    }
    return gradient;
}

From source file:org.trustedanalytics.atk.giraph.algorithms.lda.GiraphLdaComputation.java

License:Apache License

/**
 * Compute the conditional probability of topics given word
 *
 * Each element of the vector contains the conditional probability of topic k given word
 * @param vertex of the graph/*ww  w. ja  va  2s.c  o m*/
 */
private void setTopicGivenWord(Vertex<LdaVertexId, LdaVertexData, LdaEdgeData> vertex) {
    if (vertex.getId().isDocument()) {
        return;
    }

    double wordCount = 0d;

    // LDA result before normalization contains word_count * probability of topic given word and document
    Vector weightedGamma = vertex.getValue().getLdaResult();

    for (Edge<LdaVertexId, LdaEdgeData> edge : vertex.getMutableEdges()) {
        wordCount += edge.getValue().getWordCount();
    }

    Vector topicsGivenWord = new DenseVector(new double[config.numTopics()]);
    if (wordCount > 0d) {
        topicsGivenWord = weightedGamma.divide(wordCount);
    }
    vertex.getValue().setTopicGivenWord(topicsGivenWord);
}