-
Notifications
You must be signed in to change notification settings - Fork 0
/
influence_calculation.py
95 lines (75 loc) · 4.13 KB
/
influence_calculation.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
def InfluenceCalculation(blackbox, extractor, cKNN, fKNN, pKNN, BlackBoxConstructor,
X_train, y_train, X_anomaly, n_test=10,perturb_percent=0.75):
ind = np.random.choice(len(X_anomaly),size=n_test,replace=False)
X_anomaly = X_anomaly[ind]
cKNN_influence = list()
fKNN_influence = list()
pKNN_influence = list()
# Main loop
for i in range(n_test):
##########################################################################################
########################################## cKNN ##########################################
## Achieve neighborhood samples using cKNN method
contribution_x = extractor(X_anomaly[i])
_, nbrs_cKNN = cKNN.kneighbors(contribution_x.reshape(1, -1))
nbrs_cKNN = nbrs_cKNN[0]
## Perturb the label of the data inside the neighborhood
y_train_ = y_train.copy()
perturbed_idx = np.random.choice(range(len(nbrs_cKNN)),
round(perturb_percent * len(nbrs_cKNN)),
replace=False)
perturbed_idx = nbrs_cKNN[perturbed_idx].copy()
y_train_[perturbed_idx] = 1 - y_train_[perturbed_idx]
# Creating the black box using the perturbed data
blackbox_ = BlackBoxConstructor(random_state=42)
blackbox_.fit(X_train, y_train_)
# Testing influence
bb_pred = int(blackbox.predict(X_anomaly[i].reshape(1, -1))[0])
bb_pred_ = int(blackbox_.predict(X_anomaly[i].reshape(1, -1))[0])
changed = 0 if bb_pred == bb_pred_ else 1
cKNN_influence.append(changed)
##########################################################################################
########################################## fKNN ##########################################
## Achieve neighborhood samples using fKNN method
_, nbrs_fKNN = fKNN.kneighbors(X_anomaly[i].reshape(1, -1))
nbrs_fKNN = nbrs_fKNN[0]
## Perturb the label of the data inside the neighborhood
y_train_ = y_train.copy()
perturbed_idx = np.random.choice(range(len(nbrs_fKNN)),
round(perturb_percent * len(nbrs_fKNN)),
replace=False)
perturbed_idx = nbrs_fKNN[perturbed_idx].copy()
y_train_[perturbed_idx] = 1 - y_train_[perturbed_idx]
# Creating the black box using the perturbed data
blackbox_ = BlackBoxConstructor(random_state=42)
blackbox_.fit(X_train, y_train_)
# Testing influence
bb_pred = int(blackbox.predict(X_anomaly[i].reshape(1, -1))[0])
bb_pred_ = int(blackbox_.predict(X_anomaly[i].reshape(1, -1))[0])
changed = 0 if bb_pred == bb_pred_ else 1
fKNN_influence.append(changed)
##########################################################################################
########################################## pKNN ##########################################
## Achieve neighborhood samples using pKNN method
_, nbrs_pKNN = pKNN.kneighbors(blackbox.predict_proba(X_anomaly[i].reshape(1, -1)))
nbrs_pKNN = nbrs_pKNN[0]
## Perturb the label of the data inside the neighborhood
y_train_ = y_train.copy()
perturbed_idx = np.random.choice(range(len(nbrs_pKNN)),
round(perturb_percent * len(nbrs_pKNN)),
replace=False)
perturbed_idx = nbrs_pKNN[perturbed_idx].copy()
y_train_[perturbed_idx] = 1 - y_train_[perturbed_idx]
# Creating the black box using the perturbed data
blackbox_ = BlackBoxConstructor(random_state=42)
blackbox_.fit(X_train, y_train_)
# Testing influence
bb_pred = int(blackbox.predict(X_anomaly[i].reshape(1, -1))[0])
bb_pred_ = int(blackbox_.predict(X_anomaly[i].reshape(1, -1))[0])
changed = 0 if bb_pred == bb_pred_ else 1
pKNN_influence.append(changed)
# Return results
return [np.mean(cKNN_influence),
np.mean(fKNN_influence),
np.mean(pKNN_influence)]