-
-
Save mnjenga2/05bb11ff89d7815f426e1bb223b71b2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.datasets import load_iris | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.decomposition import PCA | |
| from sklearn.pipeline import Pipeline | |
| from sklearn import tree | |
| # Load and split the data | |
| iris = load_iris() | |
| X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42) | |
| # Construct pipeline | |
| pipe = Pipeline([('scl', StandardScaler()), | |
| ('pca', PCA(n_components=2)), | |
| ('clf', tree.DecisionTreeClassifier(random_state=42))]) | |
| # Fit the pipeline | |
| pipe.fit(X_train, y_train) | |
| # Pipeline test accuracy | |
| print('Test accuracy: %.3f' % pipe.score(X_test, y_test)) | |
| # Pipeline estimator params; estimator is stored as step 3 ([2]), second item ([1]) | |
| print('\nModel hyperparameters:\n', pipe.steps[2][1].get_params()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment