Scikit-Learn Integration

Example using pliers as a node in a typical scikit-learn pipeline. Example code taken from scikit-learn's website


In [18]:
# Data setup
from glob import glob
from pliers.tests.utils import get_test_data_path
from os.path import join
import numpy as np

X = glob(join(get_test_data_path(), 'image', '*.jpg'))
# Just use random classes since this is just an example
y = np.random.randint(0, 3, len(X))
print('Number of images found: %d' % len(X))


Number of images found: 4

In [12]:
# Pliers setup
from pliers.graph import Graph
from pliers.utils.scikit import PliersTransformer
g = Graph({'roots':[{'transformer':'BrightnessExtractor'},
                    {'transformer':'SharpnessExtractor'},
                    {'transformer':'VibranceExtractor'}]})

In [19]:
# Sklearn setup
from sklearn import svm
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression
from sklearn.pipeline import Pipeline

# ANOVA SVM-C Pipeline
pliers_transformer = PliersTransformer(g)
anova_filter = SelectKBest(f_regression, k=2)
clf = svm.SVC(kernel='linear')
pipeline = Pipeline([('pliers', pliers_transformer), ('anova', anova_filter), ('svc', clf)])

In [20]:
# Fit and get training accuracy
pipeline.set_params(svc__C=.1).fit(X, y)
prediction = pipeline.predict(X)
pipeline.score(X, y)


Stim: 4it [00:00, 33.86it/s]
Stim: 4it [00:00, 43.70it/s]
Stim: 4it [00:00, 22.60it/s]
Stim: 4it [00:00, 36.86it/s]
Stim: 4it [00:00, 39.83it/s]
Stim: 4it [00:00, 20.65it/s]
Stim: 4it [00:00, 34.34it/s]
Stim: 4it [00:00, 30.44it/s]
Stim: 4it [00:00, 18.68it/s]
Out[20]:
1.0

In [21]:
# Getting the selected features chosen by anova_filter
pipeline.named_steps['anova'].get_support()


Out[21]:
array([ True, False,  True], dtype=bool)

In [ ]: