How to use the pyts.preprocessing.StandardScaler function in pyts

To help you get started, we’ve selected a few pyts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github johannfaouzi / pyts / pyts / bag_of_words / bow.py View on Github external
"""
        X = check_array(X, dtype='float64')
        n_samples, n_timestamps = X.shape
        window_size, word_size, window_step, alphabet = self._check_params(
            n_timestamps)
        n_windows = (n_timestamps - window_size + window_step) // window_step

        # Extract subsequences using a sliding window
        X_window = _windowed_view(
            X, n_samples, n_timestamps, window_size, window_step
        ).reshape(n_samples * n_windows, window_size)

        # Create a pipeline with three steps: standardization, PAA, SAX
        pipeline = make_pipeline(
            StandardScaler(
                with_mean=self.norm_mean, with_std=self.norm_std
            ),
            PiecewiseAggregateApproximation(
                window_size=None, output_size=word_size,
                overlapping=self.overlapping
            ),
            SymbolicAggregateApproximation(
                n_bins=self.n_bins, strategy=self.strategy,
                alphabet=self.alphabet
            )
        )
        X_sax = pipeline.fit_transform(X_window).reshape(
            n_samples, n_windows, word_size)

        # Join letters to make words
        X_word = np.asarray([[''.join(X_sax[i, j])
github johannfaouzi / pyts / pyts / approximation / approximation.py View on Github external
"""
        # Check fitted
        check_is_fitted(self, ['coefs_', '_n_features'])

        # Check X
        X = check_array(X)
        if X.shape[1] != self._n_features:
            raise ValueError("The length of each time series (X.shape[1]) "
                             "does not match the length of each time series "
                             "when the estimator was fitted.")

        n_samples = X.shape[0]

        # Normalization
        ss = StandardScaler(self.norm_mean, self.norm_std)
        X = ss.fit_transform(X)

        # Fast Fourier Transform
        X_fft = np.fft.fft(X)
        X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
        X_fft = X_fft.reshape(n_samples, -1, order='F')

        return X_fft[:, self.coefs_]
github johannfaouzi / pyts / pyts / approximation / dft.py View on Github external
Class labels for each data sample. Only used if ``anova=True``.

        Returns
        -------
        self : object

        """
        if self.anova:
            X, y = check_X_y(X, y, dtype='float64')
        else:
            X = check_array(X, dtype='float64')

        n_samples, n_timestamps = X.shape
        n_coefs = self._check_params(n_timestamps)
        if self.anova:
            ss = StandardScaler(self.norm_mean, self.norm_std)
            X = ss.fit_transform(X)
            X_fft = np.fft.rfft(X)
            X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
            if n_timestamps % 2 == 0:
                X_fft = X_fft.reshape(n_samples, n_timestamps + 2, order='F')
                X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:-1]]
            else:
                X_fft = X_fft.reshape(n_samples, n_timestamps + 1, order='F')
                X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:]]
            if self.drop_sum:
                X_fft = X_fft[:, 1:]
            self.support_ = self._anova(X_fft, y, n_coefs, n_timestamps)
        else:
            self.support_ = np.arange(n_coefs)
        return self
github johannfaouzi / pyts / pyts / approximation / approximation.py View on Github external
if not isinstance(self.anova, (int, float)):
            raise TypeError("'anova' must be a boolean.")
        if (not self.anova) and isinstance(self.n_coefs, int):
            if self.n_coefs % 2 != 0:
                raise ValueError("'n_coefs' must be an even integer.")

        if not self.anova:
            X = check_array(X)
        else:
            X, y = check_X_y(X, y)

        n_samples, n_features = X.shape
        self._n_features = n_features

        # Normalization
        ss = StandardScaler(self.norm_mean, self.norm_std)
        X = ss.fit_transform(X)

        X_fft = np.fft.fft(X)
        X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
        X_fft = X_fft.reshape(n_samples, -1, order='F')

        if not self.anova:
            if self.n_coefs is None:
                self.coefs_ = np.arange(2 * self._n_features)
            else:
                if self.norm_mean:
                    self.coefs_ = np.arange(2, self.n_coefs + 2)
                else:
                    self.coefs_ = np.arange(self.n_coefs)
        else:
            self.coefs_ = self._anova_selection(X_fft, y)
github johannfaouzi / pyts / pyts / approximation / dft.py View on Github external
Class labels for each data sample.

        Returns
        -------
        X_new : array, shape (n_samples, n_coefs)
            The selected Fourier coefficients for each sample.

        """
        if self.anova:
            X, y = check_X_y(X, y, dtype='float64')
        else:
            X = check_array(X, dtype='float64')
        n_samples, n_timestamps = X.shape
        n_coefs = self._check_params(n_timestamps)

        scaler = StandardScaler(self.norm_mean, self.norm_std)
        X = scaler.fit_transform(X)
        X_fft = np.fft.rfft(X)
        X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
        if n_timestamps % 2 == 0:
            X_fft = X_fft.reshape(n_samples, n_timestamps + 2, order='F')
            X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:-1]]
        else:
            X_fft = X_fft.reshape(n_samples, n_timestamps + 1, order='F')
            X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:]]
        if self.drop_sum:
            X_fft = X_fft[:, 1:]
        if self.anova:
            self.support_ = self._anova(X_fft, y, n_coefs, n_timestamps)
        else:
            self.support_ = np.arange(n_coefs)
        return X_fft[:, self.support_]
