Beispiel #1
0
    def fit(self, X, y=None, subsample=None):
        """Fit the model with a time series X

        Parameters
        ----------
        X : array-like, shape (n_timepoints, n_features)
            Training data, where n_timepoints is the number of timepoints
            and n_features is the number of features.

        y : None
            Ignored variable.

        subsample : int or None
            If set to an integer, a random number of timepoints is selected
            equal to that integer

        Returns
        -------
        X_new : array-like, shape (n_timepoints, n_components)
            Transformed values.
        """
        # Make hankel matrix from dataset
        Xs = standardize_ts(X)
        X_train = hankel_matrix(Xs, self.time_window)

        if subsample:
            self.train_indices, X_train = resample_dataset(
                X_train, subsample, random_state=self.random_state)
        if self.time_lag > 0:
            self.model.fit([np.reshape(X_train, (X_train.shape[0], -1))])
        else:
            self.model.fit(np.reshape(X_train, (X_train.shape[0], -1)))
Beispiel #2
0
    def fit(self, X, y=None, verbose=False, bins=None, timescale=None):
        """Fit the model with a time series X

        Parameters
        ----------
        X : array-like, shape (n_timepoints, n_features)
            Training data, where n_timepoints is the number of timepoints
            and n_features is the number of features.

        y : None
            Ignored variable.

        Returns
        -------
        X_new : array-like, shape (n_timepoints, n_components)
            Transformed values.
        """
        Xs = standardize_ts(X)

        if not self.lag_cutoff:
            self.lag_cutoff = int(np.floor(len(Xs) / 2))
        lagged_mi_vals = self._mutual_information_lagged(
            Xs, self.lag_cutoff, bins)
        if verbose:
            plt.plot(lagged_mi_vals)

        lag_times = self._find_minima(lagged_mi_vals, timescale)[0]

        lag_time = lag_times[0]

        self.lag_time = lag_time  # overrides constant
Beispiel #3
0
 def transform(self, X, y=None):
     X_test = hankel_matrix(standardize_ts(X), self.time_window)
     X_test = np.reshape(X_test, (X_test.shape[0], -1))
     if self.time_lag > 0:
         X_new = self.model.transform([X_test])[0]
     else:
         X_new = self.model.transform(X_test)
     return X_new
Beispiel #4
0
 def transform(self, X, y=None):
     tau = self.time_window*self.lag_time
     X_test = hankel_matrix(standardize_ts(X), 
                            q = tau,
                            p = len(X) - tau
                           )
     X_test = X_test[:, ::self.lag_time, :]
     return np.squeeze(X_test)
Beispiel #5
0
 def transform(self, X, y=None):
     X_test = hankel_matrix(standardize_ts(X), self.time_window)
     X_new = self.model.encoder.predict(X_test)
     return X_new
Beispiel #6
0
    def fit(self,
            X,
            y=None,
            subsample=None,
            tau=0,
            learning_rate=1e-3,
            batch_size=100,
            train_steps=200,
            loss='mse',
            verbose=0,
            optimizer="adam",
            early_stopping=False):
        """Fit the model with a time series X

        Parameters
        ----------
        X : array-like, shape (n_timepoints, n_features)
            Training data, where n_samples is the number of samples
            and n_features is the number of features.

        y : None
            Ignored variable.

        subsample : int or None
            If set to an integer, a random number of timepoints is selected
            equal to that integer
            
        tau : int
            The prediction time, or the number of timesteps to skip between 
            the input and output time series


        Returns
        -------
        X_new : array-like, shape (n_timepoints, n_components)
            Transformed values.
        """
        # Make hankel matrix from dataset
        Xs = standardize_ts(X)

        # X_train = hankel_matrix(Xs, self.time_window)
        # Split the hankel matrix for a prediction task
        X0 = hankel_matrix(Xs, self.time_window + tau)
        X_train = X0[:, :self.time_window]
        Y_train = X0[:, -self.time_window:]

        if subsample:
            self.train_indices, _ = resample_dataset(
                X_train, subsample, random_state=self.random_state)
            X_train = X_train[self.train_indices]
            Y_train = Y_train[self.train_indices]

        optimizers = {
            "adam": tf.keras.optimizers.Adam(lr=learning_rate),
            "nadam": tf.keras.optimizers.Nadam(lr=learning_rate)
            # "radam": tfa.optimizers.RectifiedAdam(lr=learning_rate),
        }

        tf.random.set_seed(self.random_state)
        np.random.seed(self.random_state)
        self.model.compile(
            optimizer=optimizers[optimizer],
            loss=loss,
            #experimental_run_tf_function=False
        )

        if early_stopping:
            callbacks = [
                tf.keras.callbacks.EarlyStopping(monitor='loss',
                                                 mode='min',
                                                 patience=3)
            ]
        else:
            callbacks = [None]

        self.train_history = self.model.fit(x=tf.convert_to_tensor(X_train),
                                            y=tf.convert_to_tensor(Y_train),
                                            epochs=train_steps,
                                            batch_size=batch_size,
                                            verbose=verbose)