当前位置:   article > 正文

使用chatGPT完成股票价格预测模型_如何用chatgpt写预测模型

如何用chatgpt写预测模型

序言

笔者对国内股票的程序API不甚了解,所以使用国外的股票平台Finazon,请读者自行参考。

在本文中,我使用Finazon获取我们想预测的股票的价格时间序列数据,然后,我们将这些数据输入到 ChatGPT 生成的程序中。包括数据准备、模型构建、预测、模型评估在内的所有流程都将由 ChatGPT 负责

项目摘要

通过询问chatGPT关于股价预测的模型和方法论之后,获得了以下内容

1. 获取数据
2. 线性回归
3. 基本 LSTM 模型
4. 改进 LSTM 模型
5. 超参数调优

整篇文章将遵循这个大纲,除了第一部分获取数据之外,所有其他部分将完全由 ChatGPT 单独驱动。

1. 使用 Finazon 获取数据

为了建立一个能够以更高的准确度进行预测的成功的机器学习模型,质量和可靠性的数据必须优先于其他任何因素。这就是Finazon发挥作用的地方。Finazon 是一个各种性质数据的市场,这些数据直接从 SIP、Coinbase、Binance 等授权来源提取。

对于这个项目,我们将预测苹果的股价,为此我们需要其长期时间序列数据。因此,我选择了SIP提供的Finazon美国股市数据,订阅数据后,我使用以下代码提取了Apple过去4000天(大约10-11年)的历史数据:

  1. import pandas as pd
  2. import requests
  3. from datetime import datetime as dt
  4. api_key = 'YOUR API KEY'
  5. page_no = [0,1,2,3,4]
  6. data_dict = []
  7. for i in page_no:
  8. hist_json = requests.get(f'https://api.finazon.io/latest/time_series?publisher=sip&ticker=AAPL&interval=1d&page={i}&page_size=1000&order=desc&apikey={api_key}').json()['data']
  9. data_dict.append(hist_json)
  10. aapl_df = pd.DataFrame(columns = ['t', 'o', 'h', 'l', 'c', 'v'])
  11. for i in range(0,len(data_dict)):
  12. df = pd.DataFrame(data_dict[i])
  13. aapl_df = aapl_df.append(df, ignore_index = True)
  14. aapl_df = aapl_df[::-1].reset_index(drop = True)
  15. aapl_df.columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
  16. aapl_df.Date = pd.to_datetime(aapl_df.Date.astype(int), unit = 's').dt.date
  17. aapl_df.tail()

这段代码没有什么复杂的地方。为了代码的顺利运行,请确保替换YOUR API KEY为你的 Finazon API 密钥,您可以在创建账号后获取该密钥。最终的数据框

2. 线性回归

我不想直接开始构建 LSTM 模型,而是想慢慢地使用 ChatGPT 来了解机器人给出的代码生成的结果。我要求它为我编写一个 Python 程序,使用 Scikit Learn 构建基本的线性回归模型,以预测股票价格。它最初为我提供了构建回归模型应遵循的步骤,然后编写了以下示例代码:

  1. import pandas as pd
  2. import numpy as np
  3. from sklearn.linear_model import LinearRegression
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.preprocessing import StandardScaler
  6. from sklearn.metrics import r2_score, mean_squared_error
  7. import matplotlib.pyplot as plt
  8. # Get the stock data
  9. df = pd.read_csv('aapl.csv').dropna()
  10. df = df.set_index('Date')
  11. # Add a column for the target variable
  12. df['Price'] = df['Close']
  13. # Define the features and target variable
  14. features = ['Open', 'High', 'Low', 'Volume']
  15. target = 'Price'
  16. # Split the data into training and testing sets
  17. train_size = 0.8
  18. train_data = df[:int(train_size * len(df))]
  19. test_data = df[int(train_size * len(df)):]
  20. # Normalize the data
  21. scaler = StandardScaler()
  22. train_data[features] = scaler.fit_transform(train_data[features])
  23. test_data[features] = scaler.transform(test_data[features])
  24. # Train the model
  25. model = LinearRegression()
  26. model.fit(train_data[features], train_data[target])
  27. # Make predictions on the test data
  28. predictions = model.predict(test_data[features])
  29. # Compute the R^2 score and RMSE
  30. r2 = r2_score(test_data[target], predictions)
  31. rmse = np.sqrt(mean_squared_error(test_data[target], predictions))
  32. print(f'R^2 score: {r2:.4f}')
  33. print(f'RMSE: {rmse:.4f}')
  34. # Plot the predicted and actual values
  35. plt.plot(test_data[target].values, label='Actual')
  36. plt.plot(predictions, label='Predicted')
  37. plt.legend()
  38. plt.show()

