250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 유튜브 API
- gather_nd
- BigQuery
- youtube data
- 상관관계
- tensorflow text
- requests
- 공분산
- chatGPT
- Airflow
- spark udf
- session 유지
- flask
- top_k
- Retry
- API
- TensorFlow
- login crawling
- grad-cam
- airflow subdag
- hadoop
- correlation
- UDF
- integrated gradient
- XAI
- subdag
- API Gateway
- Counterfactual Explanations
- GenericGBQException
- GCP
Archives
- Today
- Total
데이터과학 삼학년
Recurrence Plot (feat. pyts - Imaging time series) 본문
반응형
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]:
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]:
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]:
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()
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]:
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]:
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]:
728x90
반응형
LIST
'Time Series Analysis' 카테고리의 다른 글
Multivariate 시계열 데이터 LSTM 적용 케이스 예시 (3) | 2021.01.06 |
---|---|
[RPs] 시계열 데이터 이미지화 (0) | 2021.01.04 |
Prophet for python (feat. fbprophet) (0) | 2020.12.14 |
Augmented Dickey-Fuller test - 정상성 시계열 데이터 확인 방법 (0) | 2020.11.03 |
정상성과 차분 (stationarity & differencing) (0) | 2020.11.03 |
Comments