import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler…
- User
import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.callbacks import EarlyStopping from tensorflow.keras.optimizers import Adam
File path and data settings
file_path = r"C:\Users\jack8\OneDrive\stock cow ai\data\stock price.csv" features = ['Opening Price', 'Highest Price', 'Lowest Price', 'Closing Price'] target = 'Closing Price'
Read and preprocess the CSV file
df = pd.read_csv(file_path, parse_dates=['Date']) df = df.sort_values('Date')
Scale data
scaler = MinMaxScaler() scaled_data = scaler.fit_transform(df[features])
Convert the data into sequences for LSTM input
def create_sequences(data, seq_length): x, y = [], [] for i in range(len(data) - seq_length): x.append(data[i:i+seq_length]) y.append(data[i+seq_length, features.index(target)]) return np.array(x), np.array(y)
Define sequence length (e.g., 60 days)
seq_length = 40 x, y = create_sequences(scaled_data, seq_length)
Split data into training and testing sets
train_size = int(0.8 * len(x)) x_train, x_test = x[:train_size], x[train_size:] y_train, y_test = y[:train_size], y[train_size:]
Build the LSTM model
model = Sequential([ LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], x_train.shape[2])), LSTM(50), Dense(1) ])
Compile the model
model.compile(optimizer=Adam(learning_rate=0.001), loss='mean_squared_error')
Train the model
early_stop = EarlyStopping(monitor='val_loss', patience=5) history = model.fit( x_train, y_train, validation_data=(x_test, y_test), epochs=50, batch_size=32, callbacks=[early_stop], verbose=1 )
Predict and inverse scale the predictions
predicted = model.predict(x_test) predicted_prices = scaler.inverse_transform(np.concatenate([scaled_data[-len(predicted):, :-1], predicted], axis=1))[:, -1]
Actual prices
actual_prices = scaler.inverse_transform(scaled_data[-len(predicted):])[:, features.index(target)]
Display results
import matplotlib.pyplot as plt
plt.figure(figsize=(14,5)) plt.plot(actual_prices, color='red', label='Actual Stock Price') plt.plot(predicted_prices, color='blue', label='Predicted Stock Price') plt.title('Stock Price Prediction') plt.xlabel('Time') plt.ylabel('Stock Price') plt.legend() plt.show()
###幫我增加mape,rmse,loss值
- Assistant