sklearn stacked generalization

Solutions on MaxInterview for sklearn stacked generalization by the best coders in the world

showing results for - "sklearn stacked generalization"
Elías
31 Sep 2020
1from sklearn.datasets import load_iris
2from sklearn.ensemble import RandomForestClassifier
3from sklearn.svm import LinearSVC
4from sklearn.linear_model import LogisticRegression
5from sklearn.preprocessing import StandardScaler
6from sklearn.pipeline import make_pipeline
7from sklearn.ensemble import StackingClassifier
8X, y = load_iris(return_X_y=True)
9estimators = [
10    ('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
11    ('svr', make_pipeline(StandardScaler(),
12                          LinearSVC(random_state=42)))
13]
14clf = StackingClassifier(
15    estimators=estimators, final_estimator=LogisticRegression()
16)
17from sklearn.model_selection import train_test_split
18X_train, X_test, y_train, y_test = train_test_split(
19    X, y, stratify=y, random_state=42
20)
21clf.fit(X_train, y_train).score(X_test, y_test)
22