use of org.apache.sysml.api.mlcontext.MLResults in project incubator-systemml by apache.
the class MLContextTest method testOutputDataFramePYDMLVectorWithIDColumn.
@Test
public void testOutputDataFramePYDMLVectorWithIDColumn() {
System.out.println("MLContextTest - output DataFrame PYDML, vector with ID column");
String s = "M = full('1 2 3 4', rows=2, cols=2)";
Script script = pydml(s).out("M");
MLResults results = ml.execute(script);
Dataset<Row> dataFrame = results.getDataFrameVectorWithIDColumn("M");
List<Row> list = dataFrame.collectAsList();
Row row1 = list.get(0);
Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
Assert.assertArrayEquals(new double[] { 1.0, 2.0 }, ((Vector) row1.get(1)).toArray(), 0.0);
Row row2 = list.get(1);
Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
Assert.assertArrayEquals(new double[] { 3.0, 4.0 }, ((Vector) row2.get(1)).toArray(), 0.0);
}
use of org.apache.sysml.api.mlcontext.MLResults in project incubator-systemml by apache.
the class MLContextTest method testOutputJavaRDDStringCSVSparseDML.
/**
* Reading from dense and sparse matrices is handled differently, so we have
* tests for both dense and sparse matrices.
*/
@Test
public void testOutputJavaRDDStringCSVSparseDML() {
System.out.println("MLContextTest - output Java RDD String CSV Sparse DML");
String s = "M = matrix(0, rows=10, cols=10); M[1,1]=1; M[1,2]=2; M[2,1]=3; M[2,2]=4; print(toString(M));";
Script script = dml(s).out("M");
MLResults results = ml.execute(script);
JavaRDD<String> javaRDDStringCSV = results.getJavaRDDStringCSV("M");
List<String> lines = javaRDDStringCSV.collect();
Assert.assertEquals("1.0,2.0", lines.get(0));
Assert.assertEquals("3.0,4.0", lines.get(1));
}
use of org.apache.sysml.api.mlcontext.MLResults in project incubator-systemml by apache.
the class MLContextTest method testOutputDataFramePYDML.
@Test
public void testOutputDataFramePYDML() {
System.out.println("MLContextTest - output DataFrame PYDML");
String s = "M = full('1 2 3 4', rows=2, cols=2)";
Script script = pydml(s).out("M");
MLResults results = ml.execute(script);
Dataset<Row> dataFrame = results.getDataFrame("M");
List<Row> list = dataFrame.collectAsList();
Row row1 = list.get(0);
Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
Assert.assertEquals(1.0, row1.getDouble(1), 0.0);
Assert.assertEquals(2.0, row1.getDouble(2), 0.0);
Row row2 = list.get(1);
Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
Assert.assertEquals(3.0, row2.getDouble(1), 0.0);
Assert.assertEquals(4.0, row2.getDouble(2), 0.0);
}
use of org.apache.sysml.api.mlcontext.MLResults in project incubator-systemml by apache.
the class MLContextTest method testOutputDataFrameOfVectorsDML.
@Test
public void testOutputDataFrameOfVectorsDML() {
System.out.println("MLContextTest - output DataFrame of vectors DML");
String s = "m=matrix('1 2 3 4',rows=2,cols=2);";
Script script = dml(s).out("m");
MLResults results = ml.execute(script);
Dataset<Row> df = results.getDataFrame("m", true);
Dataset<Row> sortedDF = df.sort(RDDConverterUtils.DF_ID_COLUMN);
// verify column types
StructType schema = sortedDF.schema();
StructField[] fields = schema.fields();
StructField idColumn = fields[0];
StructField vectorColumn = fields[1];
Assert.assertTrue(idColumn.dataType() instanceof DoubleType);
Assert.assertTrue(vectorColumn.dataType() instanceof VectorUDT);
List<Row> list = sortedDF.collectAsList();
Row row1 = list.get(0);
Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
Vector v1 = (DenseVector) row1.get(1);
double[] arr1 = v1.toArray();
Assert.assertArrayEquals(new double[] { 1.0, 2.0 }, arr1, 0.0);
Row row2 = list.get(1);
Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
Vector v2 = (DenseVector) row2.get(1);
double[] arr2 = v2.toArray();
Assert.assertArrayEquals(new double[] { 3.0, 4.0 }, arr2, 0.0);
}
use of org.apache.sysml.api.mlcontext.MLResults in project incubator-systemml by apache.
the class MLContextTest method testOutputDataFrameDMLDoublesWithIDColumn.
@Test
public void testOutputDataFrameDMLDoublesWithIDColumn() {
System.out.println("MLContextTest - output DataFrame DML, doubles with ID column");
String s = "M = matrix('1 2 3 4', rows=2, cols=2);";
Script script = dml(s).out("M");
MLResults results = ml.execute(script);
Dataset<Row> dataFrame = results.getDataFrameDoubleWithIDColumn("M");
List<Row> list = dataFrame.collectAsList();
Row row1 = list.get(0);
Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
Assert.assertEquals(1.0, row1.getDouble(1), 0.0);
Assert.assertEquals(2.0, row1.getDouble(2), 0.0);
Row row2 = list.get(1);
Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
Assert.assertEquals(3.0, row2.getDouble(1), 0.0);
Assert.assertEquals(4.0, row2.getDouble(2), 0.0);
}
Aggregations