Skip to main content

Pandas Adapters For Scikit-Learn

Project description

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 https://img.shields.io/codecov/c/github/atavory/ibex/master.svg http://readthedocs.org/projects/ibex/badge/?version=latest https://img.shields.io/badge/license-BSD--3--Clause-brightgreen.svg https://badge.fury.io/py/ibex.svg

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibex-0.1.1.4.tar.gz (15.4 kB view hashes)

Uploaded Source

Built Distribution

ibex-0.1.1.4-py3.5.egg (48.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page