Repository navigation
ENH: Allow predict to return a DataFrame - #2884
Conversation
|
This is my first pull request, so I apologize if I didn't follow the correct procedure. |
|
@bfcondon Thanks for working on this. These changes need to go into the Results instance predict and not the model.predict. The results predict method is for most models outside of tsa the same and is defined at the top level of our class hierarchy https://github.com/statsmodels/statsmodels/blob/master/statsmodels/base/model.py#L717 here is how I implemented pandas indexing for a new Note exog for prediction will in most cases be different from the exog and index in orig_data, and we need to get the index from the predict exog. #2151 It looks like the PR didn't add unit tests for a new exog that is a dataframe. A possible complication to do this on the top level is what the predict return could be 2 dimensional, and a |
|
Are there any tests you think I should add? |
|
I'll check in a few hours. I think there are some generic tests across models that we could reuse for this. |
|
There is a generic test for predict in statsmodels.base some of those are just smoke tests. Those generic tests don't include MNLogit because it doesn't have the same pattern. |
|
predict with exog=None is a bit unclear. It is uses in several places internally where we expect and ndarray. The pandas wrapped version is usually available as I'm not sure we want or need to return a pandas Series or DataFrame in that case. I don't see an easy solution to get both behaviors. |
|
josef-pkt, I looked at your code L132, and it only seems to match row_labels (index), not col_labels (columns). Why not test of exog is a DataFrame (or Series), and in case of DataFrame, return (self.param*exog).sum(axis=1)? I ran into this recently when I used an exog with columns in different order than in the original design matrix. One of the main selling points of pandas is automatic index/column alignment regardless of order, so I think statsmodels should not ignore either unless for a good reason. If you agree on this principle, I could help with coding. |
|
@kaiminglee Please open a separate issue for this? |
|
I think there is not much to share between prediction methods for different model types, so I don't see problems with doing this on a model-by-model basis (starting with better behavior for OLS,predict(), which hardcodes np.dot() anyway in the current version). I'll open a separate issue for this. |
|
@kaiminglee |
|
3 points: (1)
I just tested this on 0.6.1 and this seems wrong. (2) before (3) |
|
(1) (2) OLS.predict or generally model.predict is internal. The access for users goes through the results instance which is generic, inherited and has additional patsy handling if needed. That's the proper location for any changes, as I pointed out before. (3) I don't disagree with the usecase, we can use the labels, but not inside statsmodels. Some developers write their computation code with pandas, but not in statsmodels. Mixing pandas and numpy arrays requires a lot of "very careful coding", and that's too much pain for my taste. |
|
I added the tests and it is almost working, but one main test fails. It is caused by the hazard_regression predict method returning a bunch object as follows:
Is there a way to identify when this bunch object will be returned so I can exclude it? |
|
@bfcondon I was just looking at PHReg, and have not yet looked in details at your latest changes. I will open a new issue for this. I think this needs to be changed in PHReg so we have consistent code and definition of returns across models. |
…being returned by predict.
|
Tests are now passing. Let me know if you have any comments. |
| return self.model.predict(self.params, exog, *args, **kwargs) | ||
| predict_results = self.model.predict(self.params, exog, *args, **kwargs) | ||
|
|
||
| if (isinstance(orig_exog, pd.Series) or isinstance(orig_exog, pd.DataFrame) and |
There was a problem hiding this comment.
why did you switch to an instance check instead of just checking for an attribute index?
There was a problem hiding this comment.
When I checked for the index attribute, it did not uniquely identify the pandas object. I received the several errors similar to the following:
ERROR: statsmodels.base.tests.test_generic_methods.TestGenericGEEPoisson.test_predict_types
Traceback (most recent call last):
File "/home/brendan/anaconda3/envs/statsmodels-test/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/home/brendan/statsmodels/statsmodels/base/tests/test_generic_methods.py", line 152, in test_predict_types
assert_allclose(fitted, res.predict(np.squeeze(p_exog).tolist()),
File "/home/brendan/statsmodels/statsmodels/base/model.py", line 769, in predict
return pd.Series(predict_results, index=row_labels)
File "/home/brendan/anaconda3/envs/statsmodels-test/lib/python3.4/site-packages/pandas/core/series.py", line 151, in init
index = _ensure_index(index)
File "/home/brendan/anaconda3/envs/statsmodels-test/lib/python3.4/site-packages/pandas/core/index.py", line 3963, in _ensure_index
return Index(index_like)
File "/home/brendan/anaconda3/envs/statsmodels-test/lib/python3.4/site-packages/pandas/core/index.py", line 172, in new
subarr = com._asarray_tuplesafe(data, dtype=object)
File "/home/brendan/anaconda3/envs/statsmodels-test/lib/python3.4/site-packages/pandas/core/common.py", line 2170, in _asarray_tuplesafe
values = list(values)
TypeError: 'builtin_function_or_method' object is not iterable
|
Thanks, |
|
@josef-pkt I responded to your questions. Do you have any comments on the tests? |
|
@josef-pkt Did you have any other comments? I am happy to make further updates. |
|
@bfcondon |
|
Looks good. I think we can merge it as it is. |
|
I think github allows you to do that automatically now: |
|
Ok, merged with squash through github, but it doesn't create a merge commit, so we won't use this. we only want to have One advantage is that the original PR branch doesn't need to be rebased, which keeps the original commit-comment sequence. (i.e. similar to commandline merge, but that is extra work, that we mostly avoid.) |
|
Thanks. For future reference, what is the right way to flatten a bunch of commits on a branch? |
|
@bfcondon Thank you for your first contribution.
I usually use interactive rebase to squash or make other changes to the commits, and then force push or push to a new branch. If there are no merge conflicts, then it's automatic after editing the commit text file. |
Modified the RegressionModel predict method to return a pandas DataFrame if the original model data was in the form of a DataFrame.
Fixes #1352