PyODPS feature scaling

Updated at:

This topic describes how to perform feature scaling with PyODPS, a critical data preprocessing step for many machine learning algorithms. Learn how to apply normalization and standardization to your MaxCompute data.

Prerequisites

Complete the following steps:

Procedure

  1. Download the test dataset and import it into MaxCompute.

    1. Download and decompress the Iris dataset. Rename the iris.data file to iris.csv.

    2. Create a table named pyodps_iris and upload the iris.csv dataset. For more information, see Create a table and upload data.

      Create the table by using the following statement.

      CREATE TABLE if not exists pyodps_iris
      (
      sepallength  DOUBLE comment 'Sepal length (cm)',
      sepalwidth   DOUBLE comment 'Sepal width (cm)',
      petallength  DOUBLE comment 'Petal length (cm)',
      petalwidth   DOUBLE comment 'Petal width (cm)',
      name         STRING comment 'Species'
      );
  2. Log in to the DataWorks console and select a region in the upper-left corner.

  3. In the left-side navigation pane, click Workspaces.

  4. On the Data Development page, right-click the created workflow and choose Create Node > MaxCompute > PyODPS 2.

  5. In the Create Node dialog box, enter a node name and click OK.

  6. In the PyODPS 2 node, enter the feature scaling code.

    Example code:

    # Load the table into a DataFrame.
    df = DataFrame(o.get_table('pyodps_iris'))
    
    # Rename the 'petallength' column to 'fid' to align with the example's feature column.
    df = df.rename(columns={'petallength': 'fid'})
    
    # Use the min_max_scale method for normalization to the default range of [0, 1].
    print df.min_max_scale(columns=['fid']).head()
    
    # Use the feature_range parameter to specify a custom output range, such as [-1, 1].
    print df.min_max_scale(columns=['fid'],feature_range=(-1,1)).head()
    
    # Use the preserve parameter to retain the original column.
    # The scaled data is added as a new column with the `_scaled` suffix.
    print df.min_max_scale(columns=['fid'],preserve=True).head()
    
    # Use the group parameter to perform scaling separately within each group.
    print df.min_max_scale(columns=['fid'],group=['name']).head()
    
    # Use the std_scale method for standardization (zero mean and unit variance).
    print df.std_scale(columns=['fid']).head()
  7. Click Run.

    df2 = DataFrame(o.get_table('pytable2'))
    xxx
    print df2.dropna(subset=['f1','f2','f3','f4']).head()
    xxx
    xxx
    xxx
    print df2.dropna(how='all', subset=['f1','f2','f3','f4']).head()
    
    print df2.dropna(thresh=3, subset=['f1', 'f2', 'f3', 'f4']).head()
    
    xxx
    print df2.fillna(100, subset=['f1','f2','f3','f4']).head()
    
    xxx
    print df2.fillna(df2.f2, subset=['f1','f2','f3','f4']).head()
    
    xxx
    xxx
    print df2.fillna(method='bfill', subset=['f1', 'f2', 'f3', 'f4']).head()
    
    print df2.fillna(method='ffill', subset=['f1', 'f2', 'f3', 'f4']).head()
  8. On the Run Log tab, view the results.

    Sql compiled:
    CREATE TABLE tmp_pyodps_59cb5533_6dc3_4b64_9312_b2da29d70cee LIFECYCLE 1 AS
    SELECT /*+mapjoin(t5)*/ t2.`name`, t2.`id`, (IF((t5.`fid_max_1570691133` - t5.`fid_min_1570691133`) == 0, 0, (t2.`fid` - t5.`fid_min_1570691133`) / (t5.`fid_max_1570691133` - t5.`fid_min_1570691133`)) * 1) + 0 AS `fid`
    FROM (
      SELECT t1.`name`, t1.`id`, t1.`fid`, 1 AS `idx_col_1570691133`
      FROM WB_BestPractice_dev.`pytable` t1
    ) t2
    INNER JOIN
      (
        SELECT 1 AS `idx_col_1570691133`, MIN(t4.`fid`) AS `fid_min_1570691133`, MAX(t4.`fid`) AS `fid_max_1570691133`
        FROM (
          SELECT t3.`fid`
          FROM WB_BestPractice_dev.`pytable` t3
        ) t4
      ) t5
    ON t2.`idx_col_1570691133` == t5.`idx_col_1570691133`
    Instance ID: 2019101007053495gl8by72m
    
        name  id       fid
    0  name1   4  1.000000
    1  name2   2  0.526316
    2  name2   3  0.000000
    3  name1   4  0.710526
    4  name1   3  0.184211
    5  name1   3  0.684211
    Sql compiled:
    CREATE TABLE tmp_pyodps_88a24967_3bdc_41ce_85d7_4bbd23e4ce01 LIFECYCLE 1 AS
    SELECT /*+mapjoin(t5)*/ t2.`name`, t2.`id`, (IF((t5.`fid_max_1570691139` - t5.`fid_min_1570691139`) == 0, 0, (t2.`fid` - t5.`fid_min_1570691139`) / (t5.`fid_max_1570691139` - t5.`fid_min_1570691139`)) * 2) + -1 AS `fid`
    FROM (
      SELECT t1.`name`, t1.`id`, t1.`fid`, 1 AS `idx_col_1570691139`
      FROM WB_BestPractice_dev.`pytable` t1
    ) t2
    INNER JOIN
      (
        SELECT 1 AS `idx_col_1570691139`, MIN(t4.`fid`) AS `fid_min_1570691139`, MAX(t4.`fid`) AS `fid_max_1570691139`
        FROM (
          SELECT t3.`fid`
          FROM WB_BestPractice_dev.`pytable` t3
        ) t4
      ) t5
    ON t2.`idx_col_1570691139` == t5.`idx_col_1570691139`
    Instance ID: 20191010070539772gjo56292
    
        name  id       fid
    0  name1   4  1.000000
    1  name2   2  0.052632
    2  name2   3 -1.000000
    3  name1   4  0.421053
    4  name1   3 -0.631579
    5  name1   3  0.368421
    Sql compiled:
    CREATE TABLE tmp_pyodps_439117fc_9ef7_4086_899d_a5bf77d653e5 LIFECYCLE 1 AS
    SELECT /*+mapjoin(t5)*/ t2.`name`, t2.`id`, t2.`fid`, (IF((t5.`fid_max_1570691146` - t5.`fid_min_1570691146`) == 0, 0, (t2.`fid` - t5.`fid_min_1570691146`) / (t5.`fid_max_1570691146` - t5.`fid_min_1570691146`)) * 1) + 0 AS `fid_scaled`
    FROM (
      SELECT t1.`name`, t1.`id`, t1.`fid`, 1 AS `idx_col_1570691146`
      FROM WB_BestPractice_dev.`pytable` t1
    ) t2
    INNER JOIN
      (
        SELECT 1 AS `idx_col_1570691146`, MIN(t4.`fid`) AS `fid_min_1570691146`, MAX(t4.`fid`) AS `fid_max_1570691146`
        FROM (
          SELECT t3.`fid`
          FROM WB_BestPractice_dev.`pytable` t3
        ) t4
      ) t5
    ON t2.`idx_col_1570691146` == t5.`idx_col_1570691146`
    
    Instance ID: 20191010070546769g0c14f72
    
        name  id  fid  fid_scaled
    0  name1   4  5.3    1.000000
    1  name2   2  3.5    0.526316
    2  name2   3  1.5    0.000000
    3  name1   4  4.2    0.710526
    4  name1   3  2.2    0.184211
    5  name1   3  4.1    0.684211
    Sql compiled:
    CREATE TABLE tmp_pyodps_d3839b4b_1087_4d52_91f5_52763b72f272 LIFECYCLE 1 AS
    SELECT /*+mapjoin(t3)*/ t1.`name`, t1.`id`, (IF((t3.`fid_max_1570691151` - t3.`fid_min_1570691151`) == 0, 0, (t1.`fid` - t3.`fid_min_1570691151`) / (t3.`fid_max_1570691151` - t3.`fid_min_1570691151`)) * 1) + 0 AS `fid`
    FROM WB_BestPractice_dev.`pytable` t1
    INNER JOIN
      (
        SELECT t2.`name`, MAX(t2.`fid`) AS `fid_max_1570691151`, MIN(t2.`fid`) AS `fid_min_1570691151`
        FROM WB_BestPractice_dev.`pytable` t2
        GROUP BY t2.`name`
      ) t3
    ON t1.`name` == t3.`name`
    Instance ID: 20191010070551756gtf14f72
    
    
        name  id       fid
    0  name1   4  1.000000
    1  name2   2  1.000000
    2  name2   3  0.000000
    3  name1   4  0.645161
    4  name1   3  0.000000
    5  name1   3  0.612903
    Sql compiled:
    CREATE TABLE tmp_pyodps_1ea6e5b4_129f_4d1e_a6a7_410e08d77ae6 LIFECYCLE 1 AS
    SELECT /*+mapjoin(t5)*/ t2.`name`, t2.`id`, IF(t5.`fid_std_1570691157` == 0, 0, (t2.`fid` - t5.`fid_mean_1570691157`) / t5.`fid_std_1570691157`) AS `fid`
    FROM (
      SELECT t1.`name`, t1.`id`, t1.`fid`, 1 AS `idx_col_1570691157`
      FROM WB_BestPractice_dev.`pytable` t1
    ) t2
    INNER JOIN
      (
        SELECT 1 AS `idx_col_1570691157`, AVG(t4.`fid`) AS `fid_mean_1570691157`, STDDEV(t4.`fid`) AS `fid_std_1570691157`
        FROM (
          SELECT t3.`fid`
          FROM WB_BestPractice_dev.`pytable` t3
        ) t4
      ) t5
    ON t2.`idx_col_1570691157` == t5.`idx_col_1570691157`
    Instance ID: 20191010070557788gdl14f72
    
        name  id       fid
    0  name1   4  1.436467
    1  name2   2  0.026118
    2  name2   3 -1.540938
    3  name1   4  0.574587
    4  name1   3 -0.992468
    5  name1   3  0.496234