Python计算每一行的MSE
我正在尝试计算数据帧中每一行的 MSE,
下面是我的代码 -
import pandas as pd
s={'AValues':[1,1,2,2],'month':[2016,2017,2018,2019],'fvalues':[55,66,77,88],'Fruits':['Apple','Mango','Orange','Banana']}
p=pd.DataFrame(data=s)
mse_df=pd.pivot_table(p,index='Fruits',columns='month',values=['AValues','fvalues'])
mse_df=mse_df.fillna(0)
我想在这里逐行计算苹果、香蕉、芒果和橙子的 mse。 (AValues-实际值,fvalues-预测值)
我正在尝试下面的代码-
from sklearn.metrics import mean_squared_error
mean_squared_error(mse_df['AValues'],mse_df['fvalues'])
但是,它给了我总的mse,而不是单独的或逐行的。
你能帮我看看如何找到mse吗?
I am trying to compute the MSE for every row in my dataframe,
Below is my code-
import pandas as pd
s={'AValues':[1,1,2,2],'month':[2016,2017,2018,2019],'fvalues':[55,66,77,88],'Fruits':['Apple','Mango','Orange','Banana']}
p=pd.DataFrame(data=s)
mse_df=pd.pivot_table(p,index='Fruits',columns='month',values=['AValues','fvalues'])
mse_df=mse_df.fillna(0)
This is how the dataframe output of above code looks like-
I wanted to calculate here mse for Apples, Banana, Mango and Orange i.e row by row.
(AValues- actual values , fvalues- forecasted values)
I am trying the below code-
from sklearn.metrics import mean_squared_error
mean_squared_error(mse_df['AValues'],mse_df['fvalues'])
But, it's giving me the total mse and not individual or row by row.
Can you please help me on how the mse can be found out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很惊讶您导入的这个
mean_square_error
函数没有轴 kwarg。相反,请使用numpy
,其中
ax
是您想要计算平均值的轴。我对 pandas 不太熟悉,但如果它们与 numpy 数组/矩阵/类似的形状相同,它可能是axis=1
,尽管它可能是axis=0< /代码>
I'm surprised this
mean_square_error
function you import doesn't have an axis kwarg. Instead, usenumpy
where
ax
is the axis you want the mean to be calculated over. I'm not so familiar with pandas, but if they are the same shape as numpy arrays / matrices / similar, it will probably beaxis=1
, although it could beaxis=0