代码的输出结果:

3.  基本的 LSTM 模型

让 ChatGPT 生成一个 Python 程序来构建 LSTM 模型来预测股票价格了。先简单介绍一下 LSTM:长短期记忆 (LSTM) 是一种复杂的神经网络,也是分析顺序数据的强大工具。它有选择地存储或忘记信息以理解复杂的数据序列。这使得它非常适合语音识别、自然语言处理和分析时间序列数据等任务。以下是 ChatGPT 生成的用于构建预测 LSTM 模型的代码:

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from sklearn.metrics import *
  5. from sklearn.preprocessing import MinMaxScaler
  6. from tensorflow.keras.models import Sequential
  7. from tensorflow.keras.layers import LSTM, Dense, Dropout
  8. # Load the stock price data
  9. df = pd.read_csv('aapl.csv').dropna()
  10. df = df.set_index('Date')
  11. # Preprocess the data
  12. scaler = MinMaxScaler()
  13. df_scaled = scaler.fit_transform(df['Close'].values.reshape(-1, 1))
  14. # Define the training and testing data
  15. train_size = int(len(df_scaled) * 0.8)
  16. train_data = df_scaled[:train_size, :]
  17. test_data = df_scaled[train_size:, :]
  18. # Define the function to create a sliding window dataset
  19. def create_dataset(dataset, time_steps=1):
  20. X_data, y_data = [], []
  21. for i in range(len(dataset)-time_steps-1):
  22. X_data.append(dataset[i:(i+time_steps), 0])
  23. y_data.append(dataset[i + time_steps, 0])
  24. return np.array(X_data), np.array(y_data)
  25. # Define the time steps and create the sliding window dataset
  26. time_steps = 60
  27. X_train, y_train = create_dataset(train_data, time_steps)
  28. X_test, y_test = create_dataset(test_data, time_steps)
  29. # Reshape the data for LSTM input
  30. X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
  31. X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
  32. # Define the LSTM model architecture
  33. model = Sequential()
  34. model.add(LSTM(units=64, return_sequences=True, input_shape=(X_train.shape[1], 1)))
  35. model.add(Dropout(0.2))
  36. model.add(LSTM(units=64, return_sequences=True))
  37. model.add(Dropout(0.2))
  38. model.add(LSTM(units=64, return_sequences=False))
  39. model.add(Dropout(0.2))
  40. model.add(Dense(units=1))
  41. # Compile the model
  42. model.compile(optimizer='adam', loss='mean_squared_error')
  43. # Train the model
  44. model.fit(X_train, y_train, epochs=50, batch_size=64, validation_data=(X_test, y_test), verbose=1)
  45. # Make predictions
  46. y_pred = model.predict(X_test)
  47. # Inverse transform the predicted and actual values
  48. y_pred = scaler.inverse_transform(y_pred)
  49. y_test = y_test.reshape(y_pred.shape[0], 1)
  50. y_test = scaler.inverse_transform(y_test)
  51. # Evaluate the model
  52. mse = mean_squared_error(y_test, y_pred)
  53. msle = mean_squared_log_error(y_test, y_pred)
  54. mae = mean_absolute_error(y_test, y_pred)
  55. r2 = r2_score(y_test, y_pred)
  56. print('MSE: ', mse)
  57. print('MSLE: ', msle)
  58. print('MAE: ', mae)
  59. print('R-squared: ', r2)
  60. # Plot the predicted vs actual values
  61. plt.figure(figsize=(10, 6))
  62. plt.style.use('fivethirtyeight')
  63. plt.plot(y_test, label='Actual', linewidth = 3, alpha = 0.4)
  64. plt.plot(y_pred, label='Predicted', linewidth = 1.5)
  65. plt.xlabel('Days')
  66. plt.ylabel('Stock Price')
  67. plt.title('AAPL Stock Price Prediction')
  68. plt.legend()
  69. plt.show()

