Java 类名:com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp
Python 类名:LibSvmSourceBatchOp

功能介绍

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

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
filePath 文件路径 文件路径 String
startIndex 起始索引 起始索引 Integer 1

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. ['1:2.0 2:1.0 4:0.5', 1.5],
  6. ['1:2.0 2:1.0 4:0.5', 1.7],
  7. ['1:2.0 2:1.0 4:0.5', 3.6]
  8. ])
  9. batch_data = BatchOperator.fromDataframe(df_data, schemaStr='f1 string, f2 double')
  10. filepath = '/tmp/abc.svm'
  11. sink = LibSvmSinkBatchOp().setFilePath(filepath).setLabelCol("f2").setVectorCol("f1").setOverwriteSink(True)
  12. batch_data = batch_data.link(sink)
  13. BatchOperator.execute()
  14. batch_data = LibSvmSourceBatchOp().setFilePath(filepath)
  15. batch_data.print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.sink.LibSvmSinkBatchOp;
  4. import com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class LibSvmSourceBatchOpTest {
  10. @Test
  11. public void testLibSvmSourceBatchOp() 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. BatchOperator <?> batch_data = new MemSourceBatchOp(df_data, "f1 string, f2 double");
  18. String filepath = "/tmp/abc.svm";
  19. BatchOperator <?> sink = new LibSvmSinkBatchOp().setFilePath(filepath).setLabelCol("f2").setVectorCol("f1")
  20. .setOverwriteSink(true);
  21. batch_data = batch_data.link(sink);
  22. BatchOperator.execute();
  23. batch_data = new LibSvmSourceBatchOp().setFilePath(filepath);
  24. batch_data.print();
  25. }
  26. }

运行结果

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

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

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

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