简体   繁体   English

如何在需要两个向量的函数中使用应用程序

[英]How do I use apply in pandas with a function that requires two vectors

I want to pass vector columns to apply in a pandas dataframe, here is a simplification:我想传递向量列以应用于熊猫数据帧,这里是一个简化:

def mae(y_true, y_pred):
    return (y_true - y_pred).abs().mean()

df = pd.DataFrame({"y_true": [1.1, 2, 3], "y_pred": [2, 2.5, 3]})
df[df.y_true > 1.5].apply(lambda x: mae(x.y_true, x.y_pred), axis=1)

It gives an error with or without the axis=1 .无论是否带有axis=1它都会给出错误。 I want to avoid doing the long way我想避免走很长的路

df_filtered = df[df.y_true > 1.5]
mae(df_filtered.y_true.values, df_filtered.y_pred.values)

It might be something easy, but I've done some research and don't know how to do it.这可能很容易,但我做了一些研究,但不知道如何去做。 The value returned should be 0.25返回的值应该是 0.25

You want to avoid using apply for these simple calculations since it is just syntactic sugar for a python level for .你要避免使用apply于这些简单的计算,因为它是一个Python的水平只是语法糖for Just index your dataframe where df.y_true > 1.5 , and calculate the mae as:只需在df.y_true > 1.5索引您的数据df.y_true > 1.5 ,并计算mae为:

df_ = df[df.y_true > 1.5]
(df_.y_true - df_.y_pred).abs().mean()
# 0.25

I found the piece of code I wanted.我找到了我想要的一段代码。 Using pipe solves the problem of reassigning:使用管道解决重新分配的问题:

df[df.y_true > 1.5].pipe(lambda x: mae(x.y_true, x.y_pred))
# 0.25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM