Package: ibex
Ami Tavory, Shahar Azulay, Tali Raveh-SadkaThis library aims for two (somewhat independent) goals:providingpandasadapters forestimators conforming to the scikit-learn protocol, in particular those ofscikit-learnitselfproviding easier, and more succinct ways of combining estimators, features, and pipelines(You might also want to check out the excellentpandas-sklearnwhich has the same aims, but takes a very different
approach.)The full documentation atdefines these matters in detail, but the library has an extremely-smallinterface.TL;DRThe following short example shows the main points of the library. It is an adaptation of the scikit-learn exampleConcatenating multiple feature extraction methods. In this example, we build a classifier for theiris datasetusing a combination ofPCA,univariate feature selection, and asupport vecor machine classifier.We first load the Iris dataset into a pandasDataFrame.>>> import numpy as np
>>> from sklearn import datasets
>>> import pandas as pd
>>>
>>> iris = datasets.load_iris()
>>> features, targets, iris = iris['feature_names'], iris['target_names'], pd.DataFrame(
...     np.c_[iris['data'], iris['target']],
...     columns=iris['feature_names']+['class'])
>>> iris['class'] = iris['class'].map(pd.Series(targets))
>>>
>>> iris.head()
   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  \
0                5.1               3.5                1.4               0.2
1                4.9               3.0                1.4               0.2
2                4.7               3.2                1.3               0.2
3                4.6               3.1                1.5               0.2
4                5.0               3.6                1.4               0.2
<BLANKLINE>
    class
0  setosa
1  setosa
2  setosa
3  setosa
4  setosaNow, we import the relevant steps. Note that, in this example, we import them fromibex.sklearnrather thansklearn.>>> 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 fromsklearnas well, and use them alongside and together with the steps ofibex.sklearn.)Finally, we construct a pipeline that, given aDataFrameof features:horizontally concatenates a 2-component PCADataFrame, and the best-featureDataFrame, to a resultingDataFramethen, passes the result to a support-vector machine classifier outputting a pandas series:>>> clf = PdPCA(n_components=2) + PdSelectKBest(k=1) | PdSVC(kernel="linear")clfis now apandas-ware classifier, but otherwise can be used pretty much like allsklearnestimator. For example,>>> param_grid = dict(
...     featureunion__pca__n_components=[1, 2, 3],
...     featureunion__selectkbest__k=[1, 2],
...     svc__C=[0.1, 1, 10])
>>> try:
...     from ibex.sklearn.model_selection import GridSearchCV as PdGridSearchCV
... except: # Accomodate older versions of sklearn
...     from ibex.sklearn.grid_search 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?The estimators performverification and processingon the inputs and outputs. They verify column names following calls tofit, and index results according to those of the inputs. This helps catch bugs.The results are much more interpretable:>>> svc = PdSVC(kernel="linear", probability=True)Find the coefficients of the boundaries between the different classes:>>> svc.fit(iris[features], iris['class']).coef_
            sepal length (cm)  sepal width (cm)  petal length (cm)  \
setosa              -0.046259          0.521183          -1.003045
versicolor          -0.007223          0.178941          -0.538365
virginica            0.595498          0.973900          -2.031000
<BLANKLINE>
            petal width (cm)
setosa             -0.464130
versicolor         -0.292393
virginica          -2.006303Predict belonging to classes:>>> svc.fit(iris[features], iris['class']).predict_proba(iris[features])
    setosa  versicolor  virginica
0    0.97...    0.01...   0.00...
...Find the coefficients of the boundaries between the different classes in a pipeline:>>> clf = PdPCA(n_components=2) + PdSelectKBest(k=1) | svc
>>> clf = clf.fit(iris[features], iris['class'])
>>> svc.coef_
                pca                 selectkbest
            comp_0    comp_1 petal length (cm)
setosa     -0.757016  ...0.376680         -0.575197
versicolor -0.351218  ...0.141699         -0.317562
virginica  -1.529320  ...1.472771         -1.509391It allowswritinfitg Pandas-munging estimators(see alsoMultiple-Row Features In The Movielens Dataset).UsingDataFramemetadata, it allows writing more complex meta-learning algorithms, such as stacking and nested labeled and stratified cross validation.The pipeline syntax is succinct and clear (seeMotivation For Shorter Combinations).