AI4Finance-Foundation / FinRL

FinRL: Financial Reinforcement Learning. 🔥
https://ai4finance.org
MIT License
9.4k stars 2.29k forks source link

ValueError: If using all scalar values, you must pass an index #1010

Closed nPokemon closed 1 year ago

nPokemon commented 1 year ago

ValueError Traceback (most recent call last)

in () 3 4 # Run ensemble strategy and store the result ----> 5 result = ensemble_agent.run_ensemble_strategy(A2C_model_kwargs, 6 PPO_model_kwargs, 7 DDPG_model_kwargs, 5 frames /usr/local/lib/python3.10/site-packages/pandas/core/internals/construction.py in _extract_index(data) 643 644 if not indexes and not raw_lengths: --> 645 raise ValueError("If using all scalar values, you must pass an index") 646 647 if have_series: ValueError: If using all scalar values, you must pass an index ----------------------------------- I have try many way to fix it, but no luck, any one have any idea ?
Sotokama commented 1 year ago

Also facing the same error

Omar-ALkhateeb commented 1 year ago

same issue

rendezvoush commented 1 year ago

Yeah, the same issue after "pip install stable-baselines3==2.0.0a5"

======DDPG Validation from: 2021-10-04 to 2022-01-03 ======Best Model Retraining from: 2010-01-01 to 2022-01-03 ======Trading from: 2022-01-03 to 2022-04-04

Then the following error raised:


ValueError Traceback (most recent call last) d:\Projects\Python\DQN\FinRL_Ensemble_StockTrading_ICAIF_2020.ipynb Cell 36 in () ----> 1 df_summary = ensemble_agent.run_ensemble_strategy(A2C_model_kwargs, 2 PPO_model_kwargs, 3 DDPG_model_kwargs, 4 timesteps_dict)

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\finrl\agents\stablebaselines3\models.py:668, in DRLEnsembleAgent.run_ensemble_strategy(self, A2C_model_kwargs, PPO_model_kwargs, DDPG_model_kwargs, timesteps_dict) 661 print( 662 "======Trading from: ", 663 self.unique_trade_date[i - self.rebalance_window], 664 "to ", 665 self.unique_trade_date[i], 666 ) 667 # print("Used Model: ", model_ensemble) --> 668 last_state_ensemble = self.DRL_prediction( 669 model=model_ensemble, 670 name="ensemble", 671 last_state=last_state_ensemble, 672 iter_num=i, 673 turbulence_threshold=turbulence_threshold, 674 initial=initial, 675 ) 676 ############## Trading ends ############## 678 end = time.time()

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\finrl\agents\stablebaselines3\models.py:321, in DRLEnsembleAgent.DRL_prediction(self, model, name, last_state, iter_num, turbulence_threshold, initial) 317 if i == (len(trade_data.index.unique()) - 2): 318 # print(env_test.render()) 319 last_state = trade_env.render() --> 321 df_last_state = pd.DataFrame({"last_state": last_state}) 322 df_last_state.to_csv(f"results/laststate{name}_{i}.csv", index=False) 323 return last_state

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\pandas\core\frame.py:636, in DataFrame.init(self, data, index, columns, dtype, copy) 630 mgr = self._init_mgr( 631 data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy 632 ) 634 elif isinstance(data, dict): 635 # GH#38939 de facto copy defaults to False only in non-dict cases --> 636 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager) 637 elif isinstance(data, ma.MaskedArray): 638 import numpy.ma.mrecords as mrecords

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\pandas\core\internals\construction.py:502, in dict_to_mgr(data, index, columns, dtype, typ, copy) 494 arrays = [ 495 x 496 if not hasattr(x, "dtype") or not isinstance(x.dtype, ExtensionDtype) 497 else x.copy() 498 for x in arrays 499 ] 500 # TODO: can we get rid of the dt64tz special case above? --> 502 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\pandas\core\internals\construction.py:120, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate) 117 if verify_integrity: 118 # figure out the index, if necessary 119 if index is None: --> 120 index = _extract_index(arrays) 121 else: 122 index = ensure_index(index)

