Skip to content

shaharazulay/ibex

 
 

Repository files navigation

Ibex

Ami Tavory, Shahar Azulay, Tali Raveh-Sadka

https://travis-ci.org/atavory/ibex.svg?branch=master https://landscape.io/github/atavory/ibex/master/landscape.svg?style=flat http://readthedocs.org/projects/ibex/badge/?version=latest

This library aims for two (somewhat independent) goals:

(You might also want to check out the excellent pandas-sklearn which has the same aims, but takes a very different approach.)

The full documentation at read_the_docs_ibex defines these matters in detail, but the library has an extremely-small interface.

TL;DR

The following short example shows the main points of the library. It is an adaptation of the scikit-learn example Concatenating multiple feature extraction methods. In this example, we build a classifier for the iris dataset using a combination of PCA, univariate feature selection, and a support vecor machine classifier.

We first load the Iris dataset into a pandas DataFrame.

>>> import numpy as np
>>> from sklearn import datasets
>>> import pandas as pd
>>>
>>> iris = datasets.load_iris()
>>> features, iris = iris['feature_names'], pd.DataFrame(
...     np.c_[iris['data'], iris['target']],
...     columns=iris['feature_names']+['class'])
>>>
>>> iris.columns
Index([...'sepal length (cm)', ...'sepal width (cm)', ...'petal length (cm)',
       ...'petal width (cm)', ...'class'],
      dtype='object')

Now, we import the relevant steps. Note that, in this example, we import them from ibex.sklearn rather than sklearn.

>>> from ibex.sklearn.svm import SVC as PDSVC
>>> from ibex.sklearn.feature_selection import SelectKBest as PDSelectKBest
>>> from ibex.sklearn.decomposition import PCA as PDPCA

(Of course, it's possible to import steps from sklearn as well, and use them alongside and together with the steps of ibex.sklearn.)

Finally, we construct a pipeline that, given a DataFrame of features:

  • horizontally concatenates a 2-component PCA DataFrame, and the best-feature DataFrame, to a resulting DataFrame

  • then, passes the result to a support-vector machine classifier outputting a pandas series:

    >>> clf = PDPCA(n_components=2) + PDSelectKBest(k=1) | PDSVC(kernel="linear")

clf is now a pandas-ware classifier, but otherwise can be used pretty much like all sklearn estimator. For example,

>>> param_grid = dict(
...     featureunion__pca__n_components=[1, 2, 3],
...     featureunion__selectkbest__k=[1, 2],
...     svc__C=[0.1, 1, 10])
>>> from ibex.sklearn.model_selection import GridSearchCV as PDGridSearchCV
>>> PDGridSearchCV(clf, param_grid=param_grid).fit(iris[features], iris['class']) # doctest: +SKIP
...

So what does this add to the original version?

  1. The estimators perform verification and processing on the inputs and outputs. They verify column names following calls to fit, and index results according to those of the inputs. This helps catch bugs.
  2. It allows writing Pandas-munging estimators (see also Multiple-Row Features In The Movielens Dataset).
  3. Using DataFrame metadata, it allows writing more complex meta-learning algorithms, such as stacking and nested labeled and stratified cross validation.
  4. The pipeline syntax is succinct and clear (see Motivation For Shorter Combinations).

About

pandas adapters for scikit-learn

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%