Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove repeated DF joins. #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions loglizer/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def transform(self, x, window_y, y):
y = y
data_dict = {"SessionId": x["SessionId"].values, "window_y": window_y.values, "y": y.values, "x": np.array(x["EventSequence"].tolist())}
return data_dict


class FeatureExtractor(object):

Expand Down Expand Up @@ -91,12 +91,12 @@ def fit_transform(self, X_seq, term_weighting=None, normalization=None, oov=Fals
X = X[:, idx]
self.events = np.array(X_df.columns)[idx].tolist()
X = np.hstack([X, oov_vec.reshape(X.shape[0], 1)])

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))
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)
Expand All @@ -105,8 +105,8 @@ def fit_transform(self, X_seq, term_weighting=None, normalization=None, oov=Fals
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]))

print('Train data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1]))
return X_new

def transform(self, X_seq):
Expand All @@ -129,23 +129,22 @@ def transform(self, X_seq):
X_df = pd.DataFrame(X_counts)
X_df = X_df.fillna(0)
empty_events = set(self.events) - set(X_df.columns)
for event in empty_events:
X_df[event] = [0] * len(X_df)
X_df = pd.concat([X_df, pd.DataFrame(np.zeros((len(X_df),len(empty_events))), columns=empty_events)], axis=1)
X = X_df[self.events].values
if self.oov:
oov_vec = np.sum(X_df[X_df.columns.difference(self.events)].values > 0, axis=1)
X = np.hstack([X, oov_vec.reshape(X.shape[0], 1)])

num_instance, num_event = X.shape
if self.term_weighting == 'tf-idf':
idf_matrix = X * np.tile(self.idf_vec, (num_instance, 1))
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]))
print('Test data shape: {}-by-{}\n'.format(X_new.shape[0], X_new.shape[1]))

return X_new