I have dataframe of y_true values and y_predicted values.
I would need to sequently calculate RMSE for each row, each time using the next row. Could describe it something like row+1. After that, store the result in new column RMSE.
Here is the dataframe. The code would take first row of y_true = 105, y_pred = 195 and calculate RMSE (I use from sklearn.metrics import mean_squared_error) which would be 90.0 and put it in new column.
After that we take the first row and the next one which would be y_true = [105, 297], y_pred = [195,337] and calculate the error again, the result would be 69.6. And do this for the whole dataframe.
Next time y_true = [105, 297, 262], y_pred = [195,337, 804] and so on. I assume that this could be done with a loop.
ID y_true y_pred
0 1 105 195
1 2 297 337
2 3 262 804
3 4 206 708
4 5 226 705
5 6 309 463
6 7 210 676
7 8 124 573
The result should be:
ID y_true y_pred RMSE
0 1 105 195 90.00
1 2 297 337 69.60
2 3 262 804 318.04
3 4 206 708 372.64
4 5 226 705 396.20
5 6 309 463 367.11
6 7 210 676 382.00
7 8 124 573 391.00
Currently the code calculates the RMSE
import pandas as pd
from sklearn.metrics import mean_squared_error
import numpy as np
dataframe = pd.read_excel(r'S:\...\df.xlsx')
y_true = dataframe['y_true']
y_pred = dataframe['y_pred']
rmse = mean_squared_error(y_true, y_pred, squared=False)
print(rmse)
But the question is how to calculate it for rows, each time taking the next one?