1.1 - 8.10 - RandomSample Example: SamplingMode ('basic'), Weighted - Teradata Vantage

Teradata Vantage™ - Machine Learning Engine Analytic Function Reference

Product
Teradata Vantage
Release Number
1.1
8.10
Release Date
October 2019
Content Type
Programming Reference
Publication ID
B700-4003-079K
Language
English (United States)

This example uses basic sampling to select two samples of five rows each, weighted by car weight. Because the function call includes the Seed and SeedColumn syntax elements, it always produces the same output from the same input.

Input

The input table, fs_input, has 32 observations of 11 variables for different models of cars. The variables vs and am are categorical; the others are numerical.

Variable Description
mpg Miles per U. S. gallon
cyl Number of cylinders
disp Displacement (cubic inches)
hp Gross horsepower
drat Drive ratio
wt Weight (lbs/1000)
qsec Quarter-mile time (seconds)
vs Engine configuraton (V or S (straight))
am Transmission type (automatic or manual)
gear Number of forward gears
carb Number of carburetors
fs_input
sn model mpg cyl disp hp drat wt qsec vs am gear carb
1 Mazda RX4 21 6 160 110 3.9 2.62 16.46 S manual 4 4
2 Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 S manual 4 4
3 Datsun 710 22.8 4 108 93 3.85 2.32 18.61 V manual 4 1
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 V automatic 3 1
5 Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 S automatic 3 2
6 Valiant 18.1 6 225 105 2.76 3.46 20.22 V automatic 3 1
7 Duster 360 14.3 8 360 245 3.21 3.57 15.84 S automatic 3 4
8 Merc 240D 24.4 4 146.7 62 3.69 3.19 20 V automatic 4 2
9 Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 V automatic 4 2
10 Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 V automatic 4 4
11 Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 V automatic 4 4
12 Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 S automatic 3 3
13 Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 S automatic 3 3
14 Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 S automatic 3 3
15 Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 S automatic 3 4
16 Lincoln Continental 10.4 8 460 215 3 5.424 17.82 S automatic 3 4
17 Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 S automatic 3 4
18 Fiat 128 32.4 4 78.7 66 4.08 2.2 19.47 V manual 4 1
19 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 V manual 4 2
20 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.9 V manual 4 1
21 Toyota Corona 21.5 4 120.1 97 3.7 2.465 20.01 V automatic 3 1
22 Dodge Challenger 15.5 8 318 150 2.76 3.52 16.87 S automatic 3 2
23 AMC Javelin 15.2 8 304 150 3.15 3.435 17.3 S automatic 3 2
24 Camaro Z28 13.3 8 350 245 3.73 3.84 15.41 S automatic 3 4
25 Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 S automatic 3 2
26 Fiat X1-9 27.3 4 79 66 4.08 1.935 18.9 V manual 4 1
27 Porsche 914-2 26 4 120.3 91 4.43 2.14 16.7 S manual 5 2
28 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 V manual 5 2
29 Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 S manual 5 4
30 Ferrari Dino 19.7 6 145 175 3.62 2.77 15.5 S manual 5 6
31 Maserati Bora 15 8 301 335 3.54 3.57 14.6 S manual 5 8
32 Volvo 142E 21.4 4 121 109 4.11 2.78 18.6 V manual 4 2

SQL Call

SELECT * FROM RandomSample(
  ON fs_input AS InputTable
  USING
  SamplingMode ('basic')
  NumSample(5,5)
  WeightColumn('wt')
  Seed(1)
  SeedColumn('model')
) AS dt ORDER BY 1,2,3;

Output

 set_id sn model               mpg  cyl disp  hp    drat wt    qsec  vs am        gear carb 
 ------ -- ------------------- ---- --- ----- ----- ---- ----- ----- -- --------- ---- ---- 
      0  1 mazda rx4           21.0   6 160.0 110.0  3.9  2.62 16.46 s  manual       4    4
      0 13 merc 450sl          17.3   8 275.8 180.0 3.07  3.73  17.6 s  automatic    3    3
      0 14 merc 450slc         15.2   8 275.8 180.0 3.07  3.78  18.0 s  automatic    3    3
      0 16 lincoln continental 10.4   8 460.0 215.0  3.0 5.424 17.82 s  automatic    3    4
      0 28 lotus europa        30.4   4  95.1 113.0 3.77 1.513  16.9 v  manual       5    2
      1 11 merc 280c           17.8   6 167.6 123.0 3.92  3.44  18.9 v  automatic    4    4
      1 15 cadillac fleetwood  10.4   8 472.0 205.0 2.93  5.25 17.98 s  automatic    3    4
      1 17 chrysler imperial   14.7   8 440.0 230.0 3.23 5.345 17.42 s  automatic    3    4
      1 25 pontiac firebird    19.2   8 400.0 175.0 3.08 3.845 17.05 s  automatic    3    2
      1 32 volvo 142e          21.4   4 121.0 109.0 4.11  2.78  18.6 v  manual       4    2

Download a zip file of all examples and a SQL script file that creates their input tables from the attachment in the left sidebar.