When your training data has uneven class distributions, plain random sampling underrepresents minority classes—leading to biased models. Stratified Sampling splits a dataset into groups based on a stratification column, then draws random samples from each group independently. Use this component to improve model accuracy and training stability on imbalanced datasets.
Configure the component
Method 1: Configure on the pipeline page
Add a Stratified Sampling component to your pipeline, then set the following parameters.
Fields Setting
| Parameter | Required | Description |
|---|---|---|
| Stratification Column | Yes | The column used to group records into strata. Each unique value defines one stratum. |
Parameters Setting
| Parameter | Required | Description |
|---|---|---|
| Sample Size | Conditional | The number of records to draw from each stratum. Must be a positive integer. At least one of Sample Size or Sampling Fraction is required. See Sampling parameters. |
| Sampling Fraction | Conditional | The proportion of records to draw from each stratum. Must be a floating-point number in the range (0, 1). For example, 0.2 draws 20% of records from each stratum. See Sampling parameters. |
| Random Seed | No | Controls the randomness of sampling. Default: 1234567. Set a fixed seed to get reproducible results across runs. If you use the default or change the seed between runs, results may differ each time. |
Tuning
| Parameter | Required | Description |
|---|---|---|
| Cores | No | Number of CPU cores for computation. Must be a positive integer. Defaults to a system-determined value. |
| Memory Size per Core | No | Memory allocated to each core, in MB. Must be a positive integer in the range (1, 65536). Defaults to a system-determined value. |
Sampling parameters
Sample Size and Sampling Fraction control how many records are drawn from each stratum:
Sample Size specifies an exact count per stratum. Use this when you need a fixed number of records for downstream processing—for example, set
500to draw exactly 500 records from every stratum.Sampling Fraction specifies a proportion. Use this when you want each stratum to contribute proportionally regardless of its size—for example, set
0.2to draw 20% of records from every stratum.
At least one of the two must be specified. If you leave both blank, an error is returned. If you set both, Sample Size takes precedence.
Method 2: Use PAI commands
Use the SQL Script component to run PAI commands. For setup details, see SQL Script.
The following example samples 200 records from stratum A, 300 from B, and 500 from C:
PAI -name StratifiedSample
-project algo_public
-DinputTableName="test_input"
-DoutputTableName="test_output"
-DstrataColName="label"
-DsampleSize="A:200,B:300,C:500"
-DrandomSeed=1007
-Dlifecycle=30;All parameters for the StratifiedSample command:
| Parameter | Required | Default | Description |
|---|---|---|---|
inputTableName | Yes | — | Name of the input table. |
inputTablePartitions | No | All partitions | Partitions to read from the input table. Supported formats: partition_name=value (single partition) or name1=value1/name2=value2 (multi-level). Separate multiple partitions with commas. Example: name1=value1,value2. |
outputTableName | Yes | — | Name of the output table. |
strataColName | Yes | — | Column used as the stratification key. |
sampleSize | No | — | Number of records to sample per stratum. If set to a positive integer, that count applies to every stratum. If set to a string, use the format stratum0:n0,stratum1:n1 to specify counts per stratum—for example, A:200,B:300,C:500. |
sampleRatio | No | — | Sampling proportion per stratum. If set to a number, must be a floating-point value between 0 and 1—for example, 0.2 draws 20% of each stratum's records. If set to a string, use the format stratum0:r0,stratum1:r1 to specify proportions per stratum. |
randomSeed | No | 123456 | Random seed. Must be a positive integer. Set a fixed value to make sampling reproducible across runs. |
lifecycle | No | — | Retention period of the output table in days. Valid values: 1–3650. |
coreNum | No | System-determined | Number of CPU cores. Must be a positive integer. |
memSizePerCore | No | System-determined | Memory per core, in MB. Valid values: 1–65536. |
At least one ofsampleSizeorsampleRatiomust be specified. If both are set,sampleSizetakes precedence.