Package: mlforecast
mlforecastMachine Learning 🤖 ForecastScalable machine learning for time series forecastingmlforecastis a framework to perform time series forecasting using
machine learning models, with the option to scale to massive amounts of
data using remote clusters.InstallPyPIpip install mlforecastconda-forgeconda install -c conda-forge mlforecastFor more detailed instructions you can refer to theinstallation
page.Quick StartMinimal ExampleimportlightgbmaslgbfrommlforecastimportMLForecastfromsklearn.linear_modelimportLinearRegressionmlf=MLForecast(models=[LinearRegression(),lgb.LGBMRegressor()],lags=[1,12],freq='M')mlf.fit(df)mlf.predict(12)Get Started with thisquick
guide.Follow thisend-to-end
walkthroughfor best practices.Sample notebooksm5m5-polarsm4m4-cvWhy?Current Python alternatives for machine learning models are slow,
inaccurate and don’t scale well. So we created a library that can be
used to forecast in production environments.MLForecastincludes
efficient feature engineering to train any machine learning model (withfitandpredictmethods such assklearn) to fit millions of time
series.FeaturesFastest implementations of feature engineering for time series
forecasting in Python.Out-of-the-box compatibility with Spark, Dask, and Ray.Probabilistic Forecasting with Conformal Prediction.Support for exogenous variables and static covariates.Familiarsklearnsyntax:.fitand.predict.Missing something? Please open an issue or write us inExamples and Guides📚End to End
Walkthrough:
model training, evaluation and selection for multiple time series.🔎Probabilistic
Forecasting:
use Conformal Prediction to produce prediciton intervals.👩‍🔬Cross
Validation:
robust model’s performance evaluation.🔌Predict Demand
Peaks:
electricity load forecasting for detecting daily peaks and reducing
electric bills.📈Transfer
Learning:
pretrain a model using a set of time series and then predict another one
using that pretrained model.🌡️Distributed
Training:
use a Dask, Ray or Spark cluster to train models at scale.How to useThe following provides a very basic overview, for a more detailed
description see thedocumentation.Data setupStore your time series in a pandas dataframe in long format, that is,
each row represents an observation for a specific serie and timestamp.frommlforecast.utilsimportgenerate_daily_seriesseries=generate_daily_series(n_series=20,max_length=100,n_static_features=1,static_as_categorical=False,with_trend=True)series.head()unique_iddsystatic_00id_002000-01-0117.519167721id_002000-01-0287.799695722id_002000-01-03177.442975723id_002000-01-04232.704110724id_002000-01-05317.51047472ModelsNext define your models. If you want to use the local interface this can
be any regressor that follows the scikit-learn API. For distributed
training there areLGBMForecastandXGBForecast.importlightgbmaslgbimportxgboostasxgbfromsklearn.ensembleimportRandomForestRegressormodels=[lgb.LGBMRegressor(verbosity=-1),xgb.XGBRegressor(),RandomForestRegressor(random_state=0),]Forecast objectNow instantiate aMLForecastobject with the models and the features
that you want to use. The features can be lags, transformations on the
lags and date features. The lag transformations are defined asnumbajittedfunctions that transform an
array, if they have additional arguments you can either supply a tuple
(transform_func,arg1,arg2, …) or define new functions fixing the
arguments. You can also define differences to apply to the series before
fitting that will be restored when predicting.frommlforecastimportMLForecastfrommlforecast.target_transformsimportDifferencesfromnumbaimportnjitfromwindow_ops.expandingimportexpanding_meanfromwindow_ops.rollingimportrolling_mean@njitdefrolling_mean_28(x):returnrolling_mean(x,window_size=28)fcst=MLForecast(models=models,freq='D',lags=[7,14],lag_transforms={1:[expanding_mean],7:[rolling_mean_28]},date_features=['dayofweek'],target_transforms=[Differences([1])],)TrainingTo compute the features and train the models callfiton yourForecastobject.fcst.fit(series)MLForecast(models=[LGBMRegressor, XGBRegressor, RandomForestRegressor], freq=<Day>, lag_features=['lag7', 'lag14', 'expanding_mean_lag1', 'rolling_mean_28_lag7'], date_features=['dayofweek'], num_threads=1)PredictingTo get the forecasts for the nextndays callpredict(n)on the
forecast object. This will automatically handle the updates required by
the features using a recursive strategy.predictions=fcst.predict(14)predictionsunique_iddsLGBMRegressorXGBRegressorRandomForestRegressor0id_002000-04-04299.923771309.664124298.4241641id_002000-04-05365.424147382.150085365.8160142id_002000-04-06432.562441453.373779436.3606203id_002000-04-07495.628000527.965149503.6701004id_002000-04-0860.78622375.76229962.176080..................275id_192000-03-2336.26678029.88912034.799780276id_192000-03-2444.37098434.96888439.920982277id_192000-03-2550.74622239.97023846.196266278id_192000-03-2658.90652445.12530551.653060279id_192000-03-2763.07394950.68271656.845384280 rows × 5 columnsVisualize resultsfromutilsforecast.plottingimportplot_seriesfig=plot_series(series,predictions,max_ids=4,plot_random=False)fig.savefig('figs/index.png',bbox_inches='tight')How to contributeSeeCONTRIBUTING.md.