In [20]:
from sklearn.pipeline import Pipeline, FeatureUnion
# http://scikit-learn.org/stable/modules/pipeline.html

# Pipeline can be used to chain multiple estimators into one. This is useful as there is often a fixed sequence of
# steps in processing the data, for example feature selection, normalization and classification. Pipeline serves 
# two purposes here:
# Convenience: You only have to call fit and predict once on your data to fit a whole sequence of estimators.
# Joint parameter selection: You can grid search over parameters of all estimators in the pipeline at once.
# All estimators in a pipeline, except the last one, must be transformers (i.e. must have a transform method). 
# The last estimator may be any type (transformer, classifier, etc.).

# FeatureUnion combines several transformer objects into a new transformer that combines their output. 
# A FeatureUnion takes a list of transformer objects. During fitting, each of these is fit to the data 
# independently. For transforming data, the transformers are applied in parallel, and the sample vectors 
# they output are concatenated end-to-end into larger vectors.
# FeatureUnion serves the same purposes as Pipeline - convenience and joint parameter estimation and validation.
# FeatureUnion and Pipeline can be combined to create complex models.
# (A FeatureUnion has no way of checking whether two transformers might produce identical features. 
# It only produces a union when the feature sets are disjoint, and making sure they are is the caller’s 
# responsibility.)
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC
# C-Support Vector Classification.
# The implementation is based on libsvm. The fit time complexity is more than quadratic with the number of samples
# which makes it hard to scale to dataset with more than a couple of 10000 samples.
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
# Principal component analysis (PCA)
# Linear dimensionality reduction using Singular Value Decomposition of the data and keeping only the most 
# significant singular vectors to project the data to a lower dimensional space.
# This implementation uses the scipy.linalg implementation of the singular value decomposition. It only works 
# for dense arrays and is not scalable to large dimensional data.
from sklearn.feature_selection import SelectKBest

In [21]:
iris = load_iris()
iris


