use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class ANNClassificationTrainer method getCentroidStat.
/**
*/
private <K, V> CentroidStat getCentroidStat(DatasetBuilder<K, V> datasetBuilder, Preprocessor<K, V> vectorizer, List<Vector> centers) {
PartitionDataBuilder<K, V, EmptyContext, LabeledVectorSet<LabeledVector>> partDataBuilder = new LabeledDatasetPartitionDataBuilderOnHeap<>(vectorizer);
try (Dataset<EmptyContext, LabeledVectorSet<LabeledVector>> dataset = datasetBuilder.build(envBuilder, (env, upstream, upstreamSize) -> new EmptyContext(), partDataBuilder, learningEnvironment())) {
return dataset.compute(data -> {
CentroidStat res = new CentroidStat();
for (int i = 0; i < data.rowSize(); i++) {
final IgniteBiTuple<Integer, Double> closestCentroid = findClosestCentroid(centers, data.getRow(i));
int centroidIdx = closestCentroid.get1();
double lb = data.label(i);
// add new label to label set
res.labels().add(lb);
ConcurrentHashMap<Double, Integer> centroidStat = res.centroidStat.get(centroidIdx);
if (centroidStat == null) {
centroidStat = new ConcurrentHashMap<>();
centroidStat.put(lb, 1);
res.centroidStat.put(centroidIdx, centroidStat);
} else {
int cnt = centroidStat.getOrDefault(lb, 0);
centroidStat.put(lb, cnt + 1);
}
res.counts.merge(centroidIdx, 1, (IgniteBiFunction<Integer, Integer, Integer>) (i1, i2) -> i1 + i2);
}
return res;
}, (a, b) -> {
if (a == null)
return b == null ? new CentroidStat() : b;
if (b == null)
return a;
return a.merge(b);
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class MatrixUtil method zipFoldByColumns.
/**
* Zips two matrices by column-by-column with specified function. Result is matrix same flavour as first matrix.
*
* @param mtx1 First matrix.
* @param mtx2 Second matrix.
* @param fun Function to zip with.
* @return Vector consisting from values resulted from zipping column-by-column.
*/
public static Vector zipFoldByColumns(Matrix mtx1, Matrix mtx2, IgniteBiFunction<Vector, Vector, Double> fun) {
int cols = Math.min(mtx1.columnSize(), mtx2.columnSize());
Vector vec = mtx1.likeVector(cols);
for (int i = 0; i < cols; i++) vec.setX(i, fun.apply(mtx1.getCol(i), mtx2.getCol(i)));
return vec;
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class MatrixUtil method zipFoldByRows.
/**
* Zips two matrices by row-by-row with specified function. Result is matrix same flavour as first matrix.
*
* @param mtx1 First matrix.
* @param mtx2 Second matrix.
* @param fun Function to zip with.
* @return Vector consisting from values resulted from zipping row-by-row.
*/
public static Vector zipFoldByRows(Matrix mtx1, Matrix mtx2, IgniteBiFunction<Vector, Vector, Double> fun) {
int rows = Math.min(mtx1.rowSize(), mtx2.rowSize());
Vector vec = mtx1.likeVector(rows);
for (int i = 0; i < rows; i++) vec.setX(i, fun.apply(mtx1.viewRow(i), mtx2.viewRow(i)));
return vec;
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class AbstractMatrix method getCol.
/**
* {@inheritDoc}
*/
@Override
public Vector getCol(int col) {
checkColumnIndex(col);
Vector res;
res = new DenseVector(rowSize());
for (int i = 0; i < rowSize(); i++) res.setX(i, getX(i, col));
return res;
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class AbstractMatrix method getRow.
/**
* {@inheritDoc}
*/
@Override
public Vector getRow(int row) {
checkRowIndex(row);
Vector res = new DenseVector(columnSize());
for (int i = 0; i < columnSize(); i++) res.setX(i, getX(row, i));
return res;
}
Aggregations