Refine search
protected double getPercentile(Number quantile, INDArray sorted) { if (quantile.intValue() == 0) return sorted.getDouble(0); else if (quantile.intValue() == 100) return sorted.getDouble(sorted.length() - 1); double pos = (quantile.doubleValue() / 100.0) * (double) (sorted.length() + 1); double fposition = FastMath.floor(pos); int position = (int) fposition; double diff = pos - fposition; double lower = sorted.getDouble(position - 1); double upper = sorted.getDouble(position); return lower + diff * (upper - lower); }
protected double getPercentile(Number quantile, INDArray sorted) { if (quantile.intValue() == 0) return sorted.getDouble(0); else if (quantile.intValue() == 100) return sorted.getDouble(sorted.length() - 1); double pos = (quantile.doubleValue() / 100.0) * (double) (sorted.length() + 1); double fposition = FastMath.floor(pos); int position = (int)fposition; double diff = pos - fposition; double lower = sorted.getDouble(position-1); double upper = sorted.getDouble(position); return lower + diff * (upper - lower); }
static INDArray find(INDArray arr1, Predicate<Double> predicate) { List<Double> values = new ArrayList<>(); for(int i = 0; i < arr1.length(); i++) { Double value = arr1.getDouble(i); if(predicate.test(value)) { values.add(value); } } INDArray result = Nd4j.create(new int[]{values.size()}); for(int i = 0; i < values.size(); i++) { result.putScalar(i, values.get(i)); } return result; }
static INDArray compare(INDArray arr1, INDArray arr2, Predicate<Boolean []> predicate) { INDArray result = Nd4j.create(arr1.shape()); for (int i = 0; i < arr1.length(); i++) { boolean answer = predicate.test(new Boolean[]{arr1.getDouble(i) == 1.0, arr2.getDouble(i) == 1.0}); result.putScalar(i, answer ? 1.0 : 0.0); } return result; }
/** * Given a sequence of Iterators over a transform of matrices, fill in all of * the matrices with the entries in the theta vector. Errors are * thrown if the theta vector does not exactly fill the matrices. */ public static void setParams(INDArray theta, Iterator<? extends INDArray>... matrices) { int index = 0; for (Iterator<? extends INDArray> matrixIterator : matrices) { while (matrixIterator.hasNext()) { INDArray matrix = matrixIterator.next().linearView(); for (int i = 0; i < matrix.length(); i++) { matrix.putScalar(i, theta.getDouble(index)); index++; } } } if (index != theta.length()) { throw new AssertionError("Did not entirely use the theta vector"); } }
/** * Given a sequence of Iterators over a transform of matrices, fill in all of * the matrices with the entries in the theta vector. Errors are * thrown if the theta vector does not exactly fill the matrices. */ public static void setParams(INDArray theta, Collection<INDArray>... matrices) { int index = 0; for (Collection<INDArray> matrixCollection : matrices) { for (INDArray matrix : matrixCollection) { INDArray linear = matrix.linearView(); for (int i = 0; i < matrix.length(); i++) { linear.putScalar(i, theta.getDouble(index)); index++; } } } if (index != theta.length()) { throw new AssertionError("Did not entirely use the theta vector"); } }
private String vectorToString(INDArray arr, boolean summarize) { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < arr.length(); i++) { if (arr instanceof IComplexNDArray) { sb.append(((IComplexNDArray) arr).getComplex(i).toString()); } else { if (summarize && i > 2 && i < arr.length() - 3) { if (i == 3) sb.append(" ..."); } else { double arrElement = arr.getDouble(i); if (!dontOverrideFormat && ((Math.abs(arrElement) < this.minToPrintWithoutSwitching && arrElement!= 0) || (Math.abs(arrElement) >= this.maxToPrintWithoutSwitching))) { if (i < arr.length() - 1) { if (!summarize || i < 2 || i > arr.length() - 3 || (summarize && arr.length() == 6)) { sb.append(colSep);
/** * Or over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] or(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); // FIXME: int cast boolean[] result = new boolean[(int) arr.length()]; for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) > 0) result[i] = true; else result[i] = false; } return result; }
@Override public List<long[]> calculateOutputShape() { int numArgs = args().length; if(numArgs < 1) return Collections.emptyList(); val shape = args()[0].getArr(); if(shape == null) return Collections.emptyList(); else { if(shape.length() == 1) { if(shape.getDouble(0) < 1) return Arrays.asList(new long[]{1,1}); else return Arrays.asList(new long[]{1,shape.getInt(0)}); } } return Arrays.asList(shape.data().asLong()); }
/** * And over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] and(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); boolean[] result = new boolean[(int) arr.length()]; long tadLength = Shape.getTADLength(n.shape(), dimension); for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) == tadLength) result[i] = true; else result[i] = false; } return result; }
/** * * @param real */ protected void copyFromReal(INDArray real) { if (!Shape.shapeEquals(shape(), real.shape())) throw new IllegalStateException("Unable to copy array. Not the same shape"); INDArray linear = real.linearView(); IComplexNDArray thisLinear = linearView(); for (int i = 0; i < linear.length(); i++) { thisLinear.putScalar(i, Nd4j.createComplexNumber(linear.getDouble(i), 0.0)); } }
System.out.println("Num. Dimensions: " + myArray.rank()); //2 dimensions -> rank 2 System.out.println("Shape: " + Arrays.toString(myArray.shape())); //[3,5] -> 3 rows, 5 columns System.out.println("Length: " + myArray.length()); // 3 rows * 5 columns = 15 total elements double val0 = myArray.getDouble(0, 1); //Get the value at row 0, column 1 - expect value 2.0 as we set this earlier System.out.println("\nValue at (0,1): " + val0);
/** * Returns the double data * for this ndarray. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the double data for this ndarray */ public static double[] getDoubleData(INDArray buf) { if (buf.data().dataType() != DataBuffer.Type.DOUBLE) throw new IllegalArgumentException("Double data must be obtained from a double buffer"); if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.data().asDouble(); } else { double[] ret = new double[(int) buf.length()]; INDArray linear = buf.linearView(); for (int i = 0; i < buf.length(); i++) ret[i] = linear.getDouble(i); return ret; } }
@Override public List<long[]> calculateOutputShape() { /** * This op is special case: we can't infer its shape before both inputs are available. * So if reps argument is full of 0.0s - we skip shape inference * * And during actual op invocation both inputs should be available due to topo sort */ if (is_static_reps) return Nd4j.getExecutioner().calculateOutputShape(this); if (inputArguments().length < 2) return Collections.emptyList(); val array = inputArguments()[1]; // FIXME: int cast val reps = new long[(int) array.length()]; for (int e = 0; e < reps.length; e++) reps[e] = (int) array.getDouble(e); if (ArrayUtil.prodLong(reps) == 0) return Collections.emptyList(); else return Nd4j.getExecutioner().calculateOutputShape(this); }
double arrElement = arr.getDouble(0); if (!dontOverrideFormat && ((Math.abs(arrElement) < this.minToPrintWithoutSwitching && arrElement!= 0) || (Math.abs(arrElement) >= this.maxToPrintWithoutSwitching))) { if (arr.getDouble(0) == 0) return "0"; return decimalFormat.format(arr.getDouble(0)); } else if(arr.rank() <= 1 || arr.length() == 1) { sb.append(format(Nd4j.scalar(arr.getDouble(0)),offset,summarize));
INDArray vec = ndarray.vectorAlongDimension(i, dimension); INDArray indexVector = indices.vectorAlongDimension(i, dimension); final Double[] data = new Double[(int) vec.length()]; final Double[] index = new Double[(int) vec.length()]; for (int j = 0; j < vec.length(); j++) { data[j] = vec.getDouble(j); index[j] = (double) j;
/** * Binarizes the dataset such that any number greater than cutoff is 1 otherwise zero * * @param cutoff the cutoff point */ @Override public void binarize(double cutoff) { INDArray linear = getFeatureMatrix().linearView(); for (int i = 0; i < getFeatures().length(); i++) { double curr = linear.getDouble(i); if (curr > cutoff) getFeatures().putScalar(i, 1); else getFeatures().putScalar(i, 0); } }