PyODPS feature scaling

更新时间:
复制 MD 格式

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

Before you start, 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.

      Use the following statement to create the table.

      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.运行节点.png

  8. On the Run Log tab, view the results.