import akshare as ak import numpy as np import pandas as pd import random import matplotlib.pyplot as plt class StockTradingEnv: def __init__(self): self.df = ak.stock_zh_a_daily(symbol='sh000001', adjust="qfq").iloc[::-1] self.observation_space = self.df.shape[1] self.action_space = 3 self.reset() def reset(self): self.current_step = 0 self.total_profit = 0 self.done = False self.state = self.df.iloc[self.current_step].values return self.state def step(self, action): assert self.action_space.contains(action) if action == 0: # 买入 self.buy_stock() elif action == 1: # 卖出 self.sell_stock() else: # 保持不变 pass self.current_step += 1 if self.current_step >= len(self.df) - 1: self.done = True else: self.state = self.df.iloc[self.current_step].values reward = self.get_reward() self.total_profit += reward return self.state, reward, self.done, {} def buy_stock(self): pass def sell_stock(self): pass def get_reward(self): pass class QLearningAgent: def __init__(self, state_size, action_size): self.state_size = state_size self.action_size = action_size self.epsilon = 1.0 self.epsilon_min = 0.01 self.epsilon_decay = 0.995 self.learning_rate = 0.1 self.discount_factor = 0.99 self.q_table = np.zeros((self.state_size, self.action_size)) def act(self, state): if np.random.rand() <= self.epsilon: return random.randrange(self.action_size) else: return np.argmax(self.q_table[state, :]) def learn(self, state, action, reward, next_state, done): target = reward + self.discount_factor * np.max(self.q_table[next_state, :]) self.q_table[state, action] = (1 - self.learning_rate) * self.q_table[state, action] + self.learning_rate * target if self.epsilon > self.epsilon_min: self.epsilon *= self.epsilon_decay env = StockTradingEnv() agent = QLearningAgent(env.observation_space, env.action_space) for episode in range(1000): state = env.reset() done = False while not done: action = agent.act(state) next_state, reward, done, _ = env.step(action) agent.learn(state, action, reward, next_state, done) state = next_state if episode % 10 == 0: print("Episode: %d, Total Profit: %f" % (episode, env.total_profit)) agent.save_model("model-%d.h5" % episode) def plot_profit(env, title): plt.figure(figsize=(12, 6)) plt.plot(env.df.index, env.df.close, label="Price") plt.plot(env.df.index, env.profits, label="Profits") plt.legend() plt.title(title) plt.show() env = StockTradingEnv() agent = QLearningAgent(env.observation_space, env.action_space) agent.load_model("model-100.h5") state = env.reset() done = False while not done: action = agent.act(state) next_state, reward, done, _ = env.step(action) state = next_state plot_profit(env, "QLearning Trading Strategy")优化代码
时间: 2024-02-15 16:27:29 浏览: 168
1. 对于环境类 `StockTradingEnv`,可以考虑将 `buy_stock` 和 `sell_stock` 方法的具体实现写入 `step` 方法中,避免方法数量过多。
2. 可以将 `get_reward` 方法中的具体实现改为直接计算当前持仓的收益。
3. 在循环训练过程中,可以记录每个 episode 的总收益,并将这些数据保存下来,在训练完成后进行可视化分析。
4. 可以添加更多的参数来控制训练过程,比如学习率、衰减系数等。
5. 可以将 QLearningAgent 类中的方法进行整理和封装,提高代码的可读性和可维护性。同时,也可以添加一些对模型进行保存和加载的方法,便于模型的重用和共享。
相关问题
import numpy as np import pandas as pd import matplotlib import matplotlib.pyplot as plt import seaborn as sns import chardet
### 正确导入Python数据分析和可视化库的方法
为了进行高效的数据分析与可视化,在Python环境中正确安装并导入必要的库至关重要。以下是关于`numpy`, `pandas`, `matplotlib`, `seaborn` 和 `chardet` 的具体导入方法:
#### 导入库
在开始任何项目之前,确保已经安装了所需的软件包。如果尚未安装这些库,可以使用pip命令来完成安装。
```bash
pip install numpy pandas matplotlib seaborn chardet
```
一旦确认所需库已成功安装,则可以在脚本顶部按照如下方式依次引入各个模块:
```python
import numpy as np # 提供多维数组对象以及派生对象(如掩码数组、矩阵),并且支持大量的函数操作。
import pandas as pd # 数据处理和分析的核心工具之一;提供了DataFrame结构用于存储表格型数据集。
import matplotlib.pyplot as plt # Python中最流行的绘图库之一,能够创建静态、动态交互式的图表。
import seaborn as sns # 基于Matplotlib之上构建而成的统计图形库,简化了许多常见的统计数据可视化的实现过程。
import chardet # 自动检测字符编码类型的实用程序,对于读取未知编码格式文件非常有用。
```
设置字体以便正常显示中文标签和其他特殊符号也是重要的一步。可以通过修改`matplotlib`的相关参数来进行配置:
```python
plt.rcParams['font.sans-serif'] = 'SimHei' # 设置默认字体为黑体以支持中文显示
plt.rcParams['axes.unicode_minus'] = False # 解决负号无法正确显示的问题
```
上述代码片段展示了如何准备环境,使得后续的数据处理工作更加顺畅[^1]。
import pandas as pd import openpyxl # import matplotlib.pyplot as plt import numpy as np from sklearn.ensemble import AdaBoostClassifier from sklearn.model_selection import train_test_split # 打开Excel文件 wb = openpyxl.load_workbook('./处理过的训练集/987027.xlsx')
这段代码是用于导入所需的Python库,并打开一个Excel文件的操作。其中,pandas库用于数据的处理和分析,openpyxl库用于读写Excel文件,numpy库用于数值计算,sklearn库用于机器学习算法的实现。对于打开Excel文件的操作,需要指定文件路径和文件名,本例中文件路径为'./处理过的训练集/987027.xlsx',表示该文件在当前目录下的'处理过的训练集'文件夹中,文件名为'987027.xlsx'。
阅读全文
相关推荐
















