0
0
Prompt Engineering / GenAIml~15 mins

Output format control in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Output format control
Problem:You have a machine learning model that predicts house prices. The model outputs raw predictions as floating-point numbers with many decimal places, which is hard to read and use in reports.
Current Metrics:Model predictions example: [234567.891234, 345678.912345, 456789.123456]
Issue:The output format is not user-friendly. Predictions have too many decimals and inconsistent formatting, making it difficult to interpret and present.
Your Task
Format the model's output predictions to show as currency with exactly two decimal places and commas as thousand separators.
Do not change the model or its predictions.
Only modify the output formatting code.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import numpy as np

# Simulated model predictions
predictions = np.array([234567.891234, 345678.912345, 456789.123456])

# Format predictions as currency strings with commas and 2 decimals
def format_predictions(preds):
    return [f"${pred:,.2f}" for pred in preds]

formatted_preds = format_predictions(predictions)
print(formatted_preds)
Added a function to format the raw float predictions into strings.
Used f-string formatting with :, to add commas and .2f to limit decimals to two.
Prefixed with $ to represent currency.
Results Interpretation

Before formatting: [234567.891234, 345678.912345, 456789.123456]

After formatting: ['$234,567.89', '$345,678.91', '$456,789.12']

Proper output formatting improves readability and usability of model predictions without changing the model itself.
Bonus Experiment
Try formatting the output predictions in European style with periods as thousand separators and commas as decimal separators, e.g., '234.567,89 €'.
💡 Hint
Use Python's locale module or custom string replacement to achieve this format.