1
$\begingroup$

I have a chlorophyl-a and sea surface temp (SST) in a NetCDF format, the data is 4 dimensional mattrix, each data is saved inside certain longitude and lattitude value, if the data plotted into heatmap it looked like this [enter image description here]enter image description here.

and this is how the data look like enter image description here

I want to forecast all the time series in this data using LSTM, but there is 31 lattitaude and 19 longitude so there is a lot of data, so instead of making model for every point of longitude and lattiude I intent to use a sample. but how do I took sample of this data

$\endgroup$

1 Answer 1

2
$\begingroup$

This worked for me

import numpy as np
import xarray as xr
import pandas as pd
import tensorflow as tf
import keras
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense,Dropout

ds = xr.open_datset("your_file")
ds = ds.stack(z=("latitude","longitude"))
data_variable = "your_variable"
data = np.array(ds.data_variable.values)
data = data.T
data = data.reshape(data.shape[0],data.shape[1],1)
time_steps = data.shape[1]-1

X_train, X_test, y_train, y_test = train_test_split(data[:, :-1, :], data[:, -1, :], test_size=0.2)

Normalize the data

scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train.reshape(-1, 1)).reshape(X_train.shape)
X_test = scaler.transform(X_test.reshape(-1, 1)).reshape(X_test.shape)
y_train = scaler.fit_transform(y_train.reshape(-1, 1)).reshape(y_train.shape)
y_test = scaler.transform(y_test.reshape(-1, 1)).reshape(y_test.shape)

Build LSTM model

model = Sequential()
model.add(LSTM(units=100, input_shape=(time_steps, 1),return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units = 100))
model.add(Dropout(0.2))

model.add(Dense(units=1))

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

Train the model

model.fit(X_train, y_train, epochs=50, batch_size=32)

Evaluate the model

loss = model.evaluate(X_test, y_test)
print("Test loss:", loss)

Make predictions

predictions = model.predict(X_test)

Rescale predictions and actual values to original scale

predictions = scaler.inverse_transform(predictions.reshape(-1, 1)).flatten()
actual_values = scaler.inverse_transform(y_test.reshape(-1, 1)).flatten()

Print predictions and actual values

rmse = np.sqrt(((predictions-actual_values)**2).mean())
rmse
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.