Using a stacking ensemble with Shark-ML

To show the implementation of more ensemble learning techniques, we can develop the stacking approach manually. This is not hard with the Shark-ML library, or indeed any other library.

First, we need to define weak (or elementary) algorithms that we are going to use for stacking. To unify access to the weak algorithms, we defined the base class, as follows:

struct WeakModel {virtual ~WeakModel() {}  virtual void Train(const ClassificationDataset& data_set) = 0;  virtual LinearClassifier<RealVector>& GetClassifier() = 0;};

We used it for creating three weak algorithms' models—the logistic regression, the linear discriminant analysis (LDA), and the linear SVM models, as illustrated in the following code ...

Get Hands-On Machine Learning with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.