Java 类名:com.alibaba.alink.operator.stream.source.LibSvmSourceStreamOp
Python 类名:LibSvmSourceStreamOp

功能介绍

读LibSVM文件。支持从本地、hdfs读取。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| filePath | 文件路径 | 文件路径 | String | ✓ | | |

| startIndex | 起始索引 | 起始索引 | Integer | | | 1 |

代码示例

Python 代码

  1. df_data = pd.DataFrame([
  2. ['1:2.0 2:1.0 4:0.5', 1.5],
  3. ['1:2.0 2:1.0 4:0.5', 1.7],
  4. ['1:2.0 2:1.0 4:0.5', 3.6]
  5. ])
  6. stream_data = StreamOperator.fromDataframe(df_data, schemaStr='f1 string, f2 double')
  7. filepath = '/tmp/abc.svm'
  8. sink = LibSvmSinkStreamOp().setFilePath(filepath).setLabelCol("f2").setVectorCol("f1").setOverwriteSink(True)
  9. stream_data = stream_data.link(sink)
  10. StreamOperator.execute()
  11. stream_data = LibSvmSourceStreamOp().setFilePath(filepath)
  12. stream_data.print()
  13. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.stream.StreamOperator;
  3. import com.alibaba.alink.operator.stream.sink.LibSvmSinkStreamOp;
  4. import com.alibaba.alink.operator.stream.source.LibSvmSourceStreamOp;
  5. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class LibSvmSourceStreamOpTest {
  10. @Test
  11. public void testLibSvmSourceStreamOp() throws Exception {
  12. List <Row> df_data = Arrays.asList(
  13. Row.of("1:2.0 2:1.0 4:0.5", 1.5),
  14. Row.of("1:2.0 2:1.0 4:0.5", 1.7),
  15. Row.of("1:2.0 2:1.0 4:0.5", 3.6)
  16. );
  17. StreamOperator <?> streamData = new MemSourceStreamOp(df_data, "f1 string, f2 double");
  18. String filepath = "/tmp/abc.svm";
  19. StreamOperator <?> sink = new LibSvmSinkStreamOp()
  20. .setFilePath(filepath)
  21. .setLabelCol("f2")
  22. .setVectorCol("f1")
  23. .setOverwriteSink(true);
  24. streamData.link(sink);
  25. StreamOperator.execute();
  26. StreamOperator <?> stream_data = new LibSvmSourceStreamOp().setFilePath(filepath);
  27. stream_data.print();
  28. StreamOperator.execute();
  29. }
  30. }

运行结果

| label | features | | —- | —- |

| 1.7000 | 1:2.0 2:1.0 4:0.5 |

| 3.6000 | 1:2.0 2:1.0 4:0.5 |

| 1.5000 | 1:2.0 2:1.0 4:0.5 |