title | titleSuffix | description | services | ms.service | ms.subservice | author | ms.author | ms.date | ms.topic | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Execute Python Script in the designer |
Azure Machine Learning |
Learn how to use the Execute Python Script model in Azure Machine Learning designer to run custom operations written in Python. |
machine-learning |
machine-learning |
mldata |
likebupt |
keli19 |
10/21/2021 |
how-to |
designer, devx-track-python |
In this article, you learn how to use the Execute Python Script component to add custom logic to Azure Machine Learning designer. In the following how-to, you use the Pandas library to do simple feature engineering.
You can use the in-built code editor to quickly add simple Python logic. If you want to add more complex code or upload additional Python libraries, you should use the zip file method.
The default execution environment uses the Anacondas distribution of Python. For a complete list of pre-installed packages, see the Execute Python Script component reference page.
[!INCLUDE machine-learning-missing-ui]
-
Find the Execute Python Script component in the designer palette. It can be found in the Python Language section.
-
Drag and drop the component onto the pipeline canvas.
This article uses the sample dataset, Automobile price data (Raw).
-
Drag and drop your dataset to the pipeline canvas.
-
Connect the output port of the dataset to the top-left input port of the Execute Python Script component. The designer exposes the input as a parameter to the entry point script.
The right input port is reserved for zipped Python libraries.
-
Take note of which input port you use. The designer assigns the left input port to the variable
dataset1
and the middle input port todataset2
.
Input components are optional since you can generate or import data directly in the Execute Python Script component.
The designer provides an initial entry point script for you to edit and enter your own Python code.
In this example, you use Pandas to combine two columns found in the automobile dataset, Price and Horsepower, to create a new column, Dollars per horsepower. This column represents how much you pay for each horsepower, which could be a useful feature to decide if a car is a good deal for the money.
-
Select the Execute Python Script component.
-
In the pane that appears to the right of the canvas, select the Python script text box.
-
Copy and paste the following code into the text box.
import pandas as pd def azureml_main(dataframe1 = None, dataframe2 = None): dataframe1['Dollar/HP'] = dataframe1.price / dataframe1.horsepower return dataframe1
Your pipeline should look the following image:
The entry point script must contain the function
azureml_main
. There are two function parameters that map to the two input ports for the Execute Python Script component.The return value must be a Pandas Dataframe. You can return up to two dataframes as component outputs.
-
Submit the pipeline.
Now, you have a dataset with the new feature Dollars/HP, which could be useful in training a car recommender. This is an example of feature extraction and dimensionality reduction.
Learn how to import your own data in Azure Machine Learning designer.