Java 类名:com.alibaba.alink.operator.stream.dataproc.SampleStreamOp
Python 类名:SampleStreamOp

功能介绍

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

    参数说明

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

| 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_data = pd.DataFrame([
  5. ["-0.6264538 0.1836433"],
  6. ["-0.8356286 1.5952808"],
  7. ["0.3295078 -0.8204684"],
  8. ["0.4874291 0.7383247"],
  9. ["0.5757814 -0.3053884"],
  10. ["1.5117812 0.3898432"],
  11. ["-0.6212406 -2.2146999"],
  12. ["11.1249309 9.9550664"],
  13. ["9.9838097 10.9438362"],
  14. ["10.8212212 10.5939013"],
  15. ["10.9189774 10.7821363"],
  16. ["10.0745650 8.0106483"],
  17. ["10.6198257 9.9438713"],
  18. ["9.8442045 8.5292476"],
  19. ["9.5218499 10.4179416"],
  20. ])
  21. data = StreamOperator.fromDataframe(df_data, schemaStr='features string')
  22. sampleOp = SampleStreamOp().setRatio(0.3)
  23. data.link(sampleOp).print()
  24. 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.dataproc.SampleStreamOp;
  4. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class SampleStreamOpTest {
  9. @Test
  10. public void testSampleStreamOp() throws Exception {
  11. List <Row> df_data = Arrays.asList(
  12. Row.of("-0.6264538 0.1836433"),
  13. Row.of("-0.8356286 1.5952808"),
  14. Row.of("0.3295078 -0.8204684"),
  15. Row.of("0.4874291 0.7383247"),
  16. Row.of("0.5757814 -0.3053884"),
  17. Row.of("1.5117812 0.3898432"),
  18. Row.of("-0.6212406 -2.2146999"),
  19. Row.of("11.1249309 9.9550664"),
  20. Row.of("9.9838097 10.9438362"),
  21. Row.of("10.8212212 10.5939013"),
  22. Row.of("10.9189774 10.7821363"),
  23. Row.of("10.0745650 8.0106483"),
  24. Row.of("10.6198257 9.9438713"),
  25. Row.of("9.8442045 8.5292476"),
  26. Row.of("9.5218499 10.4179416")
  27. );
  28. StreamOperator <?> data = new MemSourceStreamOp(df_data, "features string");
  29. StreamOperator <?> sampleOp = new SampleStreamOp().setRatio(0.3);
  30. data.link(sampleOp).print();
  31. StreamOperator.execute();
  32. }
  33. }

运行结果

| features | | —- |

| 10.9189774 10.7821363 |

| 10.0745650 8.0106483 |