I've been experimenting with StackingClassifiers lately, and usually it's used with cross-validation (default: K-fold, num-folds = 5). That's written like so:
from sklearn.pipeline import Pipeline
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, RandomizedSearchCV, KFold
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
model = StackingClassifier(estimators=[
('tree', Pipeline([('tree', DecisionTreeClassifier(random_state=42))])),
('knn', Pipeline([('knn', KNeighborsClassifier())])),
],final_estimator = final_estimator, n_jobs = 10, passthrough = False, cv = KFold())
model.fit(X, y)
which works just fine. However, k-fold cross validation as a way to train a stacking classifier is really quite slow. Ideally i'd like to use to approach where 80% of the training data is used to train the component models, and the other 20% used to train the final_estimator. According to the docs, you can use an iterator that yields a train-test split as an input to cv (Cross Validation) (which is wonky because it's no longer CV). So I suppose two part question
- Is it possible to use StackingClassifier to work in this way, where it's not using cross-validation(CV) at all but a train/test split (mostly to speed up fitting)?
- If so, how would this look? what would i set as my input to cv?
One other note. Looking through potential CV options in the user guide ShuffleSplit with nsplits=1 seemed like a good option, and i tried it.
model = StackingClassifier(estimators=[
('tree', Pipeline([('tree', DecisionTreeClassifier(random_state=42))])),
('knn', Pipeline([('knn', KNeighborsClassifier())])),
],final_estimator = final_estimator, n_jobs = 10, passthrough = False, cv = ShuffleSplit(n_splits=1))
model.fit(X, y)
But unfortunately that doesnt work and throws an error:
ValueError: cross_val_predict only works for partitions