데이터과학 삼학년

Recurrence Plot (feat. pyts - Imaging time series) 본문

Time Series Analysis

Recurrence Plot (feat. pyts - Imaging time series)

Dan-k 2020. 12. 18. 15:51
반응형

pyts 라이브러리는 시계열 데이터를 분석하기 위한 여러 방법 툴을 가지고 있다.

시계열 데이터를 이미지화 시키는 방법 3가지에 대해 다뤄본다.

 

1. Recurrence Plot

2. Gramian Angular Field

3. Markov Transition Field

 

 

 

In [1]:
# !pip install pyts
 

Imaging time series

1. Recurrence Plot

  • Recurrence Plot은 궤적자료를 추출한 다음, 궤적사이의 pairwise 거리를 계산하여 plot으로 나타낸 것
  • $$ \vec{x}_i = (x_i, x_{i + \tau}, \ldots, x_{i + (m - 1)\tau}), \quad \forall i \in \{1, \ldots, n - (m - 1)\tau \}$$
  • 여기서, m은 궤적의 차원을 타내내고 $\tau$ (타우)는 Time_delay를 의미함
  • $$ R_{i, j} = \Theta(\varepsilon - \| \vec{x}_i - \vec{x}_j \|), \quad \forall i,j \in \{1, \ldots, n - (m - 1)\tau \}$$
  • $\Theta$는 Heaviside function 이고 $\varepsilon$ 는 threshold 이다 -> theshold를 사용하여 다른 전략으로 RP plot을 구성할 수 있음
In [2]:
from pyts.datasets import load_gunpoint
from pyts.image import RecurrencePlot
X, _, _, _ = load_gunpoint(return_X_y=True)
transformer = RecurrencePlot(
                dimension=1,
                time_delay=1,
                threshold='point',
                percentage=10,
                flatten=False )
X_new = transformer.transform(X)
X_new.shape
Out[2]:
(50, 150, 150)
In [3]:
# Show the results for the first time series
import matplotlib.pyplot as plt

plt.figure(figsize=(5, 5))
plt.imshow(X_new[1], cmap='binary', origin='lower')
plt.title('Recurrence Plot', fontsize=16)
plt.tight_layout()
plt.show()
 
In [4]:
from pyts.image import RecurrencePlot
from pyts.datasets import load_gunpoint
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
recurrence = RecurrencePlot(dimension=15, time_delay=3, flatten=True)
logistic = LogisticRegression(solver='liblinear')
clf = make_pipeline(recurrence, logistic)
clf.fit(X_train, y_train)

clf.score(X_test, y_test)
Out[4]:
0.9333333333333333
 

2. Gramian Angular Field

  • GramianAngularField 는 $(x_i, x_j)$ 의 상관계수 matrix 를 구성함.

  • $[a, b]$ 범위로 시계열 데이터를 rescaling 함 ($-1 \leq a < b \leq 1$)

  • the polar coordinates 계산함(arcos 취하여서)
  • 각도 합을 코사인이나 사인값으로 변형
    • 각도 합의 코사인값을 계산 (the cosine of the sum of the angles for the Gramian Angular Summation Field (GASF)
    • 사인값을 계산 (the sine of the difference of the angles for the Gramian Angular Difference Field (GADF).)

$$ \tilde{x}_i = a + (b - a) \times \frac{x_i - \min(x)}{\max(x) - \min(x)}, \quad \forall i \in \{1, \ldots, n\}$$

$$\phi_i = \arccos(\tilde{x}_i), \quad \forall i \in \{1, \ldots, n\}$$

$$GASF_{i, j} = \cos(\phi_i + \phi_j), \quad \forall i, j \in \{1, \ldots, n\}$$

$$GADF_{i, j} = \sin(\phi_i - \phi_j), \quad \forall i, j \in \{1, \ldots, n\}$$

  • The method parameter controls which type of Gramian angular fields are computed.
In [5]:
from pyts.datasets import load_gunpoint
from pyts.image import GramianAngularField
X, _, _, _ = load_gunpoint(return_X_y=True)
transformer = GramianAngularField()
X_new = transformer.transform(X)
X_new.shape
Out[5]:
(50, 150, 150)
In [6]:
# Show the results for the first time series
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid


gasf = GramianAngularField(image_size=24, method='summation')
X_gasf = gasf.fit_transform(X)
gadf = GramianAngularField(image_size=24, method='difference')
X_gadf = gadf.fit_transform(X)
# Show the images for the first time series
fig = plt.figure(figsize=(8, 4))
grid = ImageGrid(fig, 111,
                 nrows_ncols=(1, 2),
                 axes_pad=0.15,
                 share_all=True,
                 cbar_location="right",
                 cbar_mode="single",
                 cbar_size="7%",
                 cbar_pad=0.3,
                 )
images = [X_gasf[0], X_gadf[0]]
titles = ['Summation', 'Difference']
for image, title, ax in zip(images, titles, grid):
    im = ax.imshow(image, cmap='rainbow', origin='lower')
    ax.set_title(title, fontdict={'fontsize': 12})
ax.cax.colorbar(im)
ax.cax.toggle_label(True)
plt.suptitle('Gramian Angular Fields', y=0.98, fontsize=16)
plt.show()
 
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:26: MatplotlibDeprecationWarning: Since 3.2, mpl_toolkits's own colorbar implementation is deprecated; it will be removed two minor releases later.  Set the 'mpl_toolkits.legacy_colorbar' rcParam to False to use Matplotlib's default colorbar implementation and suppress this deprecation warning.
/opt/conda/lib/python3.7/site-packages/mpl_toolkits/axes_grid1/axes_grid.py:51: MatplotlibDeprecationWarning: 
The mpl_toolkits.axes_grid1.colorbar module was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use matplotlib.colorbar instead.
  from .colorbar import Colorbar
 
In [7]:
from pyts.image import GramianAngularField
from pyts.datasets import load_gunpoint
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
gaf = GramianAngularField(flatten=True)
logistic = LogisticRegression(solver='liblinear')
clf = make_pipeline(gaf, logistic)
clf.fit(X_train, y_train)

clf.score(X_test, y_test)
Out[7]:
0.9733333333333334
 

3. Markov Transition Field

  • MarkovTransitionField 는 bins을 만들어 데이터를 이산화 시킨다.(iscretizes a time series into bins.)
  • Markov Transition Matrix 를 계산
  • 변이된 matrix를 field에 뿌림 --> 임시정보의 손실을 줄이기 위해 (Finally it spreads out the transition matrix to a field in order to reduce the loss of temporal information.)
In [8]:
from pyts.datasets import load_gunpoint
from pyts.image import MarkovTransitionField
X, _, _, _ = load_gunpoint(return_X_y=True)
transformer = MarkovTransitionField()
X_new = transformer.transform(X)
X_new.shape
Out[8]:
(50, 150, 150)
In [9]:
# Show the results for the first time series
import matplotlib.pyplot as plt

plt.figure(figsize=(5, 5))
plt.imshow(X_new[1], cmap='rainbow', origin='lower')
plt.title('Recurrence Plot', fontsize=18)
plt.colorbar(fraction=0.0457, pad=0.04)
plt.tight_layout()
plt.show()
 
In [10]:
from pyts.image import MarkovTransitionField
from pyts.datasets import load_gunpoint
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
mtf = MarkovTransitionField(image_size=0.1, n_bins=3, flatten=True)
logistic = LogisticRegression(solver='liblinear')
clf = make_pipeline(mtf, logistic)
clf.fit(X_train, y_train)

clf.score(X_test, y_test)
Out[10]:
0.92
728x90
반응형
LIST
Comments