繁体   English   中英

将 Prophet 或 Auto ARIMA 与 Ray 一起使用

[英]Using Prophet or Auto ARIMA with Ray

关于雷,我找不到明确的答案。 Ray 是一个用于数据处理和训练的分布式框架。 为了使其以分布式方式工作,必须使用 Modin 或 Ray 支持的其他一些分布式数据分析工具,以便数据可以在整个集群上流动,但是如果我想使用像 Facebook 的 Prophet 或 ARIMA 这样的模型熊猫数据框作为输入? 当我使用 Pandas 数据帧作为模型函数的参数时,它是否只在单个节点上工作,或者是否有可能的解决方法使其在集群上工作?

Ray 能够使用 Pandas 数据帧作为输入来训练模型!

目前,ARIMA 需要一些小的解决方法,因为它通常在幕后使用 statsmodels 库。 为了确保模型正确序列化,需要一个额外的 pickle 步骤。 Ray 将来可能会消除对泡菜变通方法的需要。

请参阅泡菜解决方法的说明: https : //alkaline-ml.com/pmdarima/1.0.0/serialization.html

这是python 3.8和ray 1.8的代码摘录。 请注意,train_model() 和 inference_model() 函数的输入是 pandas 数据帧。 额外的泡菜步骤嵌入在这些函数中。 https://gist.github.com/christy/fd0c00409f28f5db8824be447e7a393f

import ray
import pandas as pd
import pmdarima as pm
from pmdarima.model_selection import train_test_split

# read 8 months of clean, aggregated monthly taxi data
filename = "https://github.com/christy/MachineLearningTools/blob/master/data/clean_taxi_monthly.parquet?raw=true"
g_month = pd.read_parquet(filename) 

# Define a train_model function, default train on 6 months, inference 2
def train_model(theDF:pd.DataFrame, item_col:str
                , item_value:str, target_col:str
                , train_size:int=6) -> list:

    # split data into train/test
    train, test = train_test_split(theDF.loc[(theDF[item_col]==item_value), :], train_size=train_size)
    
    # train and fit auto.arima model
    model = pm.auto_arima(y=train[target_col]
                          ,X=train.loc[:, (train.columns!=target_col) 
                                          & (train.columns!=item_col)]
                         )
    # here is the extra pickle step to handle arima's statsmodel objects
    return [train, test, pickle.dumps(model)]


# Define inference_model function
def inference_model(model_pickle:bytes, test:pd.DataFrame
                    , timestamp_col:str, item_col:str, target_col:str) -> pd.DataFrame:

    # unpickle the model - shouldn't need this except for statsmodel objects
    model = pickle.loads(model_pickle)
    
    # inference on test data
    forecast = pd.DataFrame(model.predict(n_periods=test.shape[0]
                         , X=test.loc[:, (test.columns!=target_col) & (test.columns!=item_col)]
                         , index=test.index))
    
    return forecast


# start-up ray on your laptop for testing purposes
import ray
NUM_CPU = 2
ray.init(
    ignore_reinit_error=True
    , num_cpus = NUM_CPU
)

###########
# run your training as distributed jobs by using ray remote function calls
###########
    
# Convert your regular python functions to ray remote functions
train_model_remote = ray.remote(train_model).options(num_returns=3)  
inference_model_remote = ray.remote(inference_model)
    
# Train every model
item_list = list(g_month['pulocationid'].unique())
model = []
train = []
test = []

for p,v in enumerate(item_list):
    # ray lazy eval
    temp_train, temp_test, temp_model = \
        train_model_remote.remote(g_month
                                  , item_col='pulocationid', item_value=v
                                  , target_col='trip_quantity'
                                  , train_size=6)
    train.append(temp_train)
    test.append(temp_test)
    model.append(temp_model)

# Inference every test dataset
result=[]
for p,v in enumerate(item_list):
    # ray lazy eval
    result.append(inference_model_remote.remote(model[p], test[p]
                                                , timestamp_col='pickup_monthly'
                                                , item_col='pulocationid'
                                                , target_col='trip_quantity'))

# ray blocking step, to get the forecasts
forecast = ray.get(result)

暂无
暂无

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

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