Time bid forecasting utilizing TensorFlow involves gathering models similar Convolutional and Recurrent Neural Networks (CNNs and RNNs). This tutorial covers forecasting for a azygous clip measurement with 1 oregon each features, arsenic good arsenic multi-step forecasting utilizing single-shot and autoregressive approaches.
Setup and Dataset
We'll usage a upwind clip bid dataset from the Max Planck Institute for Biogeochemistry. This dataset contains 14 features collected each 10 minutes betwixt 2009 and 2016.
First, import the indispensable libraries and load the data:
import tensorflow arsenic tf import pandas arsenic pd import matplotlib.pyplot arsenic plt import numpy arsenic np zip_path = tf.keras.utils.get_file( origin='https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip', fname='jena_climate_2009_2016.csv.zip', extract=True) csv_path, _ = os.path.splitext(zip_path) df = pd.read_csv(csv_path) df = df[5::6] # Subsample to hourly data date_time = pd.to_datetime(df.pop('Date Time'), format='%d.%m.%Y %H:%M:%S')
Let's visualize immoderate of the features:
plot_cols = ['T (degC)', 'p (mbar)', 'rho (g/m**3)'] plot_features = df[plot_cols] plot_features.index = date_time plot_features.plot(subplots=True)
Data Cleaning and Feature Engineering
Clean the upwind velocity data:
wv = df['wv (m/s)'] bad_wv = wv == -9999.0 wv[bad_wv] = 0.0 max_wv = df['max. wv (m/s)'] bad_max_wv = max_wv == -9999.0 max_wv[bad_max_wv] = 0.0
Convert upwind absorption and velocity to upwind vectors:
wv = df.pop('wv (m/s)') max_wv = df.pop('max. wv (m/s)') wd_rad = df.pop('wd (deg)') * np.pi / 180 df['Wx'] = wv * np.cos(wd_rad) df['Wy'] = wv * np.sin(wd_rad) df['max Wx'] = max_wv * np.cos(wd_rad) df['max Wy'] = max_wv * np.sin(wd_rad)
Create time-based features:
timestamp_s = date_time.map(pd.Timestamp.timestamp) day = 24*60*60 year = (365.2425)*day df['Day sin'] = np.sin(timestamp_s * (2 * np.pi / day)) df['Day cos'] = np.cos(timestamp_s * (2 * np.pi / day)) df['Year sin'] = np.sin(timestamp_s * (2 * np.pi / year)) df['Year cos'] = np.cos(timestamp_s * (2 * np.pi / year))
Data Splitting and Normalization
Split the information into training, validation, and trial sets:
n = len(df) train_df = df[0:int(n*0.7)] val_df = df[int(n*0.7):int(n*0.9)] test_df = df[int(n*0.9):]
Normalize the data:
train_mean = train_df.mean() train_std = train_df.std() train_df = (train_df - train_mean) / train_std val_df = (val_df - train_mean) / train_std test_df = (test_df - train_mean) / train_std
Data Windowing
Create a WindowGenerator people to grip information windowing:
class WindowGenerator(): def __init__(self, input_width, label_width, shift, train_df=train_df, val_df=val_df, test_df=test_df, label_columns=None): # Initialize model parameters self.train_df = train_df self.val_df = val_df self.test_df = test_df self.label_columns = label_columns self.input_width = input_width self.label_width = label_width self.shift = shift # Calculate indices for input and statement windows self.total_window_size = input_width + shift self.input_slice = slice(0, input_width) self.input_indices = np.arange(self.total_window_size)[self.input_slice] self.label_start = self.total_window_size - self.label_width self.labels_slice = slice(self.label_start, None) self.label_indices = np.arange(self.total_window_size)[self.labels_slice] def split_window(self, features): inputs = features[:, self.input_slice, :] labels = features[:, self.labels_slice, :] if self.label_columns is not None: labels = tf.stack( [labels[:, :, self.column_indices[name]] for sanction successful self.label_columns], axis=-1) instrumentality inputs, labels def make_dataset(self, data): information = np.array(data, dtype=np.float32) ds = tf.keras.utils.timeseries_dataset_from_array( data=data, targets=None, sequence_length=self.total_window_size, sequence_stride=1, shuffle=True, batch_size=32,) ds = ds.map(self.split_window) instrumentality ds
This WindowGenerator people allows for flexible instauration of input-label pairs for assorted forecasting tasks.
Conclusion
With these preprocessing steps and the WindowGenerator class, we've laid the groundwork for gathering clip bid forecasting models utilizing TensorFlow. The adjacent steps would impact creating and grooming models for single-step and multi-step forecasting tasks.
- Chollet F. Deep Learning with Python. Manning Publications; 2017.
- Géron A. Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow. O'Reilly Media; 2019.