Skip to content

Instantly share code, notes, and snippets.

@mnjenga2
Forked from mmmayo13/pipelines-2a.py
Created April 4, 2019 10:09
Show Gist options
  • Select an option

  • Save mnjenga2/05bb11ff89d7815f426e1bb223b71b2f to your computer and use it in GitHub Desktop.

Select an option

Save mnjenga2/05bb11ff89d7815f426e1bb223b71b2f 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