File d:\ProgramData\Anaconda3\envs\develop\lib\site-packages\pandas\core\internals\construction.py:664, in _extract_index(data) 661 raise ValueError("Per-column arrays must each be 1-dimensional") 663 if not indexes and not raw_lengths: --> 664 raise ValueError("If using all scalar values, you must pass an index") 666 elif have_series: 667 index = union_indexes(indexes)

ValueError: If using all scalar values, you must pass an index

Aashish-1008 commented 1 year ago

Yeah, the same issue after "pip install stable-baselines3==2.0.0a5"

======DDPG Validation from: 2021-10-04 to 2022-01-03 ======Best Model Retraining from: 2010-01-01 to 2022-01-03 ======Trading from: 2022-01-03 to 2022-04-04

Then the following error raised:

kilmern commented 1 year ago

Likewise. Same problem after the suggested fix in #1022

AkashKarnatak commented 1 year ago

This commit by stable-baseline3 is the cause of above error. Changing line 319 of finrl/agents/stablebaselines3/models.py from,

last_state = trade_env.render()

to

last_state = trade_env.envs[0].render()

fixes the error.

MariamaB commented 1 year ago

ValueError: operands could not be broadcast together with shapes (30,) (29,)

Facing the following error when running:

df_summary = ensemble_agent.run_ensemble_strategy(A2C_model_kwargs, PPO_model_kwargs, DDPG_model_kwargs, timesteps_dict)

Does anyone know how to fix it?

==================================================================================

Full Error:

ValueError Traceback (most recent call last) in <cell line: 1>() ----> 1 df_summary = ensemble_agent.run_ensemble_strategy(A2C_model_kwargs, 2 PPO_model_kwargs, 3 DDPG_model_kwargs, 4 timesteps_dict)

5 frames /usr/local/lib/python3.10/dist-packages/finrl/agents/stablebaselines3/models.py in run_ensemble_strategy(self, A2C_model_kwargs, PPO_model_kwargs, DDPG_model_kwargs, timesteps_dict) 424 ], 425 ) --> 426 self.train_env = DummyVecEnv( 427 [ 428 lambda: StockTradingEnv(

/usr/local/lib/python3.10/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in init(self, env_fns) 28 29 def init(self, env_fns: List[Callable[[], gym.Env]]): ---> 30 self.envs = [_patch_env(fn()) for fn in env_fns] 31 if len(set([id(env.unwrapped) for env in self.envs])) != len(self.envs): 32 raise ValueError(

/usr/local/lib/python3.10/dist-packages/stable_baselines3/common/vec_env/dummy_vec_env.py in (.0) 28 29 def init(self, env_fns: List[Callable[[], gym.Env]]): ---> 30 self.envs = [_patch_env(fn()) for fn in env_fns] 31 if len(set([id(env.unwrapped) for env in self.envs])) != len(self.envs): 32 raise ValueError(

/usr/local/lib/python3.10/dist-packages/finrl/agents/stablebaselines3/models.py in () 426 self.train_env = DummyVecEnv( 427 [ --> 428 lambda: StockTradingEnv( 429 df=train, 430 stock_dim=self.stock_dim,

/usr/local/lib/python3.10/dist-packages/finrl/meta/env_stock_trading/env_stocktrading.py in init(self, df, stock_dim, hmax, initial_amount, num_stock_shares, buy_cost_pct, sell_cost_pct, reward_scaling, state_space, action_space, tech_indicator_list, turbulence_threshold, risk_indicator_col, make_plots, print_verbosity, day, initial, previous_state, model_name, mode, iteration) 74 self.iteration = iteration 75 # initalize state ---> 76 self.state = self._initiate_state() 77 78 # initialize reward

/usr/local/lib/python3.10/dist-packages/finrl/meta/env_stock_trading/env_stocktrading.py in _initiate_state(self) 401 # for multiple stock 402 state = ( --> 403 [self.initial_amount] 404 + self.data.close.values.tolist() 405 + self.num_stock_shares

ValueError: operands could not be broadcast together with shapes (30,) (29,)