该文档涉及的组件

随机采样 (SampleStreamOp)

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

Python 类名:SampleStreamOp

功能介绍

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

参数说明

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

代码示例

Python 代码

from pyalink.alink import *

import pandas as pd

useLocalEnv(1)

df_data = pd.DataFrame([
    ["-0.6264538 0.1836433"],
    ["-0.8356286 1.5952808"],
    ["0.3295078 -0.8204684"],
    ["0.4874291 0.7383247"],
    ["0.5757814 -0.3053884"],
    ["1.5117812 0.3898432"],
    ["-0.6212406 -2.2146999"],
    ["11.1249309 9.9550664"],
    ["9.9838097 10.9438362"],
    ["10.8212212 10.5939013"],
    ["10.9189774 10.7821363"],
    ["10.0745650 8.0106483"],
    ["10.6198257 9.9438713"],
    ["9.8442045 8.5292476"],
    ["9.5218499 10.4179416"],
])

data = StreamOperator.fromDataframe(df_data, schemaStr='features string')

sampleOp = SampleStreamOp().setRatio(0.3)

data.link(sampleOp).print()

StreamOperator.execute()

Java 代码

import org.apache.flink.types.Row;

import com.alibaba.alink.operator.stream.StreamOperator;
import com.alibaba.alink.operator.stream.dataproc.SampleStreamOp;
import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class SampleStreamOpTest {
	@Test
	public void testSampleStreamOp() throws Exception {
		List <Row> df_data = Arrays.asList(
			Row.of("-0.6264538 0.1836433"),
			Row.of("-0.8356286 1.5952808"),
			Row.of("0.3295078 -0.8204684"),
			Row.of("0.4874291 0.7383247"),
			Row.of("0.5757814 -0.3053884"),
			Row.of("1.5117812 0.3898432"),
			Row.of("-0.6212406 -2.2146999"),
			Row.of("11.1249309 9.9550664"),
			Row.of("9.9838097 10.9438362"),
			Row.of("10.8212212 10.5939013"),
			Row.of("10.9189774 10.7821363"),
			Row.of("10.0745650 8.0106483"),
			Row.of("10.6198257 9.9438713"),
			Row.of("9.8442045 8.5292476"),
			Row.of("9.5218499 10.4179416")
		);
		StreamOperator <?> data = new MemSourceStreamOp(df_data, "features string");
		StreamOperator <?> sampleOp = new SampleStreamOp().setRatio(0.3);
		data.link(sampleOp).print();
		StreamOperator.execute();
	}
}

运行结果

features
10.9189774 10.7821363
10.0745650 8.0106483