输出结果如下:

4.改进LSTM模型

我刚刚构建并用于预测的 LSTM 模型本质上非常简单,并且可以在各个方面进行改进。所以我要求 ChatGPT 给我一些改进代码以获得更好的 LSTM 模型的建议,它是这样说的:

  1. 增加 LSTM 层数:提高模型性能的一种方法是向模型添加更多 LSTM 层。这可以帮助模型捕获数据中更复杂的模式。
  2. 增加每层神经元数量:提高模型性能的另一种方法是增加每层 LSTM 神经元数量。这可以帮助模型学习输入特征和目标变量之间更复杂的关系。
  3. 调整学习率:学习率决定了模型根据预测值和实际值之间的误差调整权重的程度。较小的学习率会减慢训练过程,而较大的学习率会导致模型超出最佳权重。调整学习率可​​以提高模型的性能。
  4. 添加 dropout 层:可以将 Dropout 层添加到模型中以防止过度拟合。该技术在训练过程中随机丢弃一些神经元,这可以帮助模型更好地泛化到新数据。
  5. 尝试不同的激活函数:激活函数的选择会影响模型学习数据中复杂关系的能力。尝试不同的激活函数(例如 tanh、relu 和 sigmoid)有助于提高模型的性能。
  6. 使用更多数据:增加用于训练模型的数据量可以提高其性能。这可以通过增加历史数据的年数或使用新闻情绪数据或技术指标等附加功能来实现。
  7. 使用不同的损失函数:损失函数的选择也会影响模型的性能。使用更适合时间序列数据的损失函数,例如均方对数误差 (MSLE),可以帮助提高模型的准确性。

