当前位置: 首页>>代码示例>>Java>>正文


Java DeriveColumnsFromTimeTransform类代码示例

本文整理汇总了Java中org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform的典型用法代码示例。如果您正苦于以下问题:Java DeriveColumnsFromTimeTransform类的具体用法?Java DeriveColumnsFromTimeTransform怎么用?Java DeriveColumnsFromTimeTransform使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DeriveColumnsFromTimeTransform类属于org.datavec.api.transform.transform.time包,在下文中一共展示了DeriveColumnsFromTimeTransform类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDeriveColumnsFromTimeTransform

import org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform; //导入依赖的package包/类
@Test
public void testDeriveColumnsFromTimeTransform() throws Exception {
    Schema schema = new Schema.Builder().addColumnTime("column", DateTimeZone.forID("UTC"))
                    .addColumnString("otherColumn").build();

    Transform transform = new DeriveColumnsFromTimeTransform.Builder("column").insertAfter("otherColumn")
                    .addIntegerDerivedColumn("hour", DateTimeFieldType.hourOfDay())
                    .addIntegerDerivedColumn("day", DateTimeFieldType.dayOfMonth())
                    .addIntegerDerivedColumn("second", DateTimeFieldType.secondOfMinute())
                    .addStringDerivedColumn("humanReadable", "YYYY-MM-dd HH:mm:ss", DateTimeZone.UTC).build();

    transform.setInputSchema(schema);
    Schema out = transform.transform(schema);

    assertEquals(6, out.getColumnMetaData().size());
    TestCase.assertEquals(ColumnType.Time, out.getMetaData(0).getColumnType());
    TestCase.assertEquals(ColumnType.String, out.getMetaData(1).getColumnType());
    TestCase.assertEquals(ColumnType.Integer, out.getMetaData(2).getColumnType());
    TestCase.assertEquals(ColumnType.Integer, out.getMetaData(3).getColumnType());
    TestCase.assertEquals(ColumnType.Integer, out.getMetaData(4).getColumnType());
    TestCase.assertEquals(ColumnType.String, out.getMetaData(5).getColumnType());

    assertEquals("column", out.getName(0));
    assertEquals("otherColumn", out.getName(1));
    assertEquals("hour", out.getName(2));
    assertEquals("day", out.getName(3));
    assertEquals("second", out.getName(4));
    assertEquals("humanReadable", out.getName(5));

    long in1 = 1451651445000L; //"2016-01-01 12:30:45" GMT

    List<Writable> out1 = new ArrayList<>();
    out1.add(new LongWritable(in1));
    out1.add(new Text("otherColumnValue"));
    out1.add(new IntWritable(12)); //hour
    out1.add(new IntWritable(1)); //day
    out1.add(new IntWritable(45)); //second
    out1.add(new Text("2016-01-01 12:30:45"));

    long in2 = 1435708799000L; //"2015-06-30 23:59:59" GMT
    List<Writable> out2 = new ArrayList<>();
    out2.add(new LongWritable(in2));
    out2.add(new Text("otherColumnValue"));
    out2.add(new IntWritable(23)); //hour
    out2.add(new IntWritable(30)); //day
    out2.add(new IntWritable(59)); //second
    out2.add(new Text("2015-06-30 23:59:59"));

    assertEquals(out1,
                    transform.map(Arrays.asList((Writable) new LongWritable(in1), new Text("otherColumnValue"))));
    assertEquals(out2,
                    transform.map(Arrays.asList((Writable) new LongWritable(in2), new Text("otherColumnValue"))));



    //Check serialization: things like DateTimeFormatter etc aren't serializable, hence we need custom serialization :/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(transform);

    byte[] bytes = baos.toByteArray();

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);

    Transform deserialized = (Transform) ois.readObject();
    assertEquals(out1, deserialized
                    .map(Arrays.asList((Writable) new LongWritable(in1), new Text("otherColumnValue"))));
    assertEquals(out2, deserialized
                    .map(Arrays.asList((Writable) new LongWritable(in2), new Text("otherColumnValue"))));
}
 
开发者ID:deeplearning4j,项目名称:DataVec,代码行数:72,代码来源:TestTransforms.java

示例2: testTransforms

