stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
12.57k stars 4.03k forks source link

12_gradient_boosting_machines/06_evaluate_trading_signals.ipynb KeyError: 'No object named predictions/1/1134/63/7/500 in the file' #281

Closed dhsusf closed 1 year ago

dhsusf commented 1 year ago

I ran the 05_trading_signals_with_lightgbm_and_catboost.ipynb. Took something like 4 days, but not errors.

When running this cell in 06_evaluate_trading_signals: lookahead = 1 topn = 10 for best in range(topn): best_params = get_cb_params(catboost_daily_ic, t=lookahead, best=best) key = get_cb_key(lookahead, best_params) rounds = str(int(best_params.boost_rounds)) if best == 0: best_predictions = pd.read_hdf(results_path / 'tuning_catboost.h5', 'predictions/' + key) best_predictions = best_predictions[rounds].to_frame(best) else: best_predictions[best] = pd.read_hdf(results_path / 'tuning_catboost.h5', 'predictions/' + key)[rounds] best_predictions = best_predictions.sort_index()

I receive this error:


KeyError Traceback (most recent call last) /Volumes/PortableSSD/machine-learning-for-trading/12_gradient_boosting_machines/06_evaluate_trading_signals.ipynb Cell 110 in <cell line: 3>() 6 rounds = str(int(best_params.boost_rounds)) 7 if best == 0: ----> 8 best_predictions = pd.read_hdf(results_path / 'tuning_catboost.h5', 'predictions/' + key) 9 best_predictions = best_predictions[rounds].to_frame(best) 10 else:

File ~/opt/miniconda3/envs/ml4t/lib/python3.8/site-packages/pandas/io/pytables.py:425, in read_hdf(path_or_buf, key, mode, errors, where, start, stop, columns, iterator, chunksize, **kwargs) 420 raise ValueError( 421 "key must be provided when HDF5 " 422 "file contains multiple datasets." 423 ) 424 key = candidate_only_group._v_pathname --> 425 return store.select( 426 key, 427 where=where, 428 start=start, 429 stop=stop, 430 columns=columns, 431 iterator=iterator, 432 chunksize=chunksize, 433 auto_close=auto_close, 434 ) 435 except (ValueError, TypeError, KeyError): 436 if not isinstance(path_or_buf, HDFStore): 437 # if there is an error, close the store if we opened it.

File ~/opt/miniconda3/envs/ml4t/lib/python3.8/site-packages/pandas/io/pytables.py:820, in HDFStore.select(self, key, where, start, stop, columns, iterator, chunksize, auto_close) 818 group = self.get_node(key) 819 if group is None: --> 820 raise KeyError(f"No object named {key} in the file") 822 # create the storer and axes 823 where = _ensure_term(where, scope_level=1)

KeyError: 'No object named predictions/1/1134/63/7/500 in the file'

dhsusf commented 1 year ago

They key is being called incorrectly vs how it was built in the previous notebooks. I did this to the function and it seems to work.

def get_cb_key(t, p): key = f'{t}/{int(p.train_length)}/{int(p.test_length)}/' return key # + f'{int(p.max_depth)}/{int(p.min_child_samples)}'