-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_interpolation.py
43 lines (39 loc) · 1.31 KB
/
data_interpolation.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Guansong Pang
Source code for the PReNet algorithm in KDD'23.
"""
import numpy as np
from scipy.sparse import vstack, csc_matrix
from sklearn.neighbors import KDTree
def inject_noise_sparse(seed, n_out):
rng = np.random.RandomState(42)
n_sample, dim = seed.shape
swap_ratio = 0.05
n_swap_feat = int(swap_ratio * dim)
seed = seed.tocsc()
noise = csc_matrix((n_out, dim))
print(noise.shape)
for i in np.arange(n_out):
outlier_idx = rng.choice(n_sample, 2, replace = False)
o1 = seed[outlier_idx[0]]
o2 = seed[outlier_idx[1]]
swap_feats = rng.choice(dim, n_swap_feat, replace = False)
noise[i] = o1.copy()
noise[i, swap_feats] = o2[0, swap_feats]
return noise.tocsr()
def inject_noise(seed, n_out):
rng = np.random.RandomState(42)
n_sample, dim = seed.shape
swap_ratio = 0.05
n_swap_feat = int(swap_ratio * dim)
noise = np.empty((n_out, dim))
for i in np.arange(n_out):
outlier_idx = rng.choice(n_sample, 2, replace = False)
o1 = seed[outlier_idx[0]]
o2 = seed[outlier_idx[1]]
swap_feats = rng.choice(dim, n_swap_feat, replace = False)
noise[i] = o1.copy()
noise[i, swap_feats] = o2[swap_feats]
return noise