Don't Repeat Yourself. In programming, if you use the same block of code multiple times, it belongs in a function.
A Function is a reusable block of code that performs a specific task. In AI, we use functions for everything from calculating accuracy to processing images.
You create a function using the def keyword, followed by the function name and parentheses. Inside the parentheses, you define parameters (inputs). The return statement sends a result back.
Imagine calculating the average error for multiple different models. Instead of writing the math every time, we write an error() function once.
Let's write a reusable utility function to calculate classification accuracy.
calculate_accuracy that takes two lists as parameters: predictions and actuals.range(len(predictions)) in a loop).return the calculated accuracy.preds = [1,0,1,1] and acts = [1,0,0,1] and print the result.