All Products
Search
Document Center

Platform For AI:Stratified sampling

Last Updated:Apr 01, 2026

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

ParameterRequiredDescription
Stratification ColumnYesThe column used to group records into strata. Each unique value defines one stratum.

Parameters Setting

ParameterRequiredDescription
Sample SizeConditionalThe 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 FractionConditionalThe 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 SeedNoControls 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

ParameterRequiredDescription
CoresNoNumber of CPU cores for computation. Must be a positive integer. Defaults to a system-determined value.
Memory Size per CoreNoMemory 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 500 to 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.2 to 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:

ParameterRequiredDefaultDescription
inputTableNameYesName of the input table.
inputTablePartitionsNoAll partitionsPartitions 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.
outputTableNameYesName of the output table.
strataColNameYesColumn used as the stratification key.
sampleSizeNoNumber 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.
sampleRatioNoSampling 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.
randomSeedNo123456Random seed. Must be a positive integer. Set a fixed value to make sampling reproducible across runs.
lifecycleNoRetention period of the output table in days. Valid values: 1–3650.
coreNumNoSystem-determinedNumber of CPU cores. Must be a positive integer.
memSizePerCoreNoSystem-determinedMemory per core, in MB. Valid values: 1–65536.
At least one of sampleSize or sampleRatio must be specified. If both are set, sampleSize takes precedence.