Skip to content

Instantly share code, notes, and snippets.

@mmmayo13
Last active April 4, 2019 10:09
Show Gist options
  • Save mmmayo13/8e42494e5c8f587fea4b2f7cd830e9b9 to your computer and use it in GitHub Desktop.
Save mmmayo13/8e42494e5c8f587fea4b2f7cd830e9b9 to your computer and use it in GitHub Desktop.
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