**Some Python magic: Measures of Fit in action**

I hear you think: *“Micha, this is interesting, but how can I use this in Python?”*. Let me show you!

Let’s start with stating some true values and some predictions. I will be using some easy to see and calculate deviations. This is a nice way to get a good feeling for the discussed measures of fit, so next time you work with a model, you can use the measures yourself in a larger scenario.

`y_true = [-10, -5, 0, 5, 10, 15]`

y_always_five = [-15, -10, -5, 0, 5, 10]

y_small_large = [-7, -2, 3, 12, 17, 22]

y_outlier = [-10, -5, 0, 5, 10, 45]

Now that we have the y_true and the predictions, we can calculate if our prediction is any good. We’ll go over them one by one. The python code will only show examples for y_always_five, Table 1 will show the the measures for other predictions as well.

print("MAE y_always_five: ", mean_absolute_error(y_true, y_always_five))MAE y_always five: 5.0print("MSE y_always_five: ", mean_squared_error(y_true, y_always_five))MSE y_always_five: 25.0print("RMSE y_always_five: ", sqrt(mean_squared_error(y_true, y_always_five)))RMSE y_always_five: 5.0print("R2 y_always_five: ", r2_score(y_true, y_always_five))R2 y_always_five: 0.6571428571428571

A bunch of different numbers. Time to interpret them and get our best fit. The table below displays the different predictions and their corresponding measures (Table 1). But first some code to plot these predictions. The result is shown in Figure 1.