import org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform; //导入依赖的package包/类
@Test
public void testTransforms() {

    Map<String, String> map = new HashMap<>();
    map.put("A", "A1");
    map.put("B", "B1");

    Transform[] transforms = new Transform[] {new CategoricalToIntegerTransform("ColName"),
                    new CategoricalToOneHotTransform("ColName"),
                    new IntegerToCategoricalTransform("ColName", Arrays.asList("State0", "State1")),
                    new StringToCategoricalTransform("ColName", Arrays.asList("State0", "State1")),
                    new DuplicateColumnsTransform(Arrays.asList("Dup1", "Dup2"),
                                    Arrays.asList("NewName1", "NewName2")),
                    new RemoveColumnsTransform("R1", "R2"),
                    new RenameColumnsTransform(Arrays.asList("N1", "N2"), Arrays.asList("NewN1", "NewN2")),
                    new ReorderColumnsTransform("A", "B"),
                    new DoubleColumnsMathOpTransform("NewName", MathOp.Subtract, "A", "B"),
                    new DoubleMathOpTransform("ColName", MathOp.Multiply, 2.0),
                    new Log2Normalizer("ColName", 1.0, 0.0, 2.0), new MinMaxNormalizer("ColName", 0, 100, -1, 1),
                    new StandardizeNormalizer("ColName", 20, 5), new SubtractMeanNormalizer("ColName", 10),
                    new IntegerColumnsMathOpTransform("NewName", MathOp.Multiply, "A", "B"),
                    new IntegerMathOpTransform("ColName", MathOp.Add, 10),
                    new ReplaceEmptyIntegerWithValueTransform("Col", 3),
                    new ReplaceInvalidWithIntegerTransform("Col", 3),
                    new LongColumnsMathOpTransform("NewName", MathOp.Multiply, "A", "B"),
                    new LongMathOpTransform("Col", MathOp.ScalarMax, 5L),
                    new MapAllStringsExceptListTransform("Col", "NewVal", Arrays.asList("E1", "E2")),
                    new RemoveWhiteSpaceTransform("Col"), new ReplaceEmptyStringTransform("Col", "WasEmpty"),
                    new ReplaceStringTransform("Col_A", map),
                    new StringListToCategoricalSetTransform("Col", Arrays.asList("A", "B"), Arrays.asList("A", "B"),
                                    ","),
                    new StringMapTransform("Col", map),
                    new DeriveColumnsFromTimeTransform.Builder("TimeColName")
                                    .addIntegerDerivedColumn("Hour", DateTimeFieldType.hourOfDay())
                                    .addStringDerivedColumn("DateTime", "YYYY-MM-dd hh:mm:ss", DateTimeZone.UTC)
                                    .build(),
                    new StringToTimeTransform("TimeCol", "YYYY-MM-dd hh:mm:ss", DateTimeZone.UTC),
                    new TimeMathOpTransform("TimeCol", MathOp.Add, 1, TimeUnit.HOURS)};

    for (Transform t : transforms) {
        String yaml = y.serialize(t);
        String json = j.serialize(t);

        //            System.out.println(yaml);
        //            System.out.println(json);
        //            System.out.println();

        Transform t2 = y.deserializeTransform(yaml);
        Transform t3 = j.deserializeTransform(json);
        assertEquals(t, t2);
        assertEquals(t, t3);
    }


    String tArrAsYaml = y.serialize(transforms);
    String tArrAsJson = j.serialize(transforms);
    String tListAsYaml = y.serializeTransformList(Arrays.asList(transforms));
    String tListAsJson = j.serializeTransformList(Arrays.asList(transforms));

    //        System.out.println("\n\n\n\n");
    //        System.out.println(tListAsYaml);

    List<Transform> lFromYaml = y.deserializeTransformList(tListAsYaml);
    List<Transform> lFromJson = j.deserializeTransformList(tListAsJson);

    assertEquals(Arrays.asList(transforms), y.deserializeTransformList(tArrAsYaml));
    assertEquals(Arrays.asList(transforms), j.deserializeTransformList(tArrAsJson));
    assertEquals(Arrays.asList(transforms), lFromYaml);
    assertEquals(Arrays.asList(transforms), lFromJson);
}
 
开发者ID:deeplearning4j,项目名称:DataVec,代码行数:71,代码来源:TestYamlJsonSerde.java


注:本文中的org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。