import pandas as pd import numpy as np import matplotlib.pyplot as plt from…
- User
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import LSTM, Dense, Dropout from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
1. Load Data
file_path = r"C:\Users\jack8\OneDrive\stock cow ai\data\stock price.csv" df = pd.read_csv(file_path, parse_dates=['Date'])
3. Sort and Indexing
df.sort_values('Date', inplace=True) df.set_index('Date', inplace=True)
4. Handling Missing Values
print("Missing values before cleaning:\n", df.isnull().sum()) df.dropna(inplace=True) # Or use df.fillna(method='ffill') for forward fill print("Missing values after cleaning:\n", df.isnull().sum())
5. Feature Selection
Include 'Closing Price' as an input feature for better forecasting
features = ['Opening Price', 'Highest Price', 'Lowest Price', 'Closing Price'] target = 'Closing Price'
X = df[features] y = df[target]
6. Scaling the Data
scaler_X = MinMaxScaler(feature_range=(0, 1)) X_scaled = scaler_X.fit_transform(X)
scaler_y = MinMaxScaler(feature_range=(0, 1)) y_scaled = scaler_y.fit_transform(y.values.reshape(-1, 1))
7. Train-Test Split Based on Time
train_size = int(len(X_scaled) * 0.8) test_size = len(X_scaled) - train_size
X_train_raw, X_test_raw = X_scaled[:train_size], X_scaled[train_size:] y_train_raw, y_test_raw = y_scaled[:train_size], y_scaled[train_size:]
print(f"Training samples: {X_train_raw.shape[0]}") print(f"Testing samples: {X_test_raw.shape[0]}")
8. Creating Sequences for LSTM
def create_sequences(X, y, time_steps=10): Xs, ys = [], [] for i in range(len(X) - time_steps): Xs.append(X[i:(i + time_steps)]) ys.append(y[i + time_steps]) return np.array(Xs), np.array(ys)
time_steps = 10 X_train, y_train = create_sequences(X_train_raw, y_train_raw, time_steps) X_test, y_test = create_sequences(X_test_raw, y_test_raw, time_steps)
print(f"Shape of X_train: {X_train.shape}") # (num_train_samples, time_steps, num_features) print(f"Shape of y_train: {y_train.shape}") # (num_train_samples, 1) print(f"Shape of X_test: {X_test.shape}") # (num_test_samples, time_steps, num_features) print(f"Shape of y_test: {y_test.shape}") # (num_test_samples, 1)
9. Extract Corresponding Dates for y_test
Extract dates starting from (time_steps) to align with y values
dates = df.index[time_steps:]
Now, split the dates into train and test based on train_size
train_dates = dates[:train_size - time_steps] # Adjusted to match the sequences test_dates = dates[train_size:] # Start from train_size
10. Building the LSTM Model with Enhanced Dropout
model = Sequential() model.add(LSTM( units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.2, # 增加Dropout率至0.3 recurrent_dropout=0.2 # 添加遞歸Dropout )) model.add(Dropout(0.2)) # 增加Dropout率至0.3 model.add(LSTM( units=50, return_sequences=False, dropout=0.2, # 增加Dropout率至0.3 recurrent_dropout=0.2 # 添加遞歸Dropout )) model.add(Dropout(0.2)) # 增加Dropout率至0.3 model.add(Dense(units=25)) model.add(Dropout(0.2)) # 在全連接層之間添加Dropout model.add(Dense(units=1)) # Output layer
model.compile(optimizer='adam', loss='mean_squared_error') model.summary()
11. Training the Model
epochs = 300 batch_size = 32
history = model.fit( X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test, y_test), verbose=1 )
##早停 from keras.callbacks import EarlyStopping, ReduceLROnPlateau
定義Early Stopping
early_stop = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)
定義學習率調度
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)
修改模型訓練部分
history = model.fit( X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test, y_test), callbacks=[early_stop, reduce_lr], verbose=1 )
12. Evaluating the Model
Plot Training and Validation Loss
plt.figure(figsize=(12, 6)) plt.plot(history.history['loss'], label='Training Loss') plt.plot(history.history['val_loss'], label='Validation Loss') plt.title('Loss Over Epochs') plt.xlabel('Epoch') plt.ylabel('Mean Squared Error') plt.legend() plt.show()
Making Predictions
y_pred_scaled = model.predict(X_test) y_pred = scaler_y.inverse_transform(y_pred_scaled) y_test_actual = scaler_y.inverse_transform(y_test)
13. Plot Actual vs Predicted with Dates on x-axis
plt.figure(figsize=(14,7)) plt.plot(test_dates, y_test_actual, label='Actual Closing Price', color='blue') plt.plot(test_dates, y_pred, label='Predicted Closing Price', color='red') plt.title('Actual vs Predicted Closing Prices') plt.xlabel('Date') # Changed from 'Time' to 'Date' plt.ylabel('Closing Price') plt.legend() plt.show()
14. Calculating Performance Metrics
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
mse = mean_squared_error(y_test_actual, y_pred) mae = mean_absolute_error(y_test_actual, y_pred)
Calculate Mean Absolute Percentage Error (MAPE)
mape = np.mean(np.abs((y_test_actual - y_pred) / y_test_actual)) * 100
Calculate R-squared
r2 = r2_score(y_test_actual, y_pred)
print(f"Mean Squared Error (MSE): {mse}") print(f"Mean Absolute Error (MAE): {mae}") print(f"Mean Absolute Percentage Error (MAPE): {mape:.2f}%") print(f"R-squared (R2 ): {r2:.4f}")
15. Recursive Forecasting for Future Predictions
def forecast_multivariate(model, X_input, scaler_y, n_steps=20): """ Predict the next n_steps closing prices based on X_input.
Parameters: - model: Trained LSTM model - X_input: The initial input sequence (scaled), shape=(time_steps, num_features) - scaler_y: Scaler for the target variable - n_steps: Number of future steps to predict Returns: - List of predicted values (original scale) """ predictions = [] X_current = X_input.copy() for _ in range(n_steps): # Predict the next closing price pred_scaled = model.predict(X_current.reshape(1, X_current.shape[0], X_current.shape[1])) predictions.append(pred_scaled[0, 0]) # Create a new feature vector for the next prediction new_feature = X_current[-1].copy() # Copy the last time step # Update 'Closing Price' with the predicted value new_feature[-1] = pred_scaled # Replace the 'Closing Price' with prediction # Append the new feature to the sequence and remove the first time step X_current = np.append(X_current[1:], [new_feature.flatten()], axis=0) # Inverse transform the predictions to original scale predictions = scaler_y.inverse_transform(np.array(predictions).reshape(-1, 1)) return predictions.flatten()Example Usage:
Using the last sequence from the test set to predict the next 20 steps
last_sequence = X_test[-1] future_steps = 20 future_predictions = forecast_multivariate(model, last_sequence, scaler_y, n_steps=future_steps)
16. Plot Future Predictions with Dates
Generate future dates (business days)
last_date = test_dates[-1] future_dates = pd.bdate_range(start=last_date + pd.Timedelta(days=1), periods=future_steps)
plt.figure(figsize=(14, 7)) plt.plot(future_dates, future_predictions, label='Future Predictions', color='green') plt.title('Future Closing Price Predictions') plt.xlabel('Date') plt.ylabel('Closing Price') plt.legend() plt.show()
###能怎麼優化,請一步一步告訴我
- Assistant
要優化您目前的LSTM模型以進行股票價格預測,您可以從數據預處理、模型架構、超參數調整、特徵工程、訓練策略等多個方面進行改進。以下是一步一步的優化建議:
1. 數據預處理優化
a. 增加更多特徵 (Feature Engineering)
除了現有的
['Opening Price', 'Highest Price', 'Lowest Price', 'Closing Price'],可以添加更多技術指標來幫助模型更好地捕捉市場趨勢。例如:- 移動平均線 (Moving Averages): 短期和長期移動平均,例如5日和20日移動平均。
- 相對強弱指數 (RSI): 衡量價格變動的速度和變化。
- 移動平均收斂散度指標 (MACD): 捕捉趨勢的強弱和反轉點。
- 交易量 (Volume): 交易量信息有助於理解市場活躍度。
# 增加移動平均線 df['MA5'] = df['Closing Price'].rolling(window=5).mean() df['MA20'] = df['Closing Price'].rolling(window=20).mean() # 增加 RSI def compute_RSI(series, period=14): delta = series.diff(1) gain = delta.where(delta > 0, 0) loss = -delta.where(delta < 0, 0) avg_gain = gain.rolling(window=period).mean() avg_loss = loss.rolling(window=period).mean() RS = avg_gain / avg_loss RSI = 100 - (100 / (1 + RS)) return RSI df['RSI'] = compute_RSI(df['Closing Price']) # 增加 MACD df['EMA12'] = df['Closing Price'].ewm(span=12, adjust=False).mean() df['EMA26'] = df['Closing Price'].ewm(span=26, adjust=False).mean() df['MACD'] = df['EMA12'] - df['EMA26'] df['Signal_Line'] = df['MACD'].ewm(span=9, adjust=False).mean() # 增加交易量 features = ['Opening Price', 'Highest Price', 'Lowest Price', 'Closing Price', 'MA5', 'MA20', 'RSI', 'MACD', 'Signal_Line', 'Volume']b. 處理異常值 (Outlier Handling)
股票數據中可能存在異常波動,這些異常值可能會影響模型訓練。您可以使用統計方法或可視化工具來檢測和處理異常值。
# 使用IQR方法檢測異常值 Q1 = df.quantile(0.25) Q3 = df.quantile(0.75) IQR = Q3 - Q1 df = df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]c. 調整縮放方法
您目前使用的是
MinMaxScaler,可以嘗試其他縮放方法,如StandardScaler,看看是否能提升模型性能。from sklearn.preprocessing import StandardScaler scaler_X = StandardScaler() X_scaled = scaler_X.fit_transform(X) scaler_y = StandardScaler() y_scaled = scaler_y.fit_transform(y.values.reshape(-1, 1))d. 調整序列長度
目前的序列長度設定為10,您可以嘗試不同的序列長度,如20或30,觀察對模型性能的影響。
time_steps = 20 # 嘗試不同的序列長度 X_train, y_train = create_sequences(X_train_raw, y_train_raw, time_steps) X_test, y_test = create_sequences(X_test_raw, y_test_raw, time_steps)2. 模型架構優化
a. 調整LSTM層數和單元數
您可以嘗試不同數量的LSTM層以及不同的單元數。例如,增加到2層或4層LSTM,或改變每層的單元數(如100、200等)。
model = Sequential() model.add(LSTM( units=100, # 增加單元數 return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.3, recurrent_dropout=0.3 )) model.add(Dropout(0.3)) model.add(LSTM( units=100, return_sequences=False, dropout=0.3, recurrent_dropout=0.3 )) model.add(Dropout(0.3)) model.add(Dense(units=50)) model.add(Dropout(0.3)) model.add(Dense(units=1))b. 使用雙向LSTM (Bidirectional LSTM)
雙向LSTM能夠同時考慮序列的前後信息,可能對預測有幫助。
from keras.layers import Bidirectional model = Sequential() model.add(Bidirectional(LSTM( units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.2, recurrent_dropout=0.2 ))) model.add(Dropout(0.2)) model.add(Bidirectional(LSTM( units=50, return_sequences=False, dropout=0.2, recurrent_dropout=0.2 ))) model.add(Dropout(0.2)) model.add(Dense(units=25)) model.add(Dropout(0.2)) model.add(Dense(units=1))c. 嘗試不同的激活函數和優化器
默認的LSTM激活函數通常是
tanh和sigmoid,您可以嘗試其他激活函數,如ReLU,以及不同的優化器,如RMSprop或Nadam。model.compile(optimizer='RMSprop', loss='mean_squared_error')d. 使用正規化技術
除了Dropout之外,還可以使用L2正則化來防止過擬合。
from keras.regularizers import l2 from keras.layers import LSTM model = Sequential() model.add(LSTM( units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.2, recurrent_dropout=0.2, kernel_regularizer=l2(0.001) )) model.add(Dropout(0.2)) # 其餘層同樣添加正則化3. 超參數調整 (Hyperparameter Tuning)
a. 使用網格搜索或隨機搜索自動調整超參數
手動調整可能耗時,可以考慮使用
Keras Tuner或Optuna等工具來自動搜尋最佳超參數組合。import keras_tuner as kt def build_model(hp): model = Sequential() model.add(LSTM( units=hp.Int('units', min_value=50, max_value=200, step=50), return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=hp.Float('dropout', 0.1, 0.5, step=0.1), recurrent_dropout=hp.Float('recurrent_dropout', 0.1, 0.5, step=0.1) )) model.add(Dropout(rate=hp.Float('dropout_rate', 0.1, 0.5, step=0.1))) model.add(LSTM( units=hp.Int('units', min_value=50, max_value=200, step=50), return_sequences=False, dropout=hp.Float('dropout', 0.1, 0.5, step=0.1), recurrent_dropout=hp.Float('recurrent_dropout', 0.1, 0.5, step=0.1) )) model.add(Dropout(rate=hp.Float('dropout_rate', 0.1, 0.5, step=0.1))) model.add(Dense(units=hp.Int('dense_units', min_value=10, max_value=100, step=10))) model.add(Dropout(rate=hp.Float('dense_dropout', 0.1, 0.5, step=0.1))) model.add(Dense(units=1)) model.compile( optimizer=hp.Choice('optimizer', ['adam', 'rmsprop', 'nadam']), loss='mean_squared_error' ) return model tuner = kt.RandomSearch( build_model, objective='val_loss', max_trials=20, executions_per_trial=2, directory='kt_dir', project_name='stock_price_prediction' ) tuner.search(X_train, y_train, epochs=100, validation_data=(X_test, y_test), callbacks=[early_stop, reduce_lr]) best_hps = tuner.get_best_hyperparameters(num_trials=1)[0] print(best_hps.values)b. 調整學習率
學習率對模型收斂速度和效果有很大影響。可以通過學習率調度器(如您已經使用的
ReduceLROnPlateau)動態調整學習率,或在超參數調整中包括學習率的選擇。4. 特徵工程進一步優化
a. 使用滯後特徵 (Lag Features)
除了時間序列的滑動窗口外,可以引入滯後特徵(如前一天、前兩天的價格)來幫助模型捕捉時間依賴性。
df['Lag1'] = df['Closing Price'].shift(1) df['Lag2'] = df['Closing Price'].shift(2) # 添加更多滯後特徵 df.dropna(inplace=True)b. 添加滾動統計特徵 (Rolling Statistics)
如滾動標準差、滾動最大值和最小值。
df['Rolling_STD'] = df['Closing Price'].rolling(window=20).std() df['Rolling_Max'] = df['Closing Price'].rolling(window=20).max() df['Rolling_Min'] = df['Closing Price'].rolling(window=20).min()c. 使用主成分分析 (PCA)
如果特徵過多,可以使用PCA降維以減少噪音和計算量。
from sklearn.decomposition import PCA pca = PCA(n_components=0.95) # 保留95%的方差 X_scaled_pca = pca.fit_transform(X_scaled)5. 訓練策略優化
a. 使用時間序列交叉驗證 (TimeSeries Split)
傳統的交叉驗證在時間序列數據中可能不適用,可以使用
TimeSeriesSplit來進行交叉驗證。from sklearn.model_selection import TimeSeriesSplit tscv = TimeSeriesSplit(n_splits=5) for train_index, val_index in tscv.split(X_scaled): X_train, X_val = X_scaled[train_index], X_scaled[val_index] y_train, y_val = y_scaled[train_index], y_scaled[val_index] # 訓練模型b. 使用更多回調函數
除了早停和學習率調度,可以考慮使用模型檢查點(ModelCheckpoint)來保存最佳模型。
from keras.callbacks import ModelCheckpoint checkpoint = ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, mode='min') history = model.fit( X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test, y_test), callbacks=[early_stop, reduce_lr, checkpoint], verbose=1 )c. 增加正則化方法
除了Dropout和L2正則化,還可以考慮使用批量正規化(Batch Normalization)來加快訓練速度並穩定模型。
from keras.layers import BatchNormalization model = Sequential() model.add(LSTM( units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.2, recurrent_dropout=0.2 )) model.add(BatchNormalization()) model.add(Dropout(0.2)) # 其餘層同樣添加BatchNormalization6. 模型評估與選擇
a. 使用多種評估指標
除了MSE、MAE、MAPE和R²,還可以考慮使用其他指標,如RMSE、可解釋變異(Explained Variance)。
from sklearn.metrics import explained_variance_score rmse = np.sqrt(mean_squared_error(y_test_actual, y_pred)) ev = explained_variance_score(y_test_actual, y_pred) print(f"Root Mean Squared Error (RMSE): {rmse}") print(f"Explained Variance Score: {ev}")b. 分析殘差 (Residual Analysis)
繪製實際值與預測值之間的差異,檢查是否存在系統性偏差。
residuals = y_test_actual - y_pred plt.figure(figsize=(10,5)) plt.hist(residuals, bins=50, edgecolor='k') plt.title('Residuals Distribution') plt.xlabel('Residual') plt.ylabel('Frequency') plt.show() plt.figure(figsize=(10,5)) plt.plot(test_dates, residuals) plt.title('Residuals Over Time') plt.xlabel('Date') plt.ylabel('Residual') plt.show()7. 未來預測的進一步優化
a. 多步預測策略
目前的預測是遞歸預測(每一步依賴於前一步的預測),這種方法可能會導致誤差積累。可以考慮使用直接多步預測或序列到序列模型(Seq2Seq)來提高預測精度。
# 示例:直接多步預測 def forecast_direct(model, X_input, scaler_y, n_steps=20): predictions = [] for _ in range(n_steps): pred_scaled = model.predict(X_input.reshape(1, X_input.shape[0], X_input.shape[1])) predictions.append(pred_scaled[0, 0]) # 更新輸入序列 X_input = np.append(X_input[1:], [X_input[-1]], axis=0) # 根據需要更新 predictions = scaler_y.inverse_transform(np.array(predictions).reshape(-1, 1)) return predictions.flatten()b. 改進遞歸預測方法
在遞歸預測中,可以考慮使用生成的預測值來更新所有相關特徵,而不僅僅是目標變量。
def forecast_multivariate_improved(model, X_input, scaler_y, n_steps=20): predictions = [] X_current = X_input.copy() for _ in range(n_steps): pred_scaled = model.predict(X_current.reshape(1, X_current.shape[0], X_current.shape[1])) predictions.append(pred_scaled[0, 0]) # 更新特徵向量,包括用預測值替換Closing Price new_feature = X_current[-1].copy() new_feature[-1] = pred_scaled # 假設最後一個特徵是Closing Price # 添加新的特徵向量到序列中 X_current = np.append(X_current[1:], [new_feature], axis=0) predictions = scaler_y.inverse_transform(np.array(predictions).reshape(-1, 1)) return predictions.flatten()8. 考慮其他模型
a. 嘗試不同的深度學習模型
除了LSTM,可以嘗試GRU、雙向LSTM、Transformer等模型,看看是否能獲得更好的預測效果。
from keras.layers import GRU model = Sequential() model.add(GRU( units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]), dropout=0.2, recurrent_dropout=0.2 )) model.add(Dropout(0.2)) model.add(GRU( units=50, return_sequences=False, dropout=0.2, recurrent_dropout=0.2 )) model.add(Dropout(0.2)) model.add(Dense(units=25)) model.add(Dropout(0.2)) model.add(Dense(units=1))b. 集成學習 (Ensemble Learning)
結合多個模型的預測結果,如LSTM和隨機森林,來提升預測精度。
from sklearn.ensemble import RandomForestRegressor # LSTM預測 y_pred_lstm = model.predict(X_test) # 隨機森林預測 rf = RandomForestRegressor(n_estimators=100, random_state=42) rf.fit(X_train.reshape(X_train.shape[0], -1), y_train) y_pred_rf = rf.predict(X_test.reshape(X_test.shape[0], -1)) # 集成預測 y_pred_ensemble = (y_pred_lstm.flatten() + y_pred_rf) / 2 y_pred_ensemble = scaler_y.inverse_transform(y_pred_ensemble.reshape(-1,1))c. 使用傳統機器學習模型
有時候,傳統的機器學習模型如XGBoost、LightGBM在時間序列預測中表現良好。
import xgboost as xgb # 準備數據 X_train_flat = X_train.reshape(X_train.shape[0], -1) X_test_flat = X_test.reshape(X_test.shape[0], -1) # 訓練XGBoost模型 xgb_model = xgb.XGBRegressor(objective='reg:squarederror', n_estimators=100, learning_rate=0.05) xgb_model.fit(X_train_flat, y_train) # 預測 y_pred_xgb = xgb_model.predict(X_test_flat) y_pred_xgb = scaler_y.inverse_transform(y_pred_xgb.reshape(-1,1))9. 模型解釋與可視化
a. 使用SHAP或LIME進行模型解釋
這有助於了解哪些特徵對預測結果影響最大。
import shap # 使用DeepExplainer來解釋LSTM模型 explainer = shap.DeepExplainer(model, X_train[:100]) shap_values = explainer.shap_values(X_test[:100]) # 繪製SHAP值 shap.summary_plot(shap_values, X_test[:100])b. 可視化特徵重要性
對於傳統機器學習模型,可以使用特徵重要性來評估各特徵的影響。
import matplotlib.pyplot as plt import xgboost as xgb # 以XGBoost為例 xgb_model.fit(X_train_flat, y_train) importances = xgb_model.feature_importances_ feature_names = [f'feature_{i}' for i in range(X_train_flat.shape[1])] plt.barh(feature_names, importances) plt.xlabel('Feature Importance') plt.title('XGBoost Feature Importance') plt.show()10. 其他優化建議
a. 使用更多數據
如果可能,增加更多的歷史數據來訓練模型,這有助於模型更好地理解長期趨勢和季節性變化。
b. 多語料來源
引入其他相關數據源,如行業指數、經濟指標、新聞情感分析等,這些外部數據可能對股票價格有影響。
c. 模型持續學習 (Online Learning)
股票市場是動態的,可以實現模型持續更新以適應最新的市場變化。
# 示例:使用模型的部分重訓練 model.fit(new_X_train, new_y_train, epochs=10, batch_size=32, verbose=1)d. 減少過擬合
如果模型出現過擬合,考慮減少模型複雜度(如減少LSTM單元數、層數),增加正則化,或使用更多數據。
總結
通過上述步驟,您可以系統地優化您的LSTM模型以提升股票價格預測的準確性和穩定性。每個步驟可能對不同數據集和業務場景的效果不同,建議逐步實施並觀察模型性能的變化,以找到最適合您數據和需求的優化方案。如果在實施過程中遇到具體問題,歡迎隨時提問!