Skip to content

predict accepts a DataFrame but returns a numpy array (rather than Series) #1352

Closed

Description

@cancan101

This loses any Index information on the input DataFrame.

This would not be an issue except for the fact that the shape of the input DataFrame does not match the returned numpy array which makes combining the two tricky.

This is because some of the data is dropped when performing the transform:

exog = dmatrix(self.model.data.orig_exog.design_info.builder, exog)

This actually seems to be a rather simple fix.

dmatrix can be told to return a "dataframe" and then that DataFrame can be dot'ed with the params.

Activity

  1. josef-pkt commented on Feb 4, 2014

    @josef-pkt
    Member

    Do you have an example where the length of the returned prediction is different from the number of rows in the DataFrame?
    I thought this should always be the same.
    Is it the case if there are missing values in your predict exog? Or, is there a transform that changes the number of observations?

  2. cancan101 commented on Feb 4, 2014

    @cancan101
    Author

    The default argument for dmatrix is NA_action='drop'.

    I have a transform that can produce NaNs.

  3. cancan101 commented on Feb 4, 2014

    @cancan101
    Author

    Example:

    s = pd.DataFrame({'s':pd.Series(range(10))})
    
    def shifter(x):
        return x.shift()
    
    model = "s ~ shifter(s)"
    fit = ols(model, data=s).fit()
    
    In [18]: s.shape
    Out[18]: (10,)
    
    In [17]: fit.predict(s).shape
    Out[17]: (9,)
    
  4. cancan101 commented on Mar 6, 2014

    @cancan101
    Author

    Here is a more fleshed out example of how this can be improved:

    import pandas as pd
    from statsmodels.formula.api import ols
    from patsy import dmatrix
    
    s = pd.Series(range(10),index=pd.date_range("2013-1-1", periods=10)).to_frame()
    
    def shifter(x):
        return x.shift()
    
    model = "s ~ shifter(s)"
    
    fit = ols(model, data=s).fit()
    

    Current prediction:

    In [19]:
    fit.predict(s)
    
    Out[19]:
    array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
    

    New prediction:

    In [21]:
    def better_predict(fit, exog):
        dm = dmatrix(fit.model.data.orig_exog.design_info.builder, exog, return_type="dataframe", )
        return dm.dot(fit.params)
    
    In [22]:
    better_predict(fit, s)
    
    Out[22]:
    2013-01-02    1
    2013-01-03    2
    2013-01-04    3
    2013-01-05    4
    2013-01-06    5
    2013-01-07    6
    2013-01-08    7
    2013-01-09    8
    2013-01-10    9
    dtype: float64
    
  5. jseabold commented on Mar 6, 2014

    @jseabold
    Member

    Hmm, yes, I quite like this approach. It won't work for predict and forecast in TSA, but that should be overwritten already anyway.

  6. jseabold commented on Mar 6, 2014

    @jseabold
    Member

    I'll have to look at the speed penalty vs. pure arrays for bootstrapping, etc. but I doubt it's that substantial.

  7. josef-pkt commented on Mar 6, 2014

    @josef-pkt
    Member

    We need to avoid any non-numpy overhead for bootstrapping and cross-validation.

  8. jseabold commented on Mar 6, 2014

    @jseabold
    Member

    While I agree, my new rule is we need timings before discussing any timings. It's a concern, but I don't know how much yet. We're going to be able to remove some checks that are in there already that patsy would also do (more robustly), so the overhead might really be negligible. What's negligible for bootstrap? We decide when we have timings I guess.

    A few ways forward that come to mind.

    1. This magic goes in the Results.predict. Bootstrap relies on Model.predict, which Results.predict also calls. Everyone wins.
    2. We provide the predict somehow in the wrapper classes and bootstrap relies on Results._results.predict.

    I'm leaning towards 1. Seems simple and straight-forward.

  9. josef-pkt commented on Mar 6, 2014

    @josef-pkt
    Member

    I think we just need a simple type check; if types(exog) is np.array or something like that.
    also what happens if exog is None and we calculate the fittedvalues, which are separately wrapped AFAIR, and should be arrays in _results.

    Timing: a simple LOO crossval would do it.

  10. orome commented on Mar 22, 2014

    @orome

    I encountered this recently and ended up very confused about what was happening. I'm glad to see it's not just me. Including pandas index information in the results from predict (at least optionally), or preserving length (to match the length of predicts argument) would make predict much easier to use.

  11. josef-pkt commented on Jan 11, 2015

    @josef-pkt
    Member

    I opened a SUMMary issue #2150 and started to look into prediction methods.
    I'd like a general solution instead of patching code in various places.

  12. josef-pkt commented on Jan 12, 2015

    @josef-pkt
    Member

    see also #2145 as a more general duplicate

  13. added this to the 0.7 milestone on Jan 14, 2015
  14. jseabold commented on Jan 14, 2015

    @jseabold
    Member

    Putting this on my radar for 0.7. That's annoying.

  15. josef-pkt commented on Jan 14, 2015

    @josef-pkt
    Member

    I started part of pandas handling in my branch for prediction intervals.
    I want to remove all data/exog handling from predict and outsource it, which is another issue

    We need design decision and helper functions, Kerby has also PRs for prediction.

  16. jseabold commented on Jan 14, 2015

    @jseabold
    Member

    Ok, we have general wrapping code that I wrote for MixedLM, so it should be simple to just fix this issue without getting too far afield.

  17. saroele commented on Jan 29, 2015

    @saroele

    +1

  18. malcolm415-zz commented on Jun 4, 2015

    @malcolm415-zz

    +1

  19. modified the milestones: 0.8, 0.7 on Jul 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions