Evaluating Model Performance 1. Testing quality of predictions

I want to understand better what is done in this exercise. There is an apply function inside the predict_price and then to get the final answer another apply function is passed. How does the apply function work basically, in python? And how it helps get the predicted price in this example?

Thank You

The apply function, simply applies a given function along the elements of an axis. In this case a column, since the default axis value is 0.

This is probably best illustrated with an example.

I’ll run the following code to generate an example dataframe:

import pandas as pd

df = pd.DataFrame({
    "A": [10, 20, 30, 40],
    "B": [6, 7, 8, 9]



    A  B
0  10  6
1  20  7
2  30  8
3  40  9

Let’s say I wanted to generate a new column, C, that multiplied all the values in the B column by 10. If I wanted to do this using the apply function, I would run:

df['C'] = df['B'].apply(lambda x: x*10)



    A  B   C
0  10  6  60
1  20  7  70
2  30  8  80
3  40  9  90

I applied the function specified by the lambda to all the elements of column B, and used that to create column C.

In this example, obviously there’s a much simpler way to generate the C column that doesn’t involve using the apply function or lambda functions, but apply is very helpful when you need to execute a more complex function on each element of a particular column (or row).

Thanks a lot for explaining.