Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to log model predictions to a file.
MLOps
with open('audit_log.txt', '[1]') as f: f.write(f"Prediction: {prediction}\n")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which overwrites the file, losing previous logs.
✗ Incorrect
Use 'a' to append logs without overwriting existing data.
2fill in blank
mediumComplete the code to include a timestamp in the audit log entry.
MLOps
import datetime log_entry = f"{datetime.datetime.now()}: Prediction = {prediction}" with open('audit_log.txt', 'a') as f: f.write([1] + '\n')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling log_entry() which causes an error because it's not a function.
✗ Incorrect
log_entry is already a string, so write it directly.
3fill in blank
hardFix the error in the code that logs model input and output as JSON.
MLOps
import json log_data = {'input': model_input, 'output': model_output} with open('audit_log.json', 'a') as f: f.write(json.dumps([1]) + '\n')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing log_data() which causes a TypeError.
✗ Incorrect
json.dumps expects a dictionary, so pass log_data directly.
4fill in blank
hardFill both blanks to create a dictionary comprehension that logs only features with values above 0.5.
MLOps
filtered_log = {key: value for key, value in model_features.items() if value [1] 0.5 and key [2] 'confidence'} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for value comparison.
Using '==' instead of '!=' for key filtering.
✗ Incorrect
We want values greater than 0.5 and keys not equal to 'confidence'.
5fill in blank
hardFill all three blanks to create a filtered audit log dictionary with uppercase keys, values above 0.7, and keys not equal to 'bias'.
MLOps
filtered_audit = [1]: [2] for [3], val in audit_data.items() if val > 0.7 and [3] != 'bias'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'val' for values.
Using 'value' or 'val' as loop variable instead of 'key'.
✗ Incorrect
Use key.upper() for keys, val for values, and key as the loop variable.