Python Pandas:从数据帧计算 RMSE 的简单示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41328922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:40:38  来源:igfitidea点击:

Python Pandas: Simple example of calculating RMSE from data frame

pythonpandas

提问by zork

Need a simple example of calculating RMSE with Pandas DataFrame. Providing there is function that returns in cycle true and predicted value:

需要一个使用 Pandas DataFrame 计算 RMSE 的简单示例。提供在循环中返回真值和预测值的函数:

def fun (data):
   ...
   return trueVal, predVal

for data in set:
   fun(data)

And then some code puts these results in the following data frame where xis a real value and pis a predicted value:

然后一些代码将这些结果放在以下数据框中,其中x是真实值和p预测值:

In [20]: d
Out[20]: {'p': [1, 10, 4, 5, 5], 'x': [1, 2, 3, 4, 5]}

In [21]: df = pd.DataFrame(d)

In [22]: df
Out[22]: 
    p  x
0   1  1
1  10  2
2   4  3
3   5  4
4   5  5

Questions:

问题:

1) How to put results from funfunction in dfdata frame?

1)如何将fun函数的结果放入df数据框中?

2) How to calculate RMSE using dfdata frame?

2)如何使用df数据框计算RMSE ?

回答by piRSquared

Question 1
This depends on the format that data is in. And I'd expect you already have your true values, so this function is just a pass through.

问题 1
这取决于数据的格式。我希望您已经有了真正的值,所以这个函数只是一个传递。

Question 2

问题2

With pandas
((df.p - df.x) ** 2).mean() ** .5

pandas
((df.p - df.x) ** 2).mean() ** .5

With numpy
(np.diff(df.values) ** 2).mean() ** .5

numpy
(np.diff(df.values) ** 2).mean() ** .5