-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_movies.py
164 lines (119 loc) · 5.06 KB
/
data_movies.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2021 Grabtaxi Holdings Pte Ltd (GRAB), All rights reserved.
# Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
import torch
from torch_sparse.tensor import SparseTensor
import numpy as np
from anomaly_insert import inject_random_block_anomaly
from models.data import BipartiteData
import torch
from sklearn import preprocessing
import pandas as pd
from sentence_transformers import SentenceTransformer
# %%
def standardize(features: np.ndarray) -> np.ndarray:
scaler = preprocessing.StandardScaler()
z = scaler.fit_transform(features)
return z
def prepare_data():
model = SentenceTransformer("all-MiniLM-L6-v2")
df = pd.read_csv(f"data/movies.csv")
df["summary_char_len"] = df["summary"].astype("str").apply(len)
df["text_char_len"] = df["text"].astype("str").apply(len)
df["helpfulness"] = (
df["helpfulness_numerator"] / df["helpfulness_denominator"]
).fillna(0)
df = df.sort_values(["product_id", "user_id", "time"])
dfu = df.groupby(["product_id", "user_id"], as_index=False).last()
df_product = dfu.groupby("product_id", as_index=False).agg(
user_count=("user_id", "count"),
helpful_num_mean=("helpfulness_numerator", "mean"),
helpful_num_sum=("helpfulness_numerator", "sum"),
helpful_mean=("helpfulness", "mean"),
helpful_sum=("helpfulness", "sum"),
score_mean=("score", "mean"),
score_sum=("score", "sum"),
summary_len_mean=("summary_char_len", "mean"),
summary_len_sum=("summary_char_len", "sum"),
text_len_mean=("text_char_len", "mean"),
text_len_sum=("text_char_len", "sum"),
)
df_user = dfu.groupby("user_id", as_index=False).agg(
product_count=("product_id", "count"),
helpful_num_mean=("helpfulness_numerator", "mean"),
helpful_num_sum=("helpfulness_numerator", "sum"),
helpful_mean=("helpfulness", "mean"),
helpful_sum=("helpfulness", "sum"),
score_mean=("score", "mean"),
score_sum=("score", "sum"),
summary_len_mean=("summary_char_len", "mean"),
summary_len_sum=("summary_char_len", "sum"),
text_len_mean=("text_char_len", "mean"),
text_len_sum=("text_char_len", "sum"),
)
df_user.to_csv(f"data/movies-user.csv")
df_product.to_csv(f"data/movies-product.csv")
sentences = dfu["text"].astype("str").to_numpy()
embeddings = model.encode(sentences)
np.save(f"data/movies-embeddings.npy", embeddings)
dfu[["product_id", "user_id"]].to_csv(f"data/movies-ids.csv")
def create_graph():
df_user = pd.read_csv("data/movies-user.csv")
df_product = pd.read_csv("data/movies-product.csv")
df_review_id = pd.read_csv("data/movies-ids.csv")
embeddings = np.load("data/movies-embeddings.npy")
df_user["uid"] = df_user.index
df_product["pid"] = df_product.index
df_user_id = df_user[["user_id", "uid"]]
df_product_id = df_product[["product_id", "pid"]]
cols = [f"v{i}" for i in range(embeddings.shape[1])]
df_review = pd.concat(
[df_review_id, pd.DataFrame(embeddings, columns=cols)], axis=1
)
df_review_2 = df_review.merge(
df_user_id,
on="user_id",
).merge(df_product_id, on="product_id")
df_review_2 = df_review_2.sort_values(["uid", "pid"])
uid = torch.tensor(df_review_2["uid"].to_numpy())
pid = torch.tensor(df_review_2["pid"].to_numpy())
adj = SparseTensor(row=uid, col=pid)
edge_attr = torch.tensor(standardize(df_review_2.iloc[:, 3:-2].to_numpy())).float()
user_attr = torch.tensor(standardize(df_user.iloc[:, 2:-1].to_numpy())).float()
product_attr = torch.tensor(
standardize(df_product.iloc[:, 2:-1].to_numpy())
).float()
data = BipartiteData(adj, xu=user_attr, xv=product_attr, xe=edge_attr)
return data
def store_graph(name: str, dataset):
torch.save(dataset, f"storage/{name}.pt")
def load_graph(name: str, key: str, id=None):
if id == None:
data = torch.load(f"storage/{name}.pt")
return data[key]
else:
data = torch.load(f"storage/{name}.pt")
return data[key][id]
def synth_random():
# generate nd store data
import argparse
parser = argparse.ArgumentParser(description="GraphBEAN")
parser.add_argument("--name", type=str, default="movies_anomaly", help="name")
parser.add_argument("--n-graph", type=int, default=2, help="n graph")
args = vars(parser.parse_args())
prepare_data()
graph = create_graph()
store_graph("movies-graph", graph)
# graph = torch.load(f'storage/movies-graph.pt')
print(graph)
graph_anomaly_list = []
for i in range(args["n_graph"]):
print(f"GRAPH ANOMALY {i} >>>>>>>>>>>>>>")
graph_multi_dense = inject_random_block_anomaly(
graph, num_group=100, num_nodes_range=(1, 20)
)
graph_anomaly_list.append(graph_multi_dense)
print()
dataset = {"args": args, "graph": graph, "graph_anomaly_list": graph_anomaly_list}
store_graph(args["name"], dataset)
if __name__ == "__main__":
synth_random()