小编dev*_*tel的帖子

如何在课堂上使用 Pandas UDF

我试图弄清楚如何在 Python 中的类方法中使用selfinPandasUDF.GroupBy.Apply并在其中传递参数。我尝试了很多不同的方法但无法使其发挥作用。我还在互联网上广泛搜索了 PandasUDF 的示例,该示例在带有 self 和参数的类中使用,但找不到类似的内容。我知道如何用 来做所有前面提到的事情Pandas.GroupBy.Apply

我可以让它工作的唯一方法是将其声明为静态方法

class Train:
    return_type = StructType([
        StructField("div_nbr", FloatType()),
        StructField("store_nbr", FloatType()),
        StructField("model_str", BinaryType())
    ])
    function_type = PandasUDFType.GROUPED_MAP

    def __init__(self):
       ............

    def run_train(self):
         output = sp_df.groupby(['A', 'B']).apply(self.model_train)
         output.show(10)

    @staticmethod
    @pandas_udf(return_type, function_type)
    def model_train(pd_df):
        features_name = ['days_into_year', 'months_into_year', 'minutes_into_day', 'hour_of_day', 'recency']

        X = pd_df[features_name].copy()
        Y = pd.DataFrame(pd_df['trans_type_value']).copy()

        estimator_1 = XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=300, verbosity=1,
                                   objective='reg:squarederror', booster='gbtree', n_jobs=-1, gamma=0,
                                   min_child_weight=5, max_delta_step=0, subsample=0.6, colsample_bytree=0.8,
                                   colsample_bylevel=1, colsample_bynode=1, reg_alpha=0, reg_lambda=1,
                                   scale_pos_weight=1, base_score=0.5, random_state=1234, missing=None, …
Run Code Online (Sandbox Code Playgroud)

pandas pyspark

8
推荐指数
1
解决办法
2074
查看次数

标签 统计

pandas ×1

pyspark ×1