-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
194 lines (179 loc) · 6.8 KB
/
features.py
File metadata and controls
194 lines (179 loc) · 6.8 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'''Расчет характеристик временного ряда:
мера шума, размерность вложения, корреляционные размерность и энтропия,
показатель Херста, энтропия Колмогорова-синая'''
import numpy as np
import pandas as pd
import ctypes
from os.path import split
from inspect import getfile
from scipy.stats import entropy
from scipy.linalg import hankel
from scipy.spatial import KDTree
from sklearn.preprocessing import MinMaxScaler
from itertools import combinations
import warnings
import Libraries
import gc
from Libraries.Util import Norm01, Nback, MLS
'''мера шумности по ско разностей к ско ряда. (больше мера - меньше шума)'''
def NoiseFactor(data, axis=0, ddof=1):
a = Norm01(data)[0]
m = np.std(pd.Series(a).diff().dropna().abs())
sd = a.std(axis=axis, ddof=ddof)
return 1-float(np.where(sd == 0, 0, m/sd))
'''Оценка случайного блуждания'''
def RandWalk(ser):
return abs(NoiseFactor(pd.Series(ser).diff().fillna(method='bfill')))
'''Размерность вложения по корреляционному интегралу'''
def DimEmb(tser, eps=.1):
ser,_,_=Norm01(tser)
n=len(ser)
cn=[1]
d0=0
h=hankel(ser)
for k in range(2,n//2):#
ent=sum(cn)
w=h[:n-k, :k]
ro=np.zeros([n-k, n-k])
for i,j in combinations(np.arange(n-k), 2):
norm=np.linalg.norm(w[i]-w[j])
ro[i,j]=norm
ro[j,i]=norm
cl=[]
cn=[]
ls=np.linspace(ro[ro!=0].min(), ro.max(), num=20)
for l in ls:
c=np.heaviside(l-ro-np.diag(np.ones(n-k)),1).sum()//2
cn.append(c/(n-k)**2)
cl.append(np.log(c/(n-k)**2))
dc=(cl[1]-cl[0])/(np.log(ls[1])-np.log(ls[0]))
if abs(dc-d0) > eps: # (ro.max() - ro.min())/50.:
d0=dc
else:
break
k-=1
dc=d0
ent=sum(cn)/ent
return k, dc, ent #k - размерность вложения, dc - корреляционная размерность, ent - оценка энтропии.
def CEmbDim(dat): #То же по-быстрому с C++ процедурой EmbDim.so'
nw=1000
if len(dat)>1000:
y=dat[-1000:]
else:
y=dat
emd = ctypes.CDLL(split(getfile(Libraries.Util))[0]+'/EmbDim.so')
emd.EmbDim.restype = ctypes.c_int
emd.EmbDim.argtypes = [ctypes.c_double*nw, ctypes.c_int]
s=list(Norm01(y)[0])+[0.]*(nw-len(y))
arr = (ctypes.c_double*nw)(*s)
return emd.EmbDim(arr, len(y))
def CCorrent(dat): #Корреляционная энтропия по-быстрому с C++ процедурой CorrEntr.cpp
nw=1000
if len(dat)>1000:
y=dat[-1000:]
else:
y=dat
emd = ctypes.CDLL(split(getfile(Libraries.Util))[0]+'/CorrEntr.so')
emd.CorrEntr.restype = ctypes.c_double
emd.CorrEntr.argtypes = [ctypes.c_double*nw, ctypes.c_int]
s=list(Norm01(y)[0])+[0.]*(nw-len(y))
arr = (ctypes.c_double*nw)(*s)
centr=emd.CorrEntr(arr, len(y))
del(arr)
del(emd)
ctypes._reset_cache()
gc.collect()
return centr
'''Показатель Хёрста (R/S и H траектории)'''
def HurstTraj(ser): #RS-trajectory of Hurst
h=[]
z2,_,_=Norm01(ser)
tau=np.arange(3,len(z2))
for t in tau:
m,s=np.mean(z2[:t]), np.std(z2[:t])
x=(z2[:t]-m).cumsum()
r=max(x)-min(x)
h.append(np.log(r/s) if r*s > 0. else 0.)
h=np.array(h)
t=np.array([0.])
t=np.concatenate([t, np.log(tau[1:]/2)])
l=int(len(t)/50)
he,b = MLS(t[:l],h[:l])
mem=np.where([(h[i+1]-h[i])<0. for i in range(len(h)-1)])[0]
mem=mem[0] if len(mem) else 0
return t,h,he,mem #t-ln(tau); h - R/S trajectory (Hurst's tr=h/t); he - Hurst's exponent; mem - series' memory
def CHurst(dat): #То же по-быстрому с C++ процедурой HurstExp.so
nw=1000
if len(dat)>1000:
y=dat[-1000:]
else:
y=dat
he = ctypes.CDLL(split(getfile(Libraries.Util))[0]+'/HurstExp.so')
he.HurstExp.restype = ctypes.c_double
he.HurstExp.argtypes = [ctypes.c_double*nw, ctypes.c_int]
s=list(y)+[0.]*(nw-len(y))
arr = (ctypes.c_double*nw)(*s)
return he.HurstExp(arr, len(y))
'''Kolmogorov-Sinai Entropy'''
def ksent(time_series, k=5, tau=1, eps=1e-10):
"""
Вычисляет энтропию Колмогорова-Синая для временного ряда.
Параметры:
- time_series: одномерный временной ряд (numpy array).
- k: количество ближайших соседей (по умолчанию 3).
- tau: временной лаг для построения вложений (по умолчанию 1).
- eps: маленькое значение для избежания деления на ноль (по умолчанию 1e-10).
Возвращает:
- ks_entropy: оценка энтропии Колмогорова-Синая.
"""
n = len(time_series)
m = n - (k - 1) * tau # количество вложений
# Создаем вложения (embedding)
embeddings = np.array([time_series[i:i + k * tau:tau] for i in range(m)])
# Используем KDTree для поиска ближайших соседей
tree = KDTree(embeddings)
distances = []
for i in range(m):
# Ищем k+1 ближайших соседей (включая саму точку)
dist, _ = tree.query(embeddings[i], k + 1)
distances.append(dist[-1]) # берем расстояние до k-го соседа
distances = np.array(distances)
# Вычисляем KS энтропию
ks_entropy = np.mean(np.log(distances + eps)) / tau
return ks_entropy
'''Колмогоровская сложность по оценке Лемпеля — Зива'''
def LempelZiv(S):
n=len(S)
i=0
C=u=v=vmax=1
while (u+v)<n:
if S[i+v] == S[u+v]:
v+=1
else:
vmax = max(v, vmax)
i+=1
v=1
if i==u:
C+=1
u+=vmax
i=0
vmax=v
else:
v=1
if v!=1:
C+=1
return C
'''энтропия ряда по Шеннону'''
def ShEntr(data, bin=25):
hist,bins=np.histogram(data, bins=bin)
return entropy(hist/len(data), base=2)
'''Всё вместе в словарь'''
def get_features(ser):
warnings.filterwarnings('ignore')
features={}
features['noise']=NoiseFactor(ser, axis=0, ddof=1)
features['hurst']=СНurst(ser) #HurstTraj(ser)[2]
features['coent']=DimEmb(ser)[2]
features['ksent']=KSEntr(ser)
features['randm']=RandWalk(ser)
return features