我要求 ChatGPT 在之前的代码中包含所有这些建议,它返回给我这个更新版本:

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from sklearn.metrics import *
  5. from sklearn.preprocessing import MinMaxScaler
  6. from tensorflow.keras.models import Sequential
  7. from tensorflow.keras.layers import LSTM, Dense, Dropout
  8. from tensorflow.keras.callbacks import EarlyStopping
  9. # Obtain stock data for Apple
  10. df = pd.read_csv('aapl.csv').dropna()
  11. # Extract the closing prices
  12. close_prices = df[["Date", "Close"]].copy()
  13. close_prices.columns = ["ds", "y"]
  14. close_prices.set_index("ds", inplace=True)
  15. # Normalize the data
  16. scaler = MinMaxScaler(feature_range=(0, 1))
  17. close_prices_scaled = scaler.fit_transform(close_prices)
  18. # Create training and testing data
  19. train_size = int(len(close_prices) * 0.8)
  20. train_data = close_prices_scaled[:train_size]
  21. test_data = close_prices_scaled[train_size:]
  22. # Define the number of time steps to look back
  23. look_back = 60
  24. # Create training and testing data for the LSTM model
  25. def create_data(data):
  26. X, Y = [], []
  27. for i in range(look_back, len(data)):
  28. X.append(data[i - look_back : i, 0])
  29. Y.append(data[i, 0])
  30. X, Y = np.array(X), np.array(Y)
  31. X = np.reshape(X, (X.shape[0], X.shape[1], 1))
  32. return X, Y
  33. X_train, Y_train = create_data(train_data)
  34. X_test, Y_test = create_data(test_data)
  35. # Define the LSTM model
  36. model = Sequential()
  37. model.add(LSTM(units=64, return_sequences=True, input_shape=(X_train.shape[1], 1)))
  38. model.add(Dropout(0.2))
  39. model.add(LSTM(units=128, return_sequences=True))
  40. model.add(Dropout(0.2))
  41. model.add(LSTM(units=64))
  42. model.add(Dropout(0.2))
  43. model.add(Dense(units=1))
  44. # Compile the model
  45. model.compile(optimizer="adam", loss="mean_squared_logarithmic_error")
  46. # Define the early stopping criteria
  47. early_stop = EarlyStopping(monitor="val_loss", patience=10)
  48. # Fit the model to the training data
  49. history = model.fit(X_train, Y_train, epochs=100, batch_size=32, validation_data=(X_test, Y_test), callbacks=[early_stop])
  50. # Make predictions on the test data
  51. y_pred = model.predict(X_test)
  52. # Inverse the scaling of the data
  53. y_pred = scaler.inverse_transform(y_pred)
  54. Y_test = scaler.inverse_transform([Y_test])
  55. # Evaluate the model
  56. mse = mean_squared_error(Y_test[0], y_pred)
  57. msle = mean_squared_log_error(Y_test[0], y_pred)
  58. mae = mean_absolute_error(Y_test[0], y_pred)
  59. r2 = r2_score(Y_test[0], y_pred)
  60. print('MSE: ', mse)
  61. print('MSLE: ', msle)
  62. print('MAE: ', mae)
  63. print('R-squared: ', r2)
  64. # Plot the predicted vs actual values
  65. plt.figure(figsize=(10, 6))
  66. plt.style.use('fivethirtyeight')
  67. plt.plot(df["Date"][train_size + look_back :], Y_test.flatten(), label="Actual", linewidth = 3, alpha = 0.4)
  68. plt.plot(df["Date"][train_size + look_back :], y_pred.flatten(), label="Predicted", linewidth = 1.5)
  69. plt.title("LSTM: Actual vs Predicted")
  70. plt.xlabel("Date")
  71. plt.ylabel("Stock Price")
  72. plt.legend()
  73. plt.show()

此代码是先前代码的扩展版本,其中包含 ChatGPT 建议的所有小更改。根据ChatGPT的说法:它增加了LSTM层数,增加了每层神经元的数量,添加了dropout层,并调整了学习率。它还通过对整个历史数据进行训练来使用更多的数据,并使用不同的损失函数,均方对数误差(MSLE),这更适合时间序列数据。

在我看来,代码没有任何重大更改,但这是可以接受的,因为没有必要进行大量修改来改进现有模型。以下是两个模型结果的并排比较(上:之前的模型,下:改进的模型):

通过观察结果,我们可以得出结论,LSTM模型的性能有了一些改进。新模型的 R2 分数从 0.93 增加到 0.96,表明预测精度更高,并且 MSE、MSLE 和 MAE 分数均显着降低,使其更加接近于零。

5. 超参数调优

