The world’s leading publication for data science, AI, and ML professionals.

Time Series for Climate Change: Using Deep Learning for Precision Agriculture

How to use time series analysis and forecasting to tackle climate change

Photo by Irewolede on Unsplash
Photo by Irewolede on Unsplash

This is Part 6 of the series Time Series for Climate Change. List of articles:


Precision Agriculture

Precision agriculture aims at improving agriculture management. To optimize production while saving resources and minimizing environmental impact.

There are several technologies devoted to improving the sustainability of agriculture. Examples include:

  • Smart irrigation systems: using sensors to optimize the irrigation process;
  • Precision planting systems: optimizing the planting process, such as the spacing between seeds;
  • Crop yield prediction, which helps farmers decide what to plant each season.

Dew point temperature

Photo by Simon Kuznetsov on Unsplash
Photo by Simon Kuznetsov on Unsplash

In the case of irrigation systems, low irrigation increases plant stress and reduces crop yields. But, too much irrigation leads to excessive moisture that fosters pests. An optimal irrigation process can save large amounts of water and preserve other resources.

One indicator that influences irrigation is dew point temperature. The dew point temperature indicates how much water is in the air. This, in turn, influences the irrigation process. So, forecasting the dew point is useful to support water resource planning.

The relevance of dew point temperature spans other activities in hydrological, climate, and agricultural domains. For example, if forecasts indicate the possibility of excessive moisture, pest control treatments can be applied proactively. Forecasts can also be used to anticipate frosts, which are harmful to plants. In this case, farmers can adopt preemptive measures to protect crops.

Forecasting dew point temperature is also important in the context of energy management. In high dew point temperatures, people tend to use air conditioning systems to cope with high relative humidity levels. Forecasting these conditions can be used to predict energy demand increases. This helps improve the efficiency of the electricity grid.


Hands-on: Spatio-Temporal Forecasting of Dew Point Temperature using Deep Learning

In the rest of this article, we’ll forecast dew point temperature in several locations. You’ll learn how to build a spatio-temporal forecasting model using deep learning.

The full code for this tutorial is available on Github:

Primer on Spatio-Temporal Forecasting

Spatio-temporal data is a specific case of multivariate time series. These data sets involve observing a variable, such as dew point temperature, in several locations.

This type of data contains both a temporal dependency and a spatial dependency. A data point collected in a particular location is correlated with its lags and the current and past lags of nearby locations. Modeling both dependencies can be important to get better forecasts in each location.

Spatio-temporal forecasting is usually done with techniques such as VAR (vector auto-regression) or STAR (spatio-temporal auto-regression). We’ll use a VAR approach with a deep neural network.

Data set

We’ll use a real-world data set that is collected by the U.S. Department of Agriculture. More details are available in reference [1]. Among other things, the dataset contains information about the dew point temperature. This variable is captured in 6 nearby stations.

After downloading the data, you can read it using the following code:

import pandas as pd

DATE_TIME_COLS = ['month', 'day', 'calendar_year', 'hour', 'water_year']

# reading the data set
data = pd.read_csv(filepath)

# parsing the datetime column
data['datetime'] = 
    pd.to_datetime([f'{year}/{month}/{day} {hour}:00'
                    for year, month, day, hour in zip(data['calendar_year'],
                                                      data['month'],
                                                      data['day'],
                                                      data['hour'])])

data = data.drop(DATE_TIME_COLS, axis=1).set_index('datetime')
data.columns = data.columns.str.replace('_dpt_C', '')

Here’s what the data looks like:

The time series in the different locations appear to be correlated.

VAR – preparing the data for supervised learning

We’ll use a VAR approach to prepare the data for training a deep neural network. VAR methods aim at capturing temporal dependencies among different variables. In this case, the variables represent the dew point temperature collected at 6 locations.

We can do this by transforming each variable into a matrix format using a sliding window and then combining the results. You can check a previous article for more details on this process.

from sklearn.model_selection import train_test_split

from src.tde import transform_mv_series

N_LAGS, HORIZON = 12, 12

# number of stations
N_STATIONS = data.shape[1]

# leaving last 20% of observations for testing
train, test = train_test_split(data, test_size=0.2, shuffle=False)

# computing the average of each series in the training set
mean_by_location = train.mean()

# mean-scaling: dividing each series by its mean value
train_scaled = train / mean_by_location
test_scaled = test / mean_by_location

# transforming the data for supervised learning
X_train, Y_train = transform_mv_series(train_scaled, n_lags=N_LAGS, horizon=HORIZON)
X_test, Y_test = transform_mv_series(test_scaled, n_lags=N_LAGS, horizon=HORIZON)

Then, we build a stacked LSTM model based on keras. An LSTM (Long Short-Term Memory) is a special type of recurrent neural network that can capture temporal dependencies.

from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, RepeatVector, TimeDistributed

model = Sequential()
model.add(LSTM(64, activation='relu', input_shape=(N_LAGS, N_STATIONS)))
model.add(Dropout(.2))
model.add(RepeatVector(HORIZON))
model.add(LSTM(32, activation='relu', return_sequences=True))
model.add(Dropout(.2))
model.add(TimeDistributed(Dense(N_STATIONS)))

model.compile(optimizer='adam', loss='mse')

After defining and compiling the model, we can train it as follows:

from keras.callbacks import ModelCheckpoint

# model checkpoint for saving the best model during training
model_checkpoint = ModelCheckpoint(
    filepath='best_model_weights.h5',
    save_weights_only=True,
    monitor='val_loss',
    mode='min',
    save_best_only=True)

# fitting the model
history = model.fit(X_train, Y_train,
                    epochs=25,
                    validation_split=0.2,
                    callbacks=[model_checkpoint])

After training, we can load the best weights that were saved by the model checkpoint callback:

# The best model weights are loaded into the model after training
model.load_weights('best_model_weights.h5')

# predictions on the test set
preds = model.predict_on_batch(X_test)

Extending the model

We could extend this model in different ways. For example, include other meteorological information as explanatory variables. Meteorological data such as the dew point temperature is affected by various factors. Their inclusion may be key for better forecasting performance.

Testing other neural network configurations could also prove beneficial. We applied a stacked LSTM, but other methods have also shown promising forecasting performance. Examples include N-BEATS, DeepAR, or ES-RNN.

We could also include spatial information, such as geographical coordinates. This way, the model could improve the modeling of spatial dependencies among the different locations.


Key Takeaways

  • Precision agriculture aims at optimizing agriculture processes. This optimization can reduce the environmental impact of agriculture;
  • The dew point temperature is a key indicator in hydrology. Forecasting this variable is beneficial for various activities, such as irrigation;
  • Meteorological variables are often captured in several stations, which leads to a spatio-temporal data set;
  • Deep learning can be used to build a spatio-temporal forecasting model. We trained an LSTM neural network using a data set that contains the dew point temperature from six different locations;
  • This model could be extended by including extra explanatory variables, spatial information, or changing the architecture of the network.

Thank you for reading, and see you in the next story!

References

[1] Weather, Snow, and Streamflow data from four western juniper-dominated Experimental Catchments in southwestern Idaho, USA. (Licence: U.S. Public Domain)

[2] Arikan, Bugrayhan Bickici, et al. "Dew point time series forecasting at the North Dakota." Knowledge-Based Engineering and Sciences 2.2 (2021): 24–34.

[3] Liakos, Konstantinos G., et al. "Machine Learning in agriculture: A review." Sensors 18.8 (2018): 2674.


Related Articles