i have the following code to train a lstm model on my historical crypto dataset: %%…
- User
i have the following code to train a lstm model on my historical crypto dataset:
%%
import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM, Dropout from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt
%%
Load the dataset
df = pd.read_csv(“your_crypto_data.csv”)
Selecting the 'Close' column and converting it to numpy array
close_prices = df['Close'].values.reshape(-1, 1)
Scale the data between 0 and 1
scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(close_prices)
Creating the dataset with 30 timesteps
x = [] y = []
for i in range(30, len(scaled_data)): x.append(scaled_data[i-30:i, 0]) y.append(scaled_data[i, 0])
%%
x, y = np.array(x), np.array(y)
Splitting the dataset into training and testing
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
%%
Reshape the data to fit the LSTM layer
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
%%
Now, let's build the LSTM model:
model = Sequential()
Adding the first LSTM layer with some Dropout regularisation
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1))) model.add(Dropout(0.2))
Adding a second LSTM layer and some Dropout regularisation
model.add(LSTM(units=50, return_sequences=False)) model.add(Dropout(0.2))
Adding the output layer
model.add(Dense(units=1))
Compiling the RNN
model.compile(optimizer='adam', loss='mean_squared_error')
Fitting the RNN to the Training set
model.fit(x_train, y_train, epochs=100, batch_size=32)
%%
predicted_prices = model.predict(x_test) predicted_prices = scaler.inverse_transform(predicted_prices) # Undo the scaling
Visualising the results
plt.plot(scaler.inverse_transform(y_test.reshape(-1, 1)), color='red', label='Real Crypto Price') plt.plot(predicted_prices, color='blue', label='Predicted Crypto Price') plt.title('Crypto Price Prediction') plt.xlabel('Time') plt.ylabel('Crypto Price') plt.legend() plt.show()
currently the model train on just close price but i have other features in my dataset,like open,close,high, volume and...
update the code so the model train on all features on my dataset and predict close price of next day based on last 30 days
- Assistant
- User
and also i should mention that i have two columns that i want the model to predict one is Close and the other which named Prior update the code
- Assistant