当前位置:   article > 正文

简单的基于LSTM的黄金价格预测(Python)

简单的基于LSTM的黄金价格预测(Python)
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import plotly.express as px
  5. from sklearn.preprocessing import MinMaxScaler
  6. from sklearn.model_selection import train_test_split
  7. from sklearn.metrics import mean_absolute_percentage_error
  8. import tensorflow as tf
  9. from keras import Model
  10. from keras.layers import Input, Dense, Dropout, LSTM
  11. df = pd.read_csv('Gold Price (2013-2023).csv' )
  12. df

  1. df.info()
  2. <class 'pandas.core.frame.DataFrame'>
  3. RangeIndex: 2583 entries, 0 to 2582
  4. Data columns (total 7 columns):
  5. # Column Non-Null Count Dtype
  6. --- ------ -------------- -----
  7. 0 Date 2583 non-null object
  8. 1 Price 2583 non-null object
  9. 2 Open 2583 non-null object
  10. 3 High 2583 non-null object
  11. 4 Low 2583 non-null object
  12. 5 Vol. 2578 non-null object
  13. 6 Change % 2583 non-null object
  14. dtypes: object(7)
  15. memory usage: 141.4+ KB
  16. df.drop(['Vol.', 'Change %'], axis=1, inplace=True)
  17. df

  1. # Convert the 'Date' column to datetime
  2. df['Date'] = pd.to_datetime(df['Date'])
  3. # Sort the DataFrame by the 'Date' column in ascending order
  4. df.sort_values(by='Date', ascending=True, inplace=True)
  5. # Reset the index of the DataFrame
  6. df.reset_index(drop=True, inplace=True)
  7. numCols = df.columns.drop('Date')
  8. df[numCols] = df[numCols].replace({',': ''}, regex=True)
  9. df[numCols] = df[numCols].astype('float64')
  10. df.head()
  11. df.duplicated().sum()
  12. df.isnull().sum()
  13. Date 0
  14. Price 0
  15. Open 0
  16. High 0
  17. Low 0
  18. dtype: int64
  19. import plotly.express as px
  20. fig = px.line(y=df['Price'], x=df['Date'])
  21. fig.update_traces(line_color='black')
  22. fig.update_layout(
  23. xaxis_title='Date',
  24. yaxis_title='Price',
  25. title={
  26. 'text': 'Gold Price Data',
  27. 'y': 0.95,
  28. 'x': 0.5,
  29. 'xanchor': 'center',
  30. 'yanchor': 'top'
  31. },
  32. plot_bgcolor='rgba(255,223,0,0.9)'
  33. )
  34. fig.show()
  35. test_size = df[df.Date.dt.year == 2022].shape[0]
  36. print(test_size)

