Skip to Content
Mastering Python for Finance - Second Edition
book

Mastering Python for Finance - Second Edition

by James Ma Weiming
April 2019
Intermediate to advanced
426 pages
11h 13m
English
Packt Publishing
Content preview from Mastering Python for Finance - Second Edition

Bagging regressor

The BaggingRegressor class of sklearn.ensemble implements the bagging regressor. We can see how a bagging regressor works for multi-asset predictions of the percentage returns of JPM. The following code illustrates this:

In [ ]:    from sklearn.ensemble import BaggingRegressor    class BaggingRegressorModel(LinearRegressionModel):        def get_model(self):            return BaggingRegressor(n_estimators=20, random_state=0) In [ ]:    bagging = BaggingRegressorModel()    bagging.learn(df_lagged, y, start_date='2018',                   end_date='2019', lookback_period=10)

We created a class named BaggingRegressorModel that extends LinearRegressionModel, and the get_model() method is overridden to return the bagging regressor. The n_estimators parameter specifies 20

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Finance - Second Edition

Python for Finance - Second Edition

Yuxing Yan
Python for Finance

Python for Finance

Yves Hilpisch

Publisher Resources

ISBN: 9781789346466Supplemental Content