This provides an implementation of the Stacking ensemble method. Stacking learns several base classifiers and a top level classifier learns to predict the target based on the outputs of all the ensambled models. Historically a linear model (such as LinearBatch) is used, which translates to l
| 30 | * @author Edward Raff |
| 31 | */ |
| 32 | public class Stacking implements Classifier, Regressor |
| 33 | { |
| 34 | |
| 35 | private static final long serialVersionUID = -6173323872903232074L; |
| 36 | private int folds; |
| 37 | /** |
| 38 | * The number of weights needed per model |
| 39 | */ |
| 40 | private int weightsPerModel; |
| 41 | private Classifier aggregatingClassifier; |
| 42 | private List<Classifier> baseClassifiers; |
| 43 | |
| 44 | private Regressor aggregatingRegressor; |
| 45 | private List<Regressor> baseRegressors; |
| 46 | |
| 47 | public static final int DEFAULT_FOLDS = 3; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new Stacking classifier |
| 51 | * @param folds the number of cross validation folds for learning the base model |
| 52 | * @param aggregatingClassifier the classifier used to merge the results of all the input classifiers |
| 53 | * @param baseClassifiers the list of base classifiers to ensemble |
| 54 | */ |
| 55 | public Stacking(int folds, Classifier aggregatingClassifier, List<Classifier> baseClassifiers) |
| 56 | { |
| 57 | if(baseClassifiers.size() < 2) |
| 58 | throw new IllegalArgumentException("base classifiers must contain at least 2 elements, not " + baseClassifiers.size()); |
| 59 | setFolds(folds); |
| 60 | this.aggregatingClassifier = aggregatingClassifier; |
| 61 | this.baseClassifiers = baseClassifiers; |
| 62 | |
| 63 | boolean allRegressors = aggregatingClassifier instanceof Regressor; |
| 64 | for(Classifier cl : baseClassifiers) |
| 65 | if(!(cl instanceof Regressor)) |
| 66 | allRegressors = false; |
| 67 | |
| 68 | if(allRegressors) |
| 69 | { |
| 70 | aggregatingRegressor = (Regressor) aggregatingClassifier; |
| 71 | baseRegressors = (List) baseClassifiers;//ugly type easure exploitation... |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a new Stacking classifier |
| 77 | * @param folds the number of cross validation folds for learning the base model |
| 78 | * @param aggregatingClassifier the classifier used to merge the results of all the input classifiers |
| 79 | * @param baseClassifiers the array of base classifiers to ensemble |
| 80 | */ |
| 81 | public Stacking(int folds, Classifier aggregatingClassifier, Classifier... baseClassifiers) |
| 82 | { |
| 83 | this(folds, aggregatingClassifier, Arrays.asList(baseClassifiers)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a new Stacking classifier that uses {@value #DEFAULT_FOLDS} folds of cross validation |
| 88 | * @param aggregatingClassifier the classifier used to merge the results of all the input classifiers |
| 89 | * @param baseClassifiers the list of base classifiers to ensemble |
nothing calls this directly
no outgoing calls
no test coverage detected