/** * @return a new vector with dimension num and a default value of 1. */ public static DenseDoubleVector ones(int num) { return new DenseDoubleVector(num, 1.0d); }
@Override public final DoubleVector add(double scalar) { DoubleVector newv = new DenseDoubleVector(this.getLength()); for (int i = 0; i < this.getLength(); i++) { newv.set(i, this.get(i) + scalar); } return newv; }
private DenseDoubleVector getProbabilityDistribution(DoubleVector document) { int numClasses = classPriorProbability.getLength(); DenseDoubleVector distribution = new DenseDoubleVector(numClasses); // loop through all classes and get the max probable one for (int i = 0; i < numClasses; i++) { double probability = getProbabilityForClass(document, i); distribution.set(i, probability); } double maxProbability = distribution.max(); double probabilitySum = 0.0d; // we normalize it back for (int i = 0; i < numClasses; i++) { double probability = distribution.get(i); double normalizedProbability = FastMath.exp(probability - maxProbability + classPriorProbability.get(i)); distribution.set(i, normalizedProbability); probabilitySum += normalizedProbability; } // since the sum is sometimes not 1, we need to divide by the sum distribution = (DenseDoubleVector) distribution.divide(probabilitySum); return distribution; }
@Override public final DoubleVector subtract(double v) { DenseDoubleVector newv = new DenseDoubleVector(vector.length); for (int i = 0; i < vector.length; i++) { newv.set(i, vector[i] - v); } return newv; }
@Override public DoubleVector divide(double scalar) { if (scalar == 0d) { throw new java.lang.ArithmeticException("/ by zero"); } DenseDoubleVector v = new DenseDoubleVector(this.getLength()); for (int i = 0; i < v.getLength(); i++) { v.set(i, this.get(i) / scalar); } return v; }
/** * @return the transition probabilities for the states. */ public DoubleVector getTransitionProbabilities(int[] stateSequence) { DenseDoubleVector distribution = new DenseDoubleVector( stateSequence.length - 1); for (int i = 0; i < distribution.getDimension(); i++) { distribution.set(i, transitionProbabilities.get(stateSequence[i], stateSequence[i + 1])); } return distribution; }
/** * @return a new vector filled from index, to index, with a given stepsize. */ public static DenseDoubleVector fromUpTo(double from, double to, double stepsize) { DenseDoubleVector v = new DenseDoubleVector( (int) (FastMath.round(((to - from) / stepsize) + 0.5))); for (int i = 0; i < v.getLength(); i++) { v.set(i, from + i * stepsize); } return v; }
/** * @return If the number of outcomes is 2 (binary prediction) the returned * vector contains the class id (0 or 1) at the first index. If not, a * histogram of the classes that were predicted. */ @Override public DoubleVector predict(DoubleVector features) { List<VectorDistanceTuple<DoubleVector>> nearestNeighbours = getNearestNeighbours( features, k); DenseDoubleVector outcomeHistogram = new DenseDoubleVector(numOutcomes); for (VectorDistanceTuple<DoubleVector> tuple : nearestNeighbours) { int classIndex = 0; if (numOutcomes == 2) { classIndex = (int) tuple.getValue().get(0); } else { classIndex = tuple.getValue().maxIndex(); } outcomeHistogram.set(classIndex, outcomeHistogram.get(classIndex) + 1); } if (numOutcomes == 2) { return new SingleEntryDoubleVector(outcomeHistogram.maxIndex()); } else { return outcomeHistogram; } }
@Override public DoubleVector sqrt() { DoubleVector v = new DenseDoubleVector(this.getLength()); for (int i = 0; i < v.getLength(); i++) { v.set(i, FastMath.sqrt(vector[i])); } return v; }
/** * Returns the indices of the relevant items that are above the threshold. */ static int[] filterRelevantItems(DenseDoubleVector relevanceScores, double threshold) { TIntArrayList list = new TIntArrayList(); for (int i = 0; i < relevanceScores.getLength(); i++) { double val = relevanceScores.get(i); if (val > threshold) { list.add(i); } } return list.toArray(); }
/** * Creates a new matrix consisting out of polynomials of the input matrix.<br/> * Considering you want to do a 2 polynomial out of 3 columns you get:<br/> * (SEED: x^1 | y^1 | z^1 )| x^2 | y^2 | z^2 for the columns of the returned * matrix. * * @param seed matrix to add polynoms of it. * @param num how many polynoms, 2 for quadratic, 3 for cubic and so forth. * @return the new matrix. */ public static DenseDoubleMatrix createPolynomials(DenseDoubleMatrix seed, int num) { if (num == 1) return seed; DenseDoubleMatrix m = new DenseDoubleMatrix(seed.getRowCount(), seed.getColumnCount() * num); int index = 0; for (int c = 0; c < m.getColumnCount(); c += num) { double[] column = seed.getColumn(index++); m.setColumn(c, column); for (int i = 2; i < num + 1; i++) { DoubleVector pow = new DenseDoubleVector(column).pow(i); m.setColumn(c + i - 1, pow.toArray()); } } return m; }
@Override public void train(DoubleVector[] features, DoubleVector[] outcome) { Preconditions.checkArgument(features.length == outcome.length, "Features and Outcomes need to match in length!"); DoubleMatrix x = null; DoubleMatrix y = null; // add the bias if (features[0].isSparse()) { x = new SparseDoubleRowMatrix(DenseDoubleVector.ones(features.length), new SparseDoubleRowMatrix(features)); } else { x = new DenseDoubleMatrix(DenseDoubleVector.ones(features.length), new DenseDoubleMatrix(features)); } if (outcome[0].isSparse()) { y = new SparseDoubleRowMatrix(outcome); } else { y = new DenseDoubleMatrix(outcome); } // transpose y to get a faster lookup in the cost function y = y.transpose(); LogisticRegressionCostFunction cnf = new LogisticRegressionCostFunction(x, y, lambda); // random init theta theta = new DenseDoubleVector(x.getColumnCount() * y.getRowCount()); for (int i = 0; i < theta.getDimension(); i++) { theta.set(i, (random.nextDouble() * 2) - 1d); } theta = minimizer.minimize(cnf, theta, numIterations, verbose); }
@Override public double min() { double min = Double.MAX_VALUE; for (int i = 0; i < getLength(); i++) { double d = vector[i]; if (d < min) { min = d; } } return min; }
/** * Creates a new matrix with the given vector into the first column and the * other matrix to the other columns. This is usually used in machine learning * algorithms that add a bias on the zero-index column. * * @param first the new first column. * @param otherMatrix the other matrix to set on from the second column. */ public DenseDoubleMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) { this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1); // copy the first column System.arraycopy(first.toArray(), 0, matrix, 0, first.getDimension()); int offset = first.getDimension(); for (int col : otherMatrix.columnIndices()) { double[] clv = otherMatrix.getColumnVector(col).toArray(); System.arraycopy(clv, 0, matrix, offset, clv.length); offset += clv.length; } }
@Override public final DoubleVector subtractFrom(double v) { DenseDoubleVector newv = new DenseDoubleVector(vector.length); for (int i = 0; i < vector.length; i++) { newv.set(i, v - vector[i]); } return newv; }
/** * @return a log'd matrix that was guarded against edge cases of the * logarithm. */ public static DoubleVector logVector(DoubleVector input) { DenseDoubleVector log = new DenseDoubleVector(input.getDimension()); for (int col = 0; col < log.getDimension(); col++) { log.set(col, guardedLogarithm(input.get(col))); } return log; }
@Override public DoubleVector pow(double x) { DenseDoubleVector v = new DenseDoubleVector(this.getLength()); for (int i = 0; i < v.getLength(); i++) { double value = 0.0d; // it is faster to multiply when we having ^2 if (x == 2d) { value = vector[i] * vector[i]; } else { value = FastMath.pow(vector[i], x); } v.set(i, value); } return v; }
@Override public DoubleVector log() { DoubleVector v = new DenseDoubleVector(getLength()); for (int i = 0; i < v.getLength(); i++) { v.set(i, FastMath.log(vector[i])); } return v; }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; if (s.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = s.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); dotProduct += this.get(next.getIndex()) * next.getValue(); } } else { for (int i = 0; i < getLength(); i++) { dotProduct += this.get(i) * s.get(i); } } return dotProduct; }
@Override public double max() { double max = -Double.MAX_VALUE; for (int i = 0; i < getLength(); i++) { double d = vector[i]; if (d > max) { max = d; } } return max; }