Out[21]:
{'DESCR': 'Iris Plants Database\n\nNotes\n-----\nData Set Characteristics:\n    :Number of Instances: 150 (50 in each of three classes)\n    :Number of Attributes: 4 numeric, predictive attributes and the class\n    :Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm\n        - class:\n                - Iris-Setosa\n                - Iris-Versicolour\n                - Iris-Virginica\n    :Summary Statistics:\n    ============== ==== ==== ======= ===== ====================\n                    Min  Max   Mean    SD   Class Correlation\n    ============== ==== ==== ======= ===== ====================\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n    petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)\n    ============== ==== ==== ======= ===== ====================\n    :Missing Attribute Values: None\n    :Class Distribution: 33.3% for each of 3 classes.\n    :Creator: R.A. Fisher\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n    :Date: July, 1988\n\nThis is a copy of UCI ML iris datasets.\nhttp://archive.ics.uci.edu/ml/datasets/Iris\n\nThe famous Iris database, first used by Sir R.A Fisher\n\nThis is perhaps the best known database to be found in the\npattern recognition literature.  Fisher\'s paper is a classic in the field and\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant.  One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\nReferences\n----------\n   - Fisher,R.A. "The use of multiple measurements in taxonomic problems"\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n     Mathematical Statistics" (John Wiley, NY, 1950).\n   - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n     Structure and Classification Rule for Recognition in Partially Exposed\n     Environments".  IEEE Transactions on Pattern Analysis and Machine\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions\n     on Information Theory, May 1972, 431-433.\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II\n     conceptual clustering system finds 3 classes in the data.\n   - Many, many more ...\n',
 'data': array([[ 5.1,  3.5,  1.4,  0.2],
        [ 4.9,  3. ,  1.4,  0.2],
        [ 4.7,  3.2,  1.3,  0.2],
        [ 4.6,  3.1,  1.5,  0.2],
        [ 5. ,  3.6,  1.4,  0.2],
        [ 5.4,  3.9,  1.7,  0.4],
        [ 4.6,  3.4,  1.4,  0.3],
        [ 5. ,  3.4,  1.5,  0.2],
        [ 4.4,  2.9,  1.4,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 5.4,  3.7,  1.5,  0.2],
        [ 4.8,  3.4,  1.6,  0.2],
        [ 4.8,  3. ,  1.4,  0.1],
        [ 4.3,  3. ,  1.1,  0.1],
        [ 5.8,  4. ,  1.2,  0.2],
        [ 5.7,  4.4,  1.5,  0.4],
        [ 5.4,  3.9,  1.3,  0.4],
        [ 5.1,  3.5,  1.4,  0.3],
        [ 5.7,  3.8,  1.7,  0.3],
        [ 5.1,  3.8,  1.5,  0.3],
        [ 5.4,  3.4,  1.7,  0.2],
        [ 5.1,  3.7,  1.5,  0.4],
        [ 4.6,  3.6,  1. ,  0.2],
        [ 5.1,  3.3,  1.7,  0.5],
        [ 4.8,  3.4,  1.9,  0.2],
        [ 5. ,  3. ,  1.6,  0.2],
        [ 5. ,  3.4,  1.6,  0.4],
        [ 5.2,  3.5,  1.5,  0.2],
        [ 5.2,  3.4,  1.4,  0.2],
        [ 4.7,  3.2,  1.6,  0.2],
        [ 4.8,  3.1,  1.6,  0.2],
        [ 5.4,  3.4,  1.5,  0.4],
        [ 5.2,  4.1,  1.5,  0.1],
        [ 5.5,  4.2,  1.4,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 5. ,  3.2,  1.2,  0.2],
        [ 5.5,  3.5,  1.3,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 4.4,  3. ,  1.3,  0.2],
        [ 5.1,  3.4,  1.5,  0.2],
        [ 5. ,  3.5,  1.3,  0.3],
        [ 4.5,  2.3,  1.3,  0.3],
        [ 4.4,  3.2,  1.3,  0.2],
        [ 5. ,  3.5,  1.6,  0.6],
        [ 5.1,  3.8,  1.9,  0.4],
        [ 4.8,  3. ,  1.4,  0.3],
        [ 5.1,  3.8,  1.6,  0.2],
        [ 4.6,  3.2,  1.4,  0.2],
        [ 5.3,  3.7,  1.5,  0.2],
        [ 5. ,  3.3,  1.4,  0.2],
        [ 7. ,  3.2,  4.7,  1.4],
        [ 6.4,  3.2,  4.5,  1.5],
        [ 6.9,  3.1,  4.9,  1.5],
        [ 5.5,  2.3,  4. ,  1.3],
        [ 6.5,  2.8,  4.6,  1.5],
        [ 5.7,  2.8,  4.5,  1.3],
        [ 6.3,  3.3,  4.7,  1.6],
        [ 4.9,  2.4,  3.3,  1. ],
        [ 6.6,  2.9,  4.6,  1.3],
        [ 5.2,  2.7,  3.9,  1.4],
        [ 5. ,  2. ,  3.5,  1. ],
        [ 5.9,  3. ,  4.2,  1.5],
        [ 6. ,  2.2,  4. ,  1. ],
        [ 6.1,  2.9,  4.7,  1.4],
        [ 5.6,  2.9,  3.6,  1.3],
        [ 6.7,  3.1,  4.4,  1.4],
        [ 5.6,  3. ,  4.5,  1.5],
        [ 5.8,  2.7,  4.1,  1. ],
        [ 6.2,  2.2,  4.5,  1.5],
        [ 5.6,  2.5,  3.9,  1.1],
        [ 5.9,  3.2,  4.8,  1.8],
        [ 6.1,  2.8,  4. ,  1.3],
        [ 6.3,  2.5,  4.9,  1.5],
        [ 6.1,  2.8,  4.7,  1.2],
        [ 6.4,  2.9,  4.3,  1.3],
        [ 6.6,  3. ,  4.4,  1.4],
        [ 6.8,  2.8,  4.8,  1.4],
        [ 6.7,  3. ,  5. ,  1.7],
        [ 6. ,  2.9,  4.5,  1.5],
        [ 5.7,  2.6,  3.5,  1. ],
        [ 5.5,  2.4,  3.8,  1.1],
        [ 5.5,  2.4,  3.7,  1. ],
        [ 5.8,  2.7,  3.9,  1.2],
        [ 6. ,  2.7,  5.1,  1.6],
        [ 5.4,  3. ,  4.5,  1.5],
        [ 6. ,  3.4,  4.5,  1.6],
        [ 6.7,  3.1,  4.7,  1.5],
        [ 6.3,  2.3,  4.4,  1.3],
        [ 5.6,  3. ,  4.1,  1.3],
        [ 5.5,  2.5,  4. ,  1.3],
        [ 5.5,  2.6,  4.4,  1.2],
        [ 6.1,  3. ,  4.6,  1.4],
        [ 5.8,  2.6,  4. ,  1.2],
        [ 5. ,  2.3,  3.3,  1. ],
        [ 5.6,  2.7,  4.2,  1.3],
        [ 5.7,  3. ,  4.2,  1.2],
        [ 5.7,  2.9,  4.2,  1.3],
        [ 6.2,  2.9,  4.3,  1.3],
        [ 5.1,  2.5,  3. ,  1.1],
        [ 5.7,  2.8,  4.1,  1.3],
        [ 6.3,  3.3,  6. ,  2.5],
        [ 5.8,  2.7,  5.1,  1.9],
        [ 7.1,  3. ,  5.9,  2.1],
        [ 6.3,  2.9,  5.6,  1.8],
        [ 6.5,  3. ,  5.8,  2.2],
        [ 7.6,  3. ,  6.6,  2.1],
        [ 4.9,  2.5,  4.5,  1.7],
        [ 7.3,  2.9,  6.3,  1.8],
        [ 6.7,  2.5,  5.8,  1.8],
        [ 7.2,  3.6,  6.1,  2.5],
        [ 6.5,  3.2,  5.1,  2. ],
        [ 6.4,  2.7,  5.3,  1.9],
        [ 6.8,  3. ,  5.5,  2.1],
        [ 5.7,  2.5,  5. ,  2. ],
        [ 5.8,  2.8,  5.1,  2.4],
        [ 6.4,  3.2,  5.3,  2.3],
        [ 6.5,  3. ,  5.5,  1.8],
        [ 7.7,  3.8,  6.7,  2.2],
        [ 7.7,  2.6,  6.9,  2.3],
        [ 6. ,  2.2,  5. ,  1.5],
        [ 6.9,  3.2,  5.7,  2.3],
        [ 5.6,  2.8,  4.9,  2. ],
        [ 7.7,  2.8,  6.7,  2. ],
        [ 6.3,  2.7,  4.9,  1.8],
        [ 6.7,  3.3,  5.7,  2.1],
        [ 7.2,  3.2,  6. ,  1.8],
        [ 6.2,  2.8,  4.8,  1.8],
        [ 6.1,  3. ,  4.9,  1.8],
        [ 6.4,  2.8,  5.6,  2.1],
        [ 7.2,  3. ,  5.8,  1.6],
        [ 7.4,  2.8,  6.1,  1.9],
        [ 7.9,  3.8,  6.4,  2. ],
        [ 6.4,  2.8,  5.6,  2.2],
        [ 6.3,  2.8,  5.1,  1.5],
        [ 6.1,  2.6,  5.6,  1.4],
        [ 7.7,  3. ,  6.1,  2.3],
        [ 6.3,  3.4,  5.6,  2.4],
        [ 6.4,  3.1,  5.5,  1.8],
        [ 6. ,  3. ,  4.8,  1.8],
        [ 6.9,  3.1,  5.4,  2.1],
        [ 6.7,  3.1,  5.6,  2.4],
        [ 6.9,  3.1,  5.1,  2.3],
        [ 5.8,  2.7,  5.1,  1.9],
        [ 6.8,  3.2,  5.9,  2.3],
        [ 6.7,  3.3,  5.7,  2.5],
        [ 6.7,  3. ,  5.2,  2.3],
        [ 6.3,  2.5,  5. ,  1.9],
        [ 6.5,  3. ,  5.2,  2. ],
        [ 6.2,  3.4,  5.4,  2.3],
        [ 5.9,  3. ,  5.1,  1.8]]),
 'feature_names': ['sepal length (cm)',
  'sepal width (cm)',
  'petal length (cm)',
  'petal width (cm)'],
 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 'target_names': array(['setosa', 'versicolor', 'virginica'], 
       dtype='|S10')}

