-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The feature names should match those that were passed during fit #116
Comments
Thank you for reporting @ll550! Is this the case with using a custom predict_fn? |
@edtechre No, I just simply did a Little extension on the example. |
Thanks. Not sure if I understand completely. Can you provide some example
code where you see the issue?
…On Tue, Apr 16, 2024, 2:37 PM ll550 ***@***.***> wrote:
@edtechre <https://github.com/edtechre> No, I just simply did a Little
extension on the example.
—
Reply to this email directly, view it on GitHub
<#116 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACQHWPFMEFHT7OX66AG7ULY5WKYBAVCNFSM6AAAAABGJREDYSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJZHE3DCNBZGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@edtechre Please notice the order of [macd macds macdh]
If I put the same order of macd's indicators as in pb.model, the ctx.preds in the following function will issue the error. But the training of linear regression is ok.
Hope it will help. |
I wasn't able to repro the error using the code below. Also note that there is no way the framework would be able to fix this, since the ordering would be dependent on the ordering used to access the columns in the
|
For those who are interested in this issue, I pasted the error version and working version for comparsion. Thanks for @edtechre code. I believe he is correct. If you want to input more features, the ordering would be different from this case. You need to capture the correct order in ctx.preds (the order is from sklearn in an alphabetical form), and change them accordingly in the train_slr function. This is my erroneous version:
**--------------------------------------------------------------------------- File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\strategy.py:1083, in Strategy.backtest(self, start_date, end_date, timeframe, between_time, days, lookahead, train_size, shuffle, calc_bootstrap, disable_parallel, warmup, portfolio) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\strategy.py:1239, in Strategy.walkforward(self, windows, lookahead, start_date, end_date, timeframe, between_time, days, train_size, shuffle, calc_bootstrap, disable_parallel, warmup, portfolio) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\strategy.py:1349, in Strategy._run_walkforward(self, portfolio, df, indicator_data, tf_seconds, between_time, days, windows, lookahead, train_size, shuffle, train_only, warmup) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\strategy.py:312, in BacktestMixin.backtest_executions(self, config, executions, before_exec_fn, after_exec_fn, sessions, models, indicator_data, test_data, portfolio, pos_size_handler, exit_dates, train_only, slippage_model, enable_fractional_shares, round_fill_price, warmup) Cell In[219], line 55, in hold_long(ctx) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\context.py:937, in ExecContext.preds(self, model_name, symbol) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\context.py:325, in BaseContext.preds(self, model_name, symbol) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\pybroker\scope.py:495, in PredictionScope.fetch(self, symbol, name, end_index) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\sklearn\linear_model_base.py:286, in LinearModel.predict(self, X) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\sklearn\linear_model_base.py:269, in LinearModel._decision_function(self, X) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\sklearn\base.py:608, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, cast_to_ndarray, **check_params) File D:\ProgramData\miniconda3\envs\stock2\Lib\site-packages\sklearn\base.py:535, in BaseEstimator._check_feature_names(self, X, reset) ValueError: The feature names should match those that were passed during fit. This is my working version:
|
the functions in ctx.preds swap the feature names in the tuple,e.g. (macd macds macdh)->(macd macdh macds). It leads to the problem described in the title.
My solution is simple. Just reorganize the feature names as ctx.preds did in train_slr function.
The problem is due to sklearn doing some validations between fit and prediction. I tried to switch off such validations, but failed. Hope it could be useful for someone else.
The text was updated successfully, but these errors were encountered: