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

功能介绍

分层采样是对每个类别进行随机抽样。

参数说明

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

| strataCol | 分层列 | 分层列 | String | ✓ | | |

| strataRatios | 采用比率 | 采用比率, eg, a:0.1,b:0.3 | String | ✓ | | |

| strataRatio | 采用比率 | 采用比率 | Double | | | -1.0 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. ['a',0.0,0.0],
  6. ['a',0.2,0.1],
  7. ['b',0.2,0.8],
  8. ['b',9.5,9.7],
  9. ['b',9.1,9.6],
  10. ['b',9.3,9.9]
  11. ])
  12. streamData = StreamOperator.fromDataframe(df_data, schemaStr='x1 string, x2 double, x3 double')
  13. sampleStreamOp = StratifiedSampleStreamOp()\
  14. .setStrataCol("x1")\
  15. .setStrataRatios("a:0.5,b:0.5")
  16. sampleStreamOp.linkFrom(streamData).print()
  17. 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.StratifiedSampleStreamOp;
  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 StratifiedSampleStreamOpTest {
  9. @Test
  10. public void testStratifiedSampleStreamOp() throws Exception {
  11. List <Row> df_data = Arrays.asList(
  12. Row.of("a", 0.0, 0.0),
  13. Row.of("a", 0.2, 0.1),
  14. Row.of("b", 0.2, 0.8),
  15. Row.of("b", 9.5, 9.7),
  16. Row.of("b", 9.1, 9.6),
  17. Row.of("b", 9.3, 9.9)
  18. );
  19. StreamOperator <?> streamData = new MemSourceStreamOp(df_data, "x1 string, x2 double, x3 double");
  20. StreamOperator <?> sampleStreamOp = new StratifiedSampleStreamOp()
  21. .setStrataCol("x1")
  22. .setStrataRatios("a:0.5,b:0.5");
  23. sampleStreamOp.linkFrom(streamData).print();
  24. StreamOperator.execute();
  25. }
  26. }

运行结果

| x1 | x2 | x3 | | —- | —- | —- |

| b | 9.3 | 9.9 |

| a | 0.0 | 0.0 |

| b | 0.2 | 0.8 |