Git Product home page Git Product logo

Comments (5)

matthewwardrop avatar matthewwardrop commented on July 19, 2024 1

Hi @klaapbakken ! This functionality is indeed supported via the ModelSpec class. You can access the ModelSpec instance associated with a model matrix generated by get_model_matrix, e.g.:

from formulaic import model_matrix

df = ....
mm  = model_matrix('y ~ x', df)
ms = mm.model_spec

You can then use that model spec to generate a new model matrix from new data using:

mm2 = ms.get_model_matrix(df2)

Hope that helps! In lieu of better documentation (still coming!), you can view the ModelSpec class here: https://github.com/matthewwardrop/formulaic/blob/master/formulaic/model_spec.py

from formulaic.

matthewwardrop avatar matthewwardrop commented on July 19, 2024

I'll leave this open until this has been properly integrated into the docs.

from formulaic.

fabio-rosa-ors avatar fabio-rosa-ors commented on July 19, 2024

Hi Matthew. Great work !
I'm using the suggested approach to save/restore the state of ModelSpec within our ML framework with success, so I would suggest just a small sugar addition to catch the context when using the recovered ModelSpec: in my work I found very useful to include this utility under sugar.py

def get_model_matrix(model_spec, data, *, context=0, **kwargs):
    if isinstance(context, int):
        if hasattr(sys, "_getframe"):
            frame = sys._getframe(context + 1)
            context = LayeredMapping(frame.f_locals, frame.f_globals)
        else:
            context = None  # pragma: no cover
    return model_spec.get_model_matrix(data, context=context, **kwargs)

the body is the same as model_matrix, but with different Materializer introduced by the model_spec param.

In the previous example ms2 could be used like here:
mm2 = formulaic.get_model_matrix(mm2, df2)
with the effect of catching custom transformers referred by the formula

from formulaic.

matthewwardrop avatar matthewwardrop commented on July 19, 2024

Excellent idea @fabio-rosa-ors ! I've incorporated this into the model_matrix sugar method, so now you can just do:

model_matrix(<old model matrix>, <new data>)

Will be in Formulaic 0.3

from formulaic.

fabio-rosa-ors avatar fabio-rosa-ors commented on July 19, 2024

Great !

from formulaic.

Related Issues (20)

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.