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:
-
Activate MaxCompute.
-
Activate DataWorks.
-
Create a workflow in DataWorks. This example uses a workspace in basic mode.
Procedure
-
Download the test dataset and import it into MaxCompute.
-
Download and decompress the Iris dataset. Rename the iris.data file to iris.csv.
-
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' );
-
Log in to the DataWorks console and select a region in the upper-left corner.
In the left-side navigation pane, click Workspaces.
-
On the Data Development page, right-click the created workflow and choose .
-
In the Create Node dialog box, enter a node name and click OK.
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()Click Run.

On the Run Log tab, view the results.