260

  1. import matplotlib.pyplot as plt
  2. plt.figure(figsize=(15, 6), dpi=150)
  3. plt.rcParams['axes.facecolor'] = 'cyan'
  4. plt.rc('axes', edgecolor='white')
  5. plt.plot(df.Date[:-test_size], df.Price[:-test_size], color='black', lw=2)
  6. plt.plot(df.Date[-test_size:], df.Price[-test_size:], color='red', lw=2)
  7. plt.title('Gold Price Train and Test', fontsize=15)
  8. plt.xlabel('Date', fontsize=12)
  9. plt.ylabel('Price', fontsize=12)
  10. plt.legend(['Train Set', 'Test Set'], loc='upper left', prop={'size': 15})
  11. plt.grid(color='white')
  12. plt.show()

  1. scaler = MinMaxScaler()
  2. scaler.fit(df.Price.values.reshape(-1, 1))
  3. MinMaxScaler()
  4. window_size = 60
  5. train_data = df.Price[:-test_size]
  6. train_data = scaler.fit_transform(train_data.values.reshape(-1, 1))
  7. window_size = 60
  8. X_train = []
  9. y_train = []
  10. for i in range(window_size, len(train_data)):
  11. X_train.append(train_data[i-window_size:i, 0])
  12. y_train.append(train_data[i, 0])
  13. test_data = df.Price[-test_size-window_size:]
  14. test_data = scaler.transform(test_data.values.reshape(-1, 1))
  15. X_test = []
  16. y_test = []
  17. for i in range(window_size, len(test_data)):
  18. X_test.append(test_data[i-window_size:i, 0])
  19. y_test.append(test_data[i, 0])
  20. X_train = np.array(X_train)
  21. X_test = np.array(X_test)
  22. y_train = np.array(y_train)
  23. y_test = np.array(y_test)
  24. X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
  25. X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
  26. y_train = np.reshape(y_train, (-1, 1))
  27. y_test = np.reshape(y_test, (-1, 1))
  28. print('X_train shape:', X_train.shape)
  29. print('y_train shape:', y_train.shape)
  30. print('X_test shape:', X_test.shape)
  31. print('y_test shape:', y_test.shape)
  32. X_train shape: (2263, 60, 1)
  33. y_train shape: (2263, 1)
  34. X_test shape: (260, 60, 1)
  35. y_test shape: (260, 1)
  36. import tensorflow as tf
  37. def define_model():
  38. input1 = Input(shape=(window_size, 1))
  39. x = tf.keras.layers.LSTM(units=64, return_sequences=True)(input1)
  40. x = tf.keras.layers.Dropout(0.2)(x)
  41. x = tf.keras.layers.LSTM(units=64, return_sequences=True)(x)
  42. x = tf.keras.layers.Dropout(0.2)(x)
  43. x = tf.keras.layers.LSTM(units=64)(x)
  44. x = tf.keras.layers.Dropout(0.2)(x)
  45. x = tf.keras.layers.Dense(32, activation='softmax')(x)
  46. dnn_output = tf.keras.layers.Dense(1)(x)
  47. model = tf.keras.models.Model(inputs=input1, outputs=dnn_output)
  48. # Import and use the Nadam optimizer
  49. model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Nadam())
  50. model.summary()
  51. return model
  52. model = define_model()
  53. history = model.fit(X_train, y_train, epochs=150, batch_size=32, validation_split=0.1, verbose=1)
  54. Model: "model_3"
  55. _________________________________________________________________
  56. Layer (type) Output Shape Param #
  57. =================================================================
  58. input_4 (InputLayer) [(None, 60, 1)] 0
  59. lstm_9 (LSTM) (None, 60, 64) 16896
  60. dropout_9 (Dropout) (None, 60, 64) 0
  61. lstm_10 (LSTM) (None, 60, 64) 33024
  62. dropout_10 (Dropout) (None, 60, 64) 0
  63. lstm_11 (LSTM) (None, 64) 33024
  64. dropout_11 (Dropout) (None, 64) 0
  65. dense_6 (Dense) (None, 32) 2080
  66. dense_7 (Dense) (None, 1) 33
  67. =================================================================
  68. Total params: 85057 (332.25 KB)
  69. Trainable params: 85057 (332.25 KB)
  70. Non-trainable params: 0 (0.00 Byte)
  71. _________________________________________________________________
  72. result = model.evaluate(X_test, y_test)
  73. y_pred = model.predict(X_test)
  74. MAPE = mean_absolute_percentage_error(y_test, y_pred)
  75. Accuracy = 1 - MAPE
  76. print('Test Loss:', result)
  77. print('Test MAPE:', MAPE)
  78. print('Test Accuracy:', Accuracy)
  79. Test Loss: 0.0008509838371537626
  80. Test MAPE: 0.0319030650799213
  81. Test Accuracy: 0.9680969349200788
  82. y_test_true = scaler.inverse_transform(y_test.reshape(-1, 1)).flatten()
  83. y_test_pred = scaler.inverse_transform(y_pred.reshape(-1, 1)).flatten()
  84. plt.figure(figsize=(15, 6), dpi=150)
  85. plt.rcParams['axes.facecolor'] = 'cyan'
  86. plt.rc('axes', edgecolor='white')
  87. plt.plot(df.Date[:-test_size], df.Price[:-test_size], color='black', lw=2)
  88. plt.plot(df.Date[-test_size:], df.Price[-test_size:], color='red', lw=2)
  89. plt.title('Gold Price Train and Test', fontsize=15)
  90. plt.xlabel('Date', fontsize=12)
  91. plt.ylabel('Price', fontsize=12)
  92. plt.legend(['Train Set', 'Test Set'], loc='upper left', prop={'size': 15})
  93. plt.grid(color='white')
  94. plt.show()

知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》审稿专家,担任《中国电机工程学报》,《控制与决策》等EI期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

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

闽ICP备14008679号