Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# License: BSD-3-Clause
import numpy as np
import matplotlib.pyplot as plt
from pyts.approximation import PiecewiseAggregateApproximation
# Parameters
n_samples, n_timestamps = 100, 48
# Toy dataset
rng = np.random.RandomState(41)
X = rng.randn(n_samples, n_timestamps)
# PAA transformation
window_size = 6
paa = PiecewiseAggregateApproximation(window_size=window_size)
X_paa = paa.transform(X)
# Show the results for the first time series
plt.figure(figsize=(6, 4))
plt.plot(X[0], 'o--', ms=4, label='Original')
plt.plot(np.arange(window_size // 2,
n_timestamps + window_size // 2,
window_size), X_paa[0], 'o--', ms=4, label='PAA')
plt.vlines(np.arange(0, n_timestamps, window_size) - 0.5,
X[0].min(), X[0].max(), color='g', linestyles='--', linewidth=0.5)
plt.legend(loc='best', fontsize=10)
plt.xlabel('Time', fontsize=12)
plt.title('Piecewise Aggregate Approximation', fontsize=16)
plt.show()
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])
for j in range(n_windows)]
for i in range(n_samples)])
Parameters
----------
X : array-like, shape = (n_samples, n_timestamps)
Returns
-------
X_new : array-like, shape = (n_samples, image_size, image_size)
Transformed data. If ``flatten=True``, the shape is
`(n_samples, image_size * image_size)`.
"""
X = check_array(X)
n_samples, n_timestamps = X.shape
image_size = self._check_params(n_timestamps)
paa = PiecewiseAggregateApproximation(
window_size=None, output_size=image_size,
overlapping=self.overlapping
)
X_paa = paa.fit_transform(X)
if self.sample_range is None:
X_min, X_max = np.min(X_paa), np.max(X_paa)
if (X_min < -1) or (X_max > 1):
raise ValueError("If 'sample_range' is None, all the values "
"of X must be between -1 and 1.")
X_cos = X_paa
else:
scaler = MinMaxScaler(sample_range=self.sample_range)
X_cos = scaler.fit_transform(X_paa)
X_sin = np.sqrt(np.clip(1 - X_cos ** 2, 0, 1))
if self.method in ['s', 'summation']:
X_new = _gasf(X_cos, X_sin, n_samples, image_size)