github johannfaouzi / pyts / pyts / approximation / dft.py View on Github external
Parameters
        ----------
        X : array-like, shape (n_samples, n_timestamps)
            Input data.

        Returns
        -------
        X_new : array, shape (n_samples, n_coefs)
            The selected Fourier coefficients for each sample.

        """
        check_is_fitted(self, 'support_')
        X = check_array(X, dtype='float64')
        n_samples, n_timestamps = X.shape

        ss = StandardScaler(self.norm_mean, self.norm_std)
        X = ss.fit_transform(X)
        X_fft = np.fft.rfft(X)
        X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
        if n_timestamps % 2 == 0:
            X_fft = X_fft.reshape(n_samples, n_timestamps + 2, order='F')
            X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:-1]]
        else:
            X_fft = X_fft.reshape(n_samples, n_timestamps + 1, order='F')
            X_fft = np.c_[X_fft[:, 0], X_fft[:, 2:]]
        if self.drop_sum:
            X_fft = X_fft[:, 1:]
        return X_fft[:, self.support_]
github johannfaouzi / pyts / examples / preprocessing / plot_scalers.py View on Github external
import numpy as np
import matplotlib.pyplot as plt
from pyts.preprocessing import (StandardScaler, MinMaxScaler,
                                MaxAbsScaler, RobustScaler)

# Parameters
n_samples, n_timestamps = 100, 48
marker_size = 5

# Toy dataset
rng = np.random.RandomState(41)
X = rng.randn(n_samples, n_timestamps)

# Scale the data with different scaling algorithms
X_standard = StandardScaler().transform(X)
X_minmax = MinMaxScaler(sample_range=(0, 1)).transform(X)
X_maxabs = MaxAbsScaler().transform(X)
X_robust = RobustScaler(quantile_range=(25.0, 75.0)).transform(X)

# Show the results for the first time series
plt.figure(figsize=(16, 6))

ax1 = plt.subplot(121)
ax1.plot(X[0], 'o-', ms=marker_size, label='Original')
ax1.set_title('Original time series', fontsize=16)
ax1.legend(loc='best', fontsize=12)

ax2 = plt.subplot(122)
ax2.plot(X_standard[0], 'o--', ms=marker_size, color='C1',
         label='StandardScaler')
ax2.plot(X_minmax[0], 'o--', ms=marker_size, color='C2', label='MinMaxScaler')
github johannfaouzi / pyts / pyts / approximation / approximation.py View on Github external
raise TypeError("'anova' must be a boolean.")
        if (not self.anova) and isinstance(self.n_coefs, int):
            if self.n_coefs % 2 != 0:
                raise ValueError("If 'anova' = False, 'n_coefs' must be an "
                                 "even integer.")

        if not self.anova:
            X = check_array(X)
        else:
            X, y = check_X_y(X, y)

        n_samples, n_features = X.shape
        self._n_features = n_features

        # Normalization
        ss = StandardScaler(self.norm_mean, self.norm_std)
        X = ss.fit_transform(X)

        if not self.anova:
            if self.n_coefs is None:
                self.coefs_ = np.arange(2 * self._n_features)
            else:
                if self.norm_mean:
                    self.coefs_ = np.arange(2, self.n_coefs + 2)
                else:
                    self.coefs_ = np.arange(self.n_coefs)
        else:
            X_fft = np.fft.fft(X, axis=0)
            X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
            X_fft = X_fft.reshape(n_samples, -1, order='F')
            self.coefs_ = self._anova_selection(X_fft, y)