In [22]:
X, y = iris.data, iris.target

In [23]:
# This dataset is way to high-dimensional. Better do PCA:
pca = PCA(n_components=2)

In [24]:
# Maybe some original features where good, too?
selection = SelectKBest(k=1)
selection


Out[24]:
SelectKBest(k=1, score_func=<function f_classif at 0x0000000014E0F7B8>)

In [25]:
# Build estimator from PCA and Univariate selection:
combined_features = FeatureUnion([("pca", pca), ("univ_select", selection)])
combined_features


Out[25]:
FeatureUnion(n_jobs=1,
       transformer_list=[('pca', PCA(copy=True, n_components=2, whiten=False)), ('univ_select', SelectKBest(k=1, score_func=<function f_classif at 0x0000000014E0F7B8>))],
       transformer_weights=None)

In [26]:
# Use combined features to transform dataset:
X_features = combined_features.fit(X, y).transform(X)
X_features


Out[26]:
array([[-2.68420713, -0.32660731,  1.4       ],
       [-2.71539062,  0.16955685,  1.4       ],
       [-2.88981954,  0.13734561,  1.3       ],
       [-2.7464372 ,  0.31112432,  1.5       ],
       [-2.72859298, -0.33392456,  1.4       ],
       [-2.27989736, -0.74778271,  1.7       ],
       [-2.82089068,  0.08210451,  1.4       ],
       [-2.62648199, -0.17040535,  1.5       ],
       [-2.88795857,  0.57079803,  1.4       ],
       [-2.67384469,  0.1066917 ,  1.5       ],
       [-2.50652679, -0.65193501,  1.5       ],
       [-2.61314272, -0.02152063,  1.6       ],
       [-2.78743398,  0.22774019,  1.4       ],
       [-3.22520045,  0.50327991,  1.1       ],
       [-2.64354322, -1.1861949 ,  1.2       ],
       [-2.38386932, -1.34475434,  1.5       ],
       [-2.6225262 , -0.81808967,  1.3       ],
       [-2.64832273, -0.31913667,  1.4       ],
       [-2.19907796, -0.87924409,  1.7       ],
       [-2.58734619, -0.52047364,  1.5       ],
       [-2.3105317 , -0.39786782,  1.7       ],
       [-2.54323491, -0.44003175,  1.5       ],
       [-3.21585769, -0.14161557,  1.        ],
       [-2.30312854, -0.10552268,  1.7       ],
       [-2.35617109,  0.03120959,  1.9       ],
       [-2.50791723,  0.13905634,  1.6       ],
       [-2.469056  , -0.13788731,  1.6       ],
       [-2.56239095, -0.37468456,  1.5       ],
       [-2.63982127, -0.31929007,  1.4       ],
       [-2.63284791,  0.19007583,  1.6       ],
       [-2.58846205,  0.19739308,  1.6       ],
       [-2.41007734, -0.41808001,  1.5       ],
       [-2.64763667, -0.81998263,  1.5       ],
       [-2.59715948, -1.10002193,  1.4       ],
       [-2.67384469,  0.1066917 ,  1.5       ],
       [-2.86699985, -0.0771931 ,  1.2       ],
       [-2.62522846, -0.60680001,  1.3       ],
       [-2.67384469,  0.1066917 ,  1.5       ],
       [-2.98184266,  0.48025005,  1.3       ],
       [-2.59032303, -0.23605934,  1.5       ],
       [-2.77013891, -0.27105942,  1.3       ],
       [-2.85221108,  0.93286537,  1.3       ],
       [-2.99829644,  0.33430757,  1.3       ],
       [-2.4055141 , -0.19591726,  1.6       ],
       [-2.20883295, -0.44269603,  1.9       ],
       [-2.71566519,  0.24268148,  1.4       ],
       [-2.53757337, -0.51036755,  1.6       ],
       [-2.8403213 ,  0.22057634,  1.4       ],
       [-2.54268576, -0.58628103,  1.5       ],
       [-2.70391231, -0.11501085,  1.4       ],
       [ 1.28479459, -0.68543919,  4.7       ],
       [ 0.93241075, -0.31919809,  4.5       ],
       [ 1.46406132, -0.50418983,  4.9       ],
       [ 0.18096721,  0.82560394,  4.        ],
       [ 1.08713449, -0.07539039,  4.6       ],
       [ 0.64043675,  0.41732348,  4.5       ],
       [ 1.09522371, -0.28389121,  4.7       ],
       [-0.75146714,  1.00110751,  3.3       ],
       [ 1.04329778, -0.22895691,  4.6       ],
       [-0.01019007,  0.72057487,  3.9       ],
       [-0.5110862 ,  1.26249195,  3.5       ],
       [ 0.51109806,  0.10228411,  4.2       ],
       [ 0.26233576,  0.5478933 ,  4.        ],
       [ 0.98404455,  0.12436042,  4.7       ],
       [-0.174864  ,  0.25181557,  3.6       ],
       [ 0.92757294, -0.46823621,  4.4       ],
       [ 0.65959279,  0.35197629,  4.5       ],
       [ 0.23454059,  0.33192183,  4.1       ],
       [ 0.94236171,  0.54182226,  4.5       ],
       [ 0.0432464 ,  0.58148945,  3.9       ],
       [ 1.11624072,  0.08421401,  4.8       ],
       [ 0.35678657,  0.06682383,  4.        ],
       [ 1.29646885,  0.32756152,  4.9       ],
       [ 0.92050265,  0.18239036,  4.7       ],
       [ 0.71400821, -0.15037915,  4.3       ],
       [ 0.89964086, -0.32961098,  4.4       ],
       [ 1.33104142, -0.24466952,  4.8       ],
       [ 1.55739627, -0.26739258,  5.        ],
       [ 0.81245555,  0.16233157,  4.5       ],
       [-0.30733476,  0.36508661,  3.5       ],
       [-0.07034289,  0.70253793,  3.8       ],
       [-0.19188449,  0.67749054,  3.7       ],
       [ 0.13499495,  0.31170964,  3.9       ],
       [ 1.37873698,  0.42120514,  5.1       ],
       [ 0.58727485,  0.48328427,  4.5       ],
       [ 0.8072055 , -0.19505396,  4.5       ],
       [ 1.22042897, -0.40803534,  4.7       ],
       [ 0.81286779,  0.370679  ,  4.4       ],
       [ 0.24519516,  0.26672804,  4.1       ],
       [ 0.16451343,  0.67966147,  4.        ],
       [ 0.46303099,  0.66952655,  4.4       ],
       [ 0.89016045,  0.03381244,  4.6       ],
       [ 0.22887905,  0.40225762,  4.        ],
       [-0.70708128,  1.00842476,  3.3       ],
       [ 0.35553304,  0.50321849,  4.2       ],
       [ 0.33112695,  0.21118014,  4.2       ],
       [ 0.37523823,  0.29162202,  4.2       ],
       [ 0.64169028, -0.01907118,  4.3       ],
       [-0.90846333,  0.75156873,  3.        ],
       [ 0.29780791,  0.34701652,  4.1       ],
       [ 2.53172698,  0.01184224,  6.        ],
       [ 1.41407223,  0.57492506,  5.1       ],
       [ 2.61648461, -0.34193529,  5.9       ],
       [ 1.97081495,  0.18112569,  5.6       ],
       [ 2.34975798,  0.04188255,  5.8       ],
       [ 3.39687992, -0.54716805,  6.6       ],
       [ 0.51938325,  1.19135169,  4.5       ],
       [ 2.9320051 , -0.35237701,  6.3       ],
       [ 2.31967279,  0.24554817,  5.8       ],
       [ 2.91813423, -0.78038063,  6.1       ],
       [ 1.66193495, -0.2420384 ,  5.1       ],
       [ 1.80234045,  0.21615461,  5.3       ],
       [ 2.16537886, -0.21528028,  5.5       ],
       [ 1.34459422,  0.77641543,  5.        ],
       [ 1.5852673 ,  0.53930705,  5.1       ],
       [ 1.90474358, -0.11881899,  5.3       ],
       [ 1.94924878, -0.04073026,  5.5       ],
       [ 3.48876538, -1.17154454,  6.7       ],
       [ 3.79468686, -0.25326557,  6.9       ],
       [ 1.29832982,  0.76101394,  5.        ],
       [ 2.42816726, -0.37678197,  5.7       ],
       [ 1.19809737,  0.60557896,  4.9       ],
       [ 3.49926548, -0.45677347,  6.7       ],
       [ 1.38766825,  0.20403099,  4.9       ],
       [ 2.27585365, -0.33338653,  5.7       ],
       [ 2.61419383, -0.55836695,  6.        ],
       [ 1.25762518,  0.179137  ,  4.8       ],
       [ 1.29066965,  0.11642525,  4.9       ],
       [ 2.12285398,  0.21085488,  5.6       ],
       [ 2.3875644 , -0.46251925,  5.8       ],
       [ 2.84096093, -0.37274259,  6.1       ],
       [ 3.2323429 , -1.37052404,  6.4       ],
       [ 2.15873837,  0.21832553,  5.6       ],
       [ 1.4431026 ,  0.14380129,  5.1       ],
       [ 1.77964011,  0.50146479,  5.6       ],
       [ 3.07652162, -0.68576444,  6.1       ],
       [ 2.14498686, -0.13890661,  5.6       ],
       [ 1.90486293, -0.04804751,  5.5       ],
       [ 1.16885347,  0.1645025 ,  4.8       ],
       [ 2.10765373, -0.37148225,  5.4       ],
       [ 2.31430339, -0.18260885,  5.6       ],
       [ 1.92245088, -0.40927118,  5.1       ],
       [ 1.41407223,  0.57492506,  5.1       ],
       [ 2.56332271, -0.2759745 ,  5.9       ],
       [ 2.41939122, -0.30350394,  5.7       ],
       [ 1.94401705, -0.18741522,  5.2       ],
       [ 1.52566363,  0.37502085,  5.        ],
       [ 1.76404594, -0.07851919,  5.2       ],
       [ 1.90162908, -0.11587675,  5.4       ],
       [ 1.38966613,  0.28288671,  5.1       ]])

