Java 类名:com.alibaba.alink.operator.batch.dataproc.SampleBatchOp
Python 类名:SampleBatchOp

功能介绍

  • 本算子对数据进行随机抽样,每个样本都以相同的概率被抽到。

    参数说明

    | 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- | | ratio | 采样比例 | 采样率,范围为[0, 1] | Double | ✓ | [0.0, 1.0] | | | withReplacement | 是否放回 | 是否有放回的采样,默认不放回 | Boolean | | | false |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["0,0,0"],
  6. ["0.1,0.1,0.1"],
  7. ["0.2,0.2,0.2"],
  8. ["9,9,9"],
  9. ["9.1,9.1,9.1"],
  10. ["9.2,9.2,9.2"]
  11. ])
  12. inOp = BatchOperator.fromDataframe(df, schemaStr='Y string')
  13. sampleOp = SampleBatchOp()\
  14. .setRatio(0.3)\
  15. .setWithReplacement(False)
  16. inOp.link(sampleOp).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.dataproc.SampleBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class SampleBatchOpTest {
  9. @Test
  10. public void testSampleBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("0,0,0"),
  13. Row.of("0.1,0.1,0.1"),
  14. Row.of("0.2,0.2,0.2"),
  15. Row.of("9,9,9"),
  16. Row.of("9.1,9.1,9.1"),
  17. Row.of("9.2,9.2,9.2")
  18. );
  19. BatchOperator <?> inOp = new MemSourceBatchOp(df, "Y string");
  20. BatchOperator <?> sampleOp = new SampleBatchOp()
  21. .setRatio(0.3)
  22. .setWithReplacement(false);
  23. inOp.link(sampleOp).print();
  24. }
  25. }

运行结果

| Y | | —- |

| 0,0,0 |

| 0.2,0.2,0.2 |