Python计算每一行的MSE

发布于 2025-01-12 13:55:25 字数 764 浏览 0 评论 0原文

我正在尝试计算数据帧中每一行的 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-
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-19 13:55:25

我很惊讶您导入的这个 mean_square_error 函数没有轴 kwarg。相反,请使用 numpy

import numpy as np
MSE = np.mean((mse_df['AValues'] - mse_df['fvalues'])**2, axis=ax)

,其中 ax 是您想要计算平均值的轴。我对 pandas 不太熟悉,但如果它们与 numpy 数组/矩阵/类似的形状相同,它可能是 axis=1,尽管它可能是 axis=0< /代码>

I'm surprised this mean_square_error function you import doesn't have an axis kwarg. Instead, use numpy

import numpy as np
MSE = np.mean((mse_df['AValues'] - mse_df['fvalues'])**2, axis=ax)

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 be axis=1, although it could be axis=0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文