如果没有超参数调整,任何人工智能和机器学习项目都是不完整的。以下是有关超参数调整的一些背景知识:涉及调整机器学习模型的设置以提高其性能的过程称为超参数调整。这些设置称为超参数,是在训练模型之前设置的,包括学习率、层数和神经元等参数。微调这些设置的主要目的是提高模型的预测准确性。以下是 ChatGPT 在要求对先前生成的模型执行超参数调整时生成的代码:

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.preprocessing import MinMaxScaler
  4. from tensorflow.keras.models import Sequential
  5. from tensorflow.keras.layers import Dense, LSTM, Dropout
  6. from tensorflow.keras.optimizers import Adam
  7. from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
  8. from sklearn.model_selection import GridSearchCV, TimeSeriesSplit
  9. # Download the data
  10. df = pd.read_csv('aapl.csv').dropna()
  11. df = df.set_index('Date')
  12. # Scale the data
  13. scaler = MinMaxScaler()
  14. scaled_data = scaler.fit_transform(df['Close'].values.reshape(-1, 1))
  15. # Define the training and test sets
  16. train_size = int(len(scaled_data) * 0.8)
  17. train_data = scaled_data[:train_size]
  18. test_data = scaled_data[train_size:]
  19. # Define the function to create the LSTM model
  20. def create_model(neurons=50, layers=2, dropout=0.2, learning_rate=0.001):
  21. model = Sequential()
  22. for i in range(layers):
  23. model.add(LSTM(units=neurons, return_sequences=True, input_shape=(train_data.shape[1], 1)))
  24. model.add(Dropout(dropout))
  25. model.add(LSTM(units=neurons))
  26. model.add(Dropout(dropout))
  27. model.add(Dense(units=1))
  28. optimizer = Adam(learning_rate=learning_rate)
  29. model.compile(optimizer=optimizer, loss='mean_squared_error')
  30. return model
  31. # Define the parameter grid for hyperparameter tuning
  32. param_grid = {
  33. 'neurons': [50, 100, 200],
  34. 'layers': [2, 3],
  35. 'dropout': [0.2, 0.3],
  36. 'learning_rate': [0.001, 0.01]
  37. }
  38. # Define the time series split for cross-validation
  39. tscv = TimeSeriesSplit(n_splits=3)
  40. # Create the KerasRegressor for hyperparameter tuning
  41. model = KerasRegressor(build_fn=create_model, epochs=50, batch_size=32, verbose=0)
  42. # Perform the hyperparameter tuning
  43. grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=tscv, scoring='neg_mean_squared_error', n_jobs=-1)
  44. grid_result = grid.fit(train_data, train_data)
  45. # Print the best parameters and score
  46. print("Best parameters: ", grid_result.best_params_)
  47. print("Best score: ", np.sqrt(-grid_result.best_score_))
  48. # Use the best model to make predictions on the test data
  49. best_model = grid_result.best_estimator_.model
  50. predictions = best_model.predict(test_data.reshape(-1, 1))
  51. predictions = scaler.inverse_transform(predictions)
  52. # Plot the predictions and actual values
  53. import matplotlib.pyplot as plt
  54. # Plot the predicted vs actual values
  55. plt.figure(figsize=(10, 6))
  56. plt.style.use('fivethirtyeight')
  57. plt.plot(pd.to_datetime(df.index[train_size:]), df['Close'][train_size:], label='Actual', linewidth = 3, alpha = 0.4)
  58. plt.plot(pd.to_datetime(df.index[train_size:]), predictions, label='Predicted', linewidth = 1.5)
  59. plt.title(f"AAPL Stock Price Prediction")
  60. plt.xlabel("Date")
  61. plt.ylabel("Stock Price")
  62. plt.legend()
  63. plt.show()

最佳参数:  {'dropout': 0.3, 'layers': 2, 'learning_rate': 0.001, 'neurons': 200}
最佳得分:  0.02022405674165489

ChatGPT进一步使用最佳参数来构建模型并进行预测。该图是超参数调优得出的模型的最终结果:

结论

尽管 ChatGPT 帮助我们编写了一些非常好的代码来预测股票价格,但这些模型在现实市场中大多已经过时,因为价格也会由情绪驱动,而这些因素无法使用由一个机器人去编写的代码。

这并不意味着我们迄今为止所做的工作绝对是垃圾,而是实际分析的起点。通过这些人工智能模型了解价格变动为我们提供了分析股票时应遵循的一些方向或方法布局。因此,在做出投资决策时,预测分析只能被视为次要工具,而不是盲目相信它。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/839911
推荐阅读
相关标签
  

闽ICP备14008679号