forked from fspinna/TS_AgnosticLocalExplainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyutils.py
76 lines (61 loc) · 2.58 KB
/
myutils.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 13 17:15:54 2019
@author: francesco
"""
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
import fastdtw
def reconstruction_blackbox_consistency(autoencoder, blackbox, dataset, keras = True, discriminative = False):
if keras:
if discriminative:
decoded_tss = autoencoder.predict(dataset)[0]
else:
decoded_tss = autoencoder.predict(dataset)
y_exp_pred = blackbox.predict(dataset)
y_exp_recon_pred = blackbox.predict(decoded_tss)
return accuracy_score(np.argmax(y_exp_pred, axis = 1), np.argmax(y_exp_recon_pred, axis = 1))
else:
# for KNN
if discriminative:
decoded_tss = autoencoder.predict(dataset)[0]
else:
decoded_tss = autoencoder.predict(dataset)
y_exp_pred = blackbox.predict(dataset.reshape(dataset.shape[:2]))
y_exp_recon_pred = blackbox.predict(decoded_tss.reshape(decoded_tss.shape[:2]))
return accuracy_score(y_exp_pred, y_exp_recon_pred)
def df_to_sktime(df):
# 2d dataframe to 2d sktime dataframe
df_dict = {"dim_0": []}
for series in df:
df_dict["dim_0"].append(pd.Series(series))
return pd.DataFrame(df_dict)
def dtw_distance(a, b):
return fastdtw.fastdtw(a, b)[0]
class BlackboxPredictWrapper(object):
def __init__(self, blackbox, input_dimensions):
self.blackbox = blackbox
self.input_dimensions = input_dimensions
def predict(self, dataset):
# 3d dataset (batch, timesteps, 1)
if self.input_dimensions == 2:
dataset = dataset[:,:,0] # 3d to 2d array (batch, timesteps)
prediction = self.blackbox.predict(dataset)
if len(prediction.shape) > 1 and (prediction.shape[1] != 1):
prediction = np.argmax(prediction, axis = 1) # from probability to predicted class
prediction = prediction.ravel()
return prediction
def predict_proba(self, dataset):
# X: 3d array (batch, timesteps, 1)
if self.input_dimensions == 2:
dataset = dataset[:,:,0] # 3d to 2d array (batch, timesteps)
prediction = self.blackbox.predict_proba(dataset)
else: prediction = self.blackbox.predict(dataset)
return prediction
def stabilities_df(stabilities, dataset_length):
rows = list(range(5,dataset_length-1,5))
df = pd.DataFrame(stabilities.mean(axis=0), columns = ["mean", "max", "min"], index = rows)
df.index.names = ["k"]
return df