Assuming you already have a pandas dataframe and you want to add a new column containing mean squared error for two particular columns in the dataframe, you can use numpy to compute the mean squared error and then add the new column to the dataframe using the following code:
main.py332 chars12 lines
This will output the following dataframe:
main.py204 chars7 lines
In the code above, we imported pandas and numpy libraries. Then, we created a sample dataframe with two columns 'predictions' and 'targets'. Next, we computed the mean squared error using numpy and assigned the value to a new column 'mse' in the existing dataframe.
Note: In the code above, we calculated mean squared error for all the values in the 'predictions' and 'targets' column of the data frame and stored the same value to the 'mse' column which we added in the dataframe. For calculating MSE individually for each value, you can iterate over the dataframe as follow:
main.py102 chars3 lines
gistlibby LogSnag