Created
October 17, 2023 02:38
-
-
Save saimadhu-polamuri/c862fbf72457241dbd06318d3ac5bcb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import MinMaxScaler | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.layers import Dense, Dropout, LSTM,Flatten | |
# Load data | |
df = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv') | |
# Prepare data | |
scaler = MinMaxScaler() | |
data = scaler.fit_transform(df['Temp'].values.reshape(-1, 1)) | |
train_data = data[:3000] | |
test_data = data[3000:] | |
# Create sequences | |
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]) | |
return np.array(X), np.array(y) | |
seq_length = 30 | |
X_train, y_train = create_sequences(train_data, seq_length) | |
X_test, y_test = create_sequences(test_data, seq_length) | |
# Build model | |
model = Sequential() | |
model.add(Dense(64, activation='relu', input_shape=(seq_length, 1))) | |
model.add(Flatten()) | |
model.add(Dense(1, activation='linear')) | |
model.compile(optimizer='adam', loss='mse', metrics=['mae']) | |
# Train model | |
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=0) | |
# Make predictions | |
train_predictions = model.predict(X_train) | |
test_predictions = model.predict(X_test) | |
# Scale back the data | |
train_predictions = scaler.inverse_transform(train_predictions) | |
y_train = scaler.inverse_transform(y_train) | |
test_predictions = scaler.inverse_transform(test_predictions) | |
y_test = scaler.inverse_transform(y_test) | |
# Evaluate model | |
train_rmse = np.sqrt(mean_squared_error(y_train, train_predictions)) | |
test_rmse = np.sqrt(mean_squared_error(y_test, test_predictions)) | |
print(f'Train RMSE: {train_rmse:.2f}') | |
print(f'Test RMSE: {test_rmse:.2f}') | |
test_sequences = [] | |
for i in range(seq_length, len(test_data)): | |
test_sequences.append(test_data[i-seq_length:i]) | |
test_sequences = np.array(test_sequences) | |
test_sequences = np.reshape(test_sequences, (test_sequences.shape[0], test_sequences.shape[1], 1)) | |
# Generate predictions for the test data | |
test_predictions = model.predict(test_sequences) | |
# Shift predictions to the original scale | |
test_predictions = scaler.inverse_transform(test_predictions.reshape(-1, 1)) | |
# Plot the results | |
plt.figure(figsize=(10,6)) | |
plt.plot(df.index[3000+seq_length:], df['Temp'].values[3000+seq_length:], label='Test') | |
plt.plot(df.index[seq_length:3000], train_predictions.flatten(), label='Train Predictions') | |
plt.plot(df.index[3000+seq_length:], test_predictions.flatten(), label='Test Predictions') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment