Git Product home page Git Product logo

patsylearn's Introduction

Patsy-Learn

A simple patsy to scikit-learn adaptor. Be aware that the content of this package is pretty experimental. Feedback welcome.

This package provides two classes:

  • A scikit-learn meta-estimator PatsyModel, that feeds the design matrix created by patsy into a scikit-learn estimator.
  • A scikit-learn transformer, that uses a patsy formula to transform and select features.

Both classes work on pandas DataFrames. If you want to use PatsyModel for a supervised estimator, the input dataframe is expected to contain both the data and the target.

Example

# put the iris dataset into a pandas dataframe
from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()
names = [f_name.replace(" ", "_").strip("_(cm)") for f_name in iris.feature_names]
iris_df = pd.DataFrame(iris.data, columns=names)
iris_df['species'] = iris.target

# create logistic regression with two features
from patsylearn import PatsyModel
from sklearn.linear_model import LogisticRegression
model = PatsyModel(LogisticRegression(), "species ~ sepal_length + petal_length")
# model is an sklearn estimator.

from sklearn.cross_validation import train_test_split
data_train, data_test = train_test_split(iris_df)
model.fit(data_train)
print(model.score(data_test))

# use PatsyTransformer to just select and transform features
transformer = PatsyTransformer("sepal_length + np.log(petal_length) + petal_length:sepal_width")
# transformer is a scikit-learn transformer

transformer.fit(data)
print(transformer.transform(data).shape)

patsylearn's People

Contributors

amueller avatar pnavaro avatar sashaostr avatar scls19fr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

patsylearn's Issues

Fix evalenv, add NA_action kwarg

need

def foo(..., eval_env=0):
    eval_env = EvalEnvironment.capture(eval_env, reference=1)
    # ...
    dmatrix(..., eval_env=eval_env)

Don't add additional intercept

using something like

def foo(formula, ...):
    if not isinstance(formula, patsy.ModelDesc):
        formula = patsy.ModelDesc.from_string(formula)
    if patsy.INTERCEPT in formula.rhs_termlist:
        formula.rhs_termlist.remove(patsy.INTERCEPT)
    # everything else is the same

Thanks @njsmith

Move to scikit-learn-contrib?

This looks like a very neat way for working with data frames, especially specifying feature interactions etc without enormous transformer networks. What's holding it back from being more widely advertised and available?

running example from the homepage gives error


TypeError Traceback (most recent call last)
in ()
16 from sklearn.cross_validation import train_test_split
17 data_train, data_test = train_test_split(iris_df)
---> 18 model.fit(data_train)
19 print(model.score(data_test))
20

/opt/anaconda/lib/python2.7/site-packages/patsylearn/patsy_adaptor.pyc in fit(self, data)
81 """
82 eval_env = EvalEnvironment.capture(self.eval_env, reference=1)
---> 83 formula = _drop_intercept(self.formula, self.add_intercept)
84 design_y, design_X = dmatrices(formula, data, eval_env=eval_env,
85 NA_action=self.NA_action)

/opt/anaconda/lib/python2.7/site-packages/patsylearn/patsy_adaptor.pyc in _drop_intercept(formula, add_intercept)
15 if not add_intercept:
16 if not isinstance(formula, ModelDesc):
---> 17 formula = ModelDesc.from_formula(formula)
18 if INTERCEPT in formula.rhs_termlist:
19 formula.rhs_termlist.remove(INTERCEPT)

TypeError: from_formula() takes exactly 3 arguments (2 given)

Support transforming Transformers into stateful transforms

basically we need some kind of mixin or wrapper that does:

class transformer_to_patsy:
    def __init__(self, transformer, use_partial_fit=False):
        self.fitted = False
        self.use_partial_fit = use_partial_fit
        self.transformer = transformer

    def memorize_chunk(self, X):
        if self.use_partial_fit:
            self.transformer.partial_fit(X)
        elif self.fitted:
            raise RuntimeError
        else:
            self.fitted = True
            self.transformer.fit(X)

    def memorize_finish(self):
        pass

    def transform(self, X):
        return self.transformer.transform()

Then you can define quantiletransform = transformer_to_patsy(QuantileTransformer()) and use quantiletransform(mycategories) in a formula.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.