@Override public double[] toArray() { return vector.toArray(); }
@Override public double[] toArray() { return vector.toArray(); }
@Override public double[] toArray() { return this.mainVector.toArray(); }
@Override public void setRowVector(int rowIndex, DoubleVector row) { this.setRow(rowIndex, row.toArray()); }
@Override public void setColumnVector(int col, DoubleVector column) { this.setColumn(col, column.toArray()); }
private static DoubleVector addBias(DoubleVector activations) { return new DenseDoubleVector(1d, activations.toArray()); }
@Override public int extractPredictedClass(DoubleVector predict) { if (predict.getLength() == 1) { return (int) Math.rint(predict.get(0)); } else { return ArrayUtils.maxIndex(predict.toArray()); } }
private DoubleVector computeHiddenActivations(DoubleVector input, DoubleMatrix theta) { // add the bias to the input DoubleVector biased = new DenseDoubleVector(1d, input.toArray()); return activationFunction.apply(theta.multiplyVectorRow(biased)); }
@Override public int extractPredictedClass(DoubleVector predict, double threshold) { if (predict.getLength() == 1) { if (predict.get(0) <= threshold) { return 0; } else { return 1; } } else { return ArrayUtils.maxIndex(predict.toArray()); } }
/** * Transforms the given vector into this vector. * * @param vec a double vector. */ public DenseDoubleVector(DoubleVector vec) { this.vector = new double[vec.getDimension()]; if (vec.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vec.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); this.vector[next.getIndex()] = next.getValue(); } } else { System.arraycopy(vec.toArray(), 0, this.vector, 0, this.vector.length); } }
@Override public double measureDistance(DoubleVector vec1, DoubleVector vec2) { if (vec1.isSparse() || vec2.isSparse()) { return FastMath.sqrt(vec2.subtract(vec1).pow(2).sum()); } else { // dense vectors usually doesn't do a defensive copy, so it is faster than // the implementation above. return measureDistance(vec1.toArray(), vec2.toArray()); } }
@Override public double measureDistance(DoubleVector vec1, DoubleVector vec2) { if (vec1.isSparse() || vec2.isSparse()) { return FastMath.sqrt(vec2.subtract(vec1).pow(2).sum()); } else { // dense vectors usually doesn't do a defensive copy, so it is faster than // the implementation above. return measureDistance(vec1.toArray(), vec2.toArray()); } }
/** * 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; } }
/** * Scales a vector into the interval given by min and max. * * @param input the input vector. * @param fromMin the lower bound of the input interval. * @param fromMax the upper bound of the input interval. * @param toMin the lower bound of the target interval. * @param toMax the upper bound of the target interval. * @return the new vector with scaled values. */ public static DoubleVector minMaxScale(DoubleVector input, double fromMin, double fromMax, double toMin, double toMax) { DoubleVector newOne = new DenseDoubleVector(input.getDimension()); double[] array = input.toArray(); for (int i = 0; i < array.length; i++) { newOne.set(i, minMaxScale(array[i], fromMin, fromMax, toMin, toMax)); } return newOne; }
/** * Generates a single-row matrix with the given first column value (usually * used as a bias) and the given vector as the appendant. * * @param firstColumnValue the value at dimension 0. * @param vector the vector for the following dimensions (1..n+1) */ public DenseDoubleMatrix(double firstColumnValue, DoubleVector vector) { this(1, vector.getDimension() + 1); set(0, 0, firstColumnValue); if (vector.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vector.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); set(0, next.getIndex() + 1, next.getValue()); } } else { System.arraycopy(vector.toArray(), 0, this.matrix, 1, vector.getDimension()); } }
pInput.toArray(), pInput.getLength())); for (int j = 0; j < particlePositions[i].getLength(); j++) { particlePositions[i].set(j, particlePositions[i].get(j)
@Override public DoubleVector predict(DoubleVector features) { if (features.isSparse()) { SparseDoubleVector tmp = new SparseDoubleVector( features.getDimension() + 1); tmp.set(0, 1d); Iterator<DoubleVectorElement> iterateNonZero = features.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); tmp.set(next.getIndex() + 1, next.getValue()); } features = tmp; } else { features = new DenseDoubleVector(1d, features.toArray()); } return new DenseDoubleVector(new double[]{SIGMOID.get().apply( features.dot(theta))}); }
/** * 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; }
/** * @return the normalized matrix (0 mean and stddev of 1) as well as the mean * and the stddev. */ public static Tuple3<DoubleMatrix, DoubleVector, DoubleVector> meanNormalizeColumns( DoubleMatrix x) { DenseDoubleMatrix toReturn = new DenseDoubleMatrix(x.getRowCount(), x.getColumnCount()); final int length = x.getColumnCount(); DoubleVector meanVector = new DenseDoubleVector(length); DoubleVector stddevVector = new DenseDoubleVector(length); for (int col = 0; col < length; col++) { DoubleVector column = x.getColumnVector(col); double mean = column.sum() / column.getLength(); meanVector.set(col, mean); double var = column.subtract(mean).pow(2).sum() / column.getLength(); stddevVector.set(col, Math.sqrt(var)); } for (int col = 0; col < length; col++) { DoubleVector column = x.getColumnVector(col) .subtract(meanVector.get(col)).divide(stddevVector.get(col)); toReturn.setColumn(col, column.toArray()); } return new Tuple3<>(toReturn, meanVector, stddevVector); }