-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
91 lines (74 loc) · 2.69 KB
/
preprocessing.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
"""
The interface for data preprocessing.
Authors:
LogPAI Team
"""
import pandas as pd
import os
import numpy as np
import re
from collections import Counter
from scipy.special import expit
from itertools import compress
class FeatureExtractor(object):
def __init__(self):
self.idf_vec = None
self.mean_vec = None
self.events = None
self.term_weighting = None
self.normalization = None
self.oov = None
def fit_transform(self, X_seq, term_weighting=None, normalization=None, oov=False, min_count=1):
""" Fit and transform the data matrix
Arguments
---------
X_seq: ndarray, log sequences matrix
term_weighting: None or `tf-idf`
normalization: None or `zero-mean`
oov: bool, whether to use OOV event
min_count: int, the minimal occurrence of events (default 0), only valid when oov=True.
Returns
-------
X_new: The transformed data matrix
"""
X = np.array(X_seq)
self.normalization = normalization
self.term_weighting = term_weighting
self.oov = oov
num_instance, num_event = X.shape
if self.term_weighting == 'tf-idf':
df_vec = np.sum(X > 0, axis=0)
self.idf_vec = np.log(num_instance / (df_vec + 1e-8))
idf_matrix = X * np.tile(self.idf_vec, (num_instance, 1))
X = idf_matrix
if self.normalization == 'zero-mean':
mean_vec = X.mean(axis=0)
self.mean_vec = mean_vec.reshape(1, num_event)
X = X - np.tile(self.mean_vec, (num_instance, 1))
elif self.normalization == 'sigmoid':
X[X != 0] = expit(X[X != 0])
X_new = X
print('Train data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1]))
return X_new
def transform(self, X_seq):
""" Transform the data matrix with trained parameters
Arguments
---------
X: log sequences matrix
term_weighting: None or `tf-idf`
Returns
-------
X_new: The transformed data matrix
"""
X = np.array(X_seq)
num_instance, num_event = X.shape
if self.term_weighting == 'tf-idf':
idf_matrix = X * np.tile(self.idf_vec, (num_instance, 1))
X = idf_matrix
if self.normalization == 'zero-mean':
X = X - np.tile(self.mean_vec, (num_instance, 1))
elif self.normalization == 'sigmoid':
X[X != 0] = expit(X[X != 0])
X_new = X
print('Test data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1]))
return X_new