|
| 1 | +--- |
| 2 | +title: Python |
| 3 | +titleSuffix: Azure Machine Learning |
| 4 | +description: Learn how to use Python in Azure Machine Learning designer to transform data. |
| 5 | +services: machine-learning |
| 6 | +ms.service: machine-learning |
| 7 | +ms.subservice: core |
| 8 | +ms.topic: how-to |
| 9 | + |
| 10 | +author: peterclu |
| 11 | +ms.author: peterlu |
| 12 | +ms.date: 02/28/2020 |
| 13 | +--- |
| 14 | + |
| 15 | +# Execute Python code in Azure Machine Learning designer |
| 16 | + |
| 17 | +In this article, you learn how to use the [Execute Python Script](algorithm-module-reference/execute-python-script.md) module to add custom logic to Azure Machine Learning designer. In the following how-to, you use the Pandas library to do simple feature engineering. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +The default execution environment uses the Anacondas distribution of Python. For a complete list of pre-installed packages, see the [Execute Python Script module reference](algorithm-module-reference/execute-python-script.md) page. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Execute Python written in the designer |
| 26 | + |
| 27 | +### Add the Execute Python Script module |
| 28 | + |
| 29 | +1. Find the **Execute Python Script** module in the designer palette. It can be found in the **Python Language** section. |
| 30 | + |
| 31 | +1. Drag and drop the module onto the pipeline canvas. |
| 32 | + |
| 33 | +### Connect input datasets |
| 34 | + |
| 35 | +This article uses the sample dataset, **Automobile price data (Raw)**. |
| 36 | + |
| 37 | +1. Drag and drop your dataset to the pipeline canvas. |
| 38 | + |
| 39 | +1. Connect the output port of the dataset to the top-left input port of the **Execute Python Script** module. The designer exposes the input as a parameter to the entry point script. |
| 40 | + |
| 41 | + The right input port is reserved for zipped python libraries. |
| 42 | + |
| 43 | +  |
| 44 | + |
| 45 | + |
| 46 | +1. Take note of which input port you use. The designer assigns the left input port to the variable `dataset1` and the middle input port to `dataset2`. |
| 47 | + |
| 48 | +Input modules are optional since you can generate or import data directly in the **Execute Python Script** module. |
| 49 | + |
| 50 | +### Write your Python code |
| 51 | + |
| 52 | +The designer provides an initial entry point script for you to edit and enter your own Python code. |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +1. Select the **Execute Python Script** module. |
| 57 | + |
| 58 | +1. In the pane that appears to the right of the canvas, select the **Python script** text box. |
| 59 | + |
| 60 | +1. Copy and paste the following code into the text box. |
| 61 | + |
| 62 | + ```python |
| 63 | + import pandas as pd |
| 64 | + |
| 65 | + def azureml_main(dataframe1 = None, dataframe2 = None): |
| 66 | + dataframe1['Dollar/HP'] = dataframe1.price / dataframe1.horsepower |
| 67 | + return dataframe1 |
| 68 | + ``` |
| 69 | + Your pipeline should look the following image: |
| 70 | + |
| 71 | +  |
| 72 | + |
| 73 | + 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** module. |
| 74 | + |
| 75 | + The return value must be a Pandas Dataframe. You can return up to two dataframes as module outputs. |
| 76 | + |
| 77 | +1. Run the pipeline. |
| 78 | + |
| 79 | +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. |
| 80 | + |
| 81 | +## Next steps |
| 82 | + |
| 83 | +Learn how to [import your own data](how-to-designer-import-data.md) in Azure Machine Learning designer. |
0 commit comments