In [27]:
svm = SVC(kernel="linear")
#  kernel must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable.

In [28]:
pipeline = Pipeline([("features", combined_features), ("svm", svm)])
# Do grid search over k, n_components and C:
param_grid = dict(features__pca__n_components=[1, 2, 3],
                  features__univ_select__k=[1, 2],
                  svm__C=[0.1, 1, 10])
param_grid


Out[28]:
{'features__pca__n_components': [1, 2, 3],
 'features__univ_select__k': [1, 2],
 'svm__C': [0.1, 1, 10]}

In [29]:
grid_search = GridSearchCV(pipeline, param_grid=param_grid, verbose=10)
grid_search.fit(X, y)


Fitting 3 folds for each of 18 candidates, totalling 54 fits
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1, score=0.960784 -   0.1s
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1, score=0.901961 -   0.0s
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=1, score=0.941176 -   0.0s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=1, score=0.921569 -   0.0s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=1, score=0.960784 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=1, score=0.921569 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2, score=0.960784 -   0.0s
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2, score=0.921569 -   0.0s
[CV] features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=0.1, features__univ_select__k=2, score=0.979167 -   0.0s
[Parallel(n_jobs=1)]: Done   1 jobs       | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done   2 jobs       | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done   5 jobs       | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done   8 jobs       | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done  13 jobs       | elapsed:    0.2s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=2, score=0.960784 -   0.0s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=2, score=0.921569 -   0.0s
[CV] features__pca__n_components=1, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=1, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=2, score=0.980392 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=2, score=0.901961 -   0.0s
[CV] features__pca__n_components=1, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=1, svm__C=10, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1, score=0.960784 -   0.0s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1, score=0.901961 -   0.0s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=1, score=0.980392 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=1, score=0.941176 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=1, score=0.980392 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=1, score=0.941176 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2, score=0.980392 -   0.0s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2, score=0.941176 -   0.0s
[Parallel(n_jobs=1)]: Done  18 jobs       | elapsed:    0.2s
[Parallel(n_jobs=1)]: Done  25 jobs       | elapsed:    0.2s
[Parallel(n_jobs=1)]: Done  32 jobs       | elapsed:    0.3s
[CV] features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=0.1, features__univ_select__k=2, score=0.979167 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=2, score=0.960784 -   0.0s
[CV] features__pca__n_components=2, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=1, features__univ_select__k=2, score=0.979167 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=2, score=0.980392 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=2, score=0.921569 -   0.0s
[CV] features__pca__n_components=2, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=2, svm__C=10, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1, score=0.980392 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1, score=0.941176 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=1, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=1, score=0.941176 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=1, score=0.979167 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=1, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=1, score=0.921569 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=1 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=1, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2, score=0.980392 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2, score=0.941176 -   0.0s
[CV] features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=0.1, features__univ_select__k=2, score=0.979167 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=2 
[Parallel(n_jobs=1)]: Done  41 jobs       | elapsed:    0.3s
[Parallel(n_jobs=1)]: Done  50 jobs       | elapsed:    0.4s
[Parallel(n_jobs=1)]: Done  54 out of  54 | elapsed:    0.4s finished
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=2, score=0.960784 -   0.0s
[CV] features__pca__n_components=3, svm__C=1, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=1, features__univ_select__k=2, score=0.979167 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=2, score=1.000000 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=2, score=0.921569 -   0.0s
[CV] features__pca__n_components=3, svm__C=10, features__univ_select__k=2 
[CV]  features__pca__n_components=3, svm__C=10, features__univ_select__k=2, score=1.000000 -   0.0s
Out[29]:
GridSearchCV(cv=None, error_score='raise',
       estimator=Pipeline(steps=[('features', FeatureUnion(n_jobs=1,
       transformer_list=[('pca', PCA(copy=True, n_components=2, whiten=False)), ('univ_select', SelectKBest(k=1, score_func=<function f_classif at 0x0000000014E0F7B8>))],
       transformer_weights=None)), ('svm', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False))]),
       fit_params={}, iid=True, loss_func=None, n_jobs=1,
       param_grid={'features__pca__n_components': [1, 2, 3], 'svm__C': [0.1, 1, 10], 'features__univ_select__k': [1, 2]},
       pre_dispatch='2*n_jobs', refit=True, score_func=None, scoring=None,
       verbose=10)

In [30]:
print(grid_search.best_estimator_)


Pipeline(steps=[('features', FeatureUnion(n_jobs=1,
       transformer_list=[('pca', PCA(copy=True, n_components=2, whiten=False)), ('univ_select', SelectKBest(k=2, score_func=<function f_classif at 0x0000000014E0F7B8>))],
       transformer_weights=None)), ('svm', SVC(C=1, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False))])

In [ ]: