Code recipes
Code recipes for Notebook users allows adding functional code logic within a recipe in the machine learning flow without having to create a separate custom template. This simplifies the ability to create new custom templates within a recipe. With the AI capabilities integrated into the platform, users can invoke AI and retrieve the code based on the query passed. The generated code is used to create custom code recipes on the Notebook in different stages of model building. Users can set the ask.ai flag in the code to True to use the AI capabilities for code generation and set the same parameter to False to manually write the code for creating custom code recipes.
Fetching code from the AI
Use this code block to fetch the code logic you want to use in the flow from the AI based on the query passed as an input.
Firstly, you have to create a recipe for a dataset inside the project.
recipe = project.addRecipe([titanic], name="code_recipe")
Secondly, use the following code block to send your query to the AI and get the code.
code_response = recipe.generate_code(
user_input="Show dataframe having people against Fare. Plot histogram of number of people against Fare. Plot plot of survived by gender",
ask_ai=True,
with_signature=True,
outputs=["output1"],
charts=["chart1", "chart2"]
)
print(code_response.get_body())
The following table describes about each parameter in the generate_code
block:
- Parameter:
Description
- user_input:
The input that is given to the AI to generate the code.
- ask_ai:
Set this flag either to False or True. Setting this to True will allow you to use the AI capabilities and fetch the code from the AI for the query passed and changing this to False will allow you to manually write the code within the recipe.
- with_signature:
By default, it is set to True.
- outputs:
The output generated by the template.
Output from the AI
The following is the code block generated by the AI for the given user input.
def transform(entities):
input_df_1 = entities['titanic']
import pandas as pd
import matplotlib.pyplot as plt
output_df_1 = input_df_1[['PassengerId', 'Fare']]
plt.hist(input_df_1['Fare'], bins=20)
plt.xlabel('Fare')
plt.ylabel('Number of People')
plt.title('Histogram of Number of People against Fare')
fig_1 = plt.figure(1)
plt.show()
survived_by_gender = input_df_1.groupby(['Sex', 'Survived'])['Survived'].count(
).unstack()
survived_by_gender.plot(kind='bar', stacked=True)
plt.xlabel('Gender')
plt.ylabel('Number of People')
plt.title('Survived by Gender')
fig_2 = plt.figure(2)
plt.show()
return {
'output1': output_df_1,
'chart1': fig_1,
'chart2': fig_2,
}
Creating a code recipe manually
Use this procedure to write the code within a recipe for executing logic in the flow manually. In the generate_code
block, you have to set the ask.ai
flag to False to manually the code within a recipe.
code_response = recipe.generate_code(
user_input="Create train and test split based on Survived column. Do not do any group by and maintain all columns",
ask_ai=False,
with_signature=True,
outputs=["output_1", "output_2"]
)
code_response.update("""def transform(entities):
input_df_1 = entities['titanic']
import pandas as pd
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(input_df_1, test_size=0.2, stratify=
input_df_1['Survived'], random_state=42)
output_df_1 = train_df[['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex',
'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']]
output_df_2 = test_df[['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex',
'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']]
return {
'output_1': output_df_1,
'output_2': output_df_2,
}""")
See also
For more information to use code recipes in your projects, refer the following sample projects: