Hi,
I'd like to perform machine learning model training in the research environment, store the trained models, and load them for prediction in the algorithm environment.
I've scanned the documentation and have not found any way to perform that.
Is it possible?
Mia Alissi
Yes, it is possible to train a machine learning model in the research environment, store the trained models, and load them for prediction in the algorithm environment in QuantConnect.
After you train a model, you can save it into the Object Store. In QuantConnect Cloud, your Object Store data is backed up on QuantConnect servers. If you save models in live algorithms, save them at the end of the training method so you can access the trained model again if your algorithm stops executing. If you save models in backtests, save them during the
OnEndOfAlgorithm
event handler so that saving multiple times doesn't slow down your backtest.You can load machine learning models from the Object Store or a custom data file like pickle. If you load models from the Object Store, before you load the model into your algorithm, in the
Initialize
method, check if the Object Store already contains the model.If you train a model in the Research Environment, you can also save it into the Object Store to transfer it to the backtesting and live trading environment.
For sklearn models, you can save/read any data including sklearn models into/from objectstore by using JSON format. You will need a dictionary to save 2 key-value pairs within it:
yourTrainedModel.get_params(),
``` For more information, you can refer to this docs and this stack overflow's thread.Please note that to avoid look-ahead bias in backtests, don't train your model on the same data you use to test the model.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Tal Davidson
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!