use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class SVMModelTest method testPredictWithErasedLabelsAndChangedThreshold.
/**
*/
@Test
public void testPredictWithErasedLabelsAndChangedThreshold() {
Vector weights = new DenseVector(new double[] { 1.0, 1.0 });
SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(weights, 1.0).withThreshold(5);
Vector observation = new DenseVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(0.0, mdl.predict(observation), PRECISION);
observation = new DenseVector(new double[] { 3.0, 4.0 });
TestUtils.assertEquals(1.0, mdl.predict(observation), PRECISION);
TestUtils.assertEquals(5, mdl.threshold(), PRECISION);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class SparkModelParser method loadKMeansModel.
/**
* Load K-Means model.
*
* @param pathToMdl Path to model.
* @param learningEnvironment learningEnvironment
*/
private static Model loadKMeansModel(String pathToMdl, LearningEnvironment learningEnvironment) {
Vector[] centers = null;
try (ParquetFileReader r = ParquetFileReader.open(HadoopInputFile.fromPath(new Path(pathToMdl), new Configuration()))) {
PageReadStore pages;
final MessageType schema = r.getFooter().getFileMetaData().getSchema();
final MessageColumnIO colIO = new ColumnIOFactory().getColumnIO(schema);
while (null != (pages = r.readNextRowGroup())) {
final int rows = (int) pages.getRowCount();
final RecordReader recordReader = colIO.getRecordReader(pages, new GroupRecordConverter(schema));
centers = new DenseVector[rows];
for (int i = 0; i < rows; i++) {
final SimpleGroup g = (SimpleGroup) recordReader.read();
// final int clusterIdx = g.getInteger(0, 0);
Group clusterCenterCoeff = g.getGroup(1, 0).getGroup(3, 0);
final int amountOfCoefficients = clusterCenterCoeff.getFieldRepetitionCount(0);
centers[i] = new DenseVector(amountOfCoefficients);
for (int j = 0; j < amountOfCoefficients; j++) {
double coefficient = clusterCenterCoeff.getGroup(0, j).getDouble(0, 0);
centers[i].set(j, coefficient);
}
}
}
} catch (IOException e) {
String msg = "Error reading parquet file: " + e.getMessage();
learningEnvironment.logger().log(MLLogger.VerboseLevel.HIGH, msg);
e.printStackTrace();
}
return new KMeansModel(centers, new EuclideanDistance());
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class SparkModelParser method readLinRegCoefficients.
/**
* Read coefficient matrix from parquet.
*
* @param g Coefficient group.
* @return Vector of coefficients.
*/
private static Vector readLinRegCoefficients(SimpleGroup g) {
Vector coefficients;
Group coeffGroup = g.getGroup(1, 0).getGroup(3, 0);
final int amountOfCoefficients = coeffGroup.getFieldRepetitionCount(0);
coefficients = new DenseVector(amountOfCoefficients);
for (int j = 0; j < amountOfCoefficients; j++) {
double coefficient = coeffGroup.getGroup(0, j).getDouble(0, 0);
coefficients.set(j, coefficient);
}
return coefficients;
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class SparkModelParser method readCoefficients.
/**
* Read coefficient matrix from parquet.
*
* @param g Coefficient group.
* @return Vector of coefficients.
*/
private static Vector readCoefficients(SimpleGroup g) {
Vector coefficients;
final int amountOfCoefficients = g.getGroup(3, 0).getGroup(5, 0).getFieldRepetitionCount(0);
coefficients = new DenseVector(amountOfCoefficients);
for (int j = 0; j < amountOfCoefficients; j++) {
double coefficient = g.getGroup(3, 0).getGroup(5, 0).getGroup(0, j).getDouble(0, 0);
coefficients.set(j, coefficient);
}
return coefficients;
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class LocalModelsTest method importExportSVMBinaryClassificationModelTest.
/**
*/
@Test
public void importExportSVMBinaryClassificationModelTest() throws IOException {
executeModelTest(mdlFilePath -> {
SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(new DenseVector(new double[] { 1, 2 }), 3);
Exporter<SVMLinearClassificationModel, String> exporter = new FileExporter<>();
mdl.saveModel(exporter, mdlFilePath);
SVMLinearClassificationModel load = exporter.load(mdlFilePath);
Assert.assertNotNull(load);
Assert.assertEquals("", mdl, load);
return null;
});
}
Aggregations