From d4698900dfd7277277227a08e5883fa73e71cc6f Mon Sep 17 00:00:00 2001 From: XuSShuai <18235417377@163.com> Date: Fri, 2 Nov 2018 12:43:31 +0800 Subject: [PATCH 1/3] add file Python/data/txt2mat.py --- Python/data/txt2mat.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Python/data/txt2mat.py diff --git a/Python/data/txt2mat.py b/Python/data/txt2mat.py new file mode 100644 index 0000000..bb52c12 --- /dev/null +++ b/Python/data/txt2mat.py @@ -0,0 +1,53 @@ +import numpy as np +import scipy.io as scio +from scipy import sparse +import argparse + + +parse = argparse.ArgumentParser() +parse.add_argument("-g", "--graph", help="each row is a training link") +parse.add_argument("-u", "--users", help="users list") +parse.add_argument("-n", "--dimension", help="dimension of user profile") +parse.add_argument("-f", "--profile", help="profile information of nodes, corresponding to users list") +parse.add_argument("-o", "--output", help="output .mat file") +args = parse.parse_args() + + +class MatFile: + def __init__(self): + pass + + @staticmethod + def _transform_graph(self) -> object: + data = np.loadtxt(args.graph, delimiter=",", dtype=np.int64) + net = sparse.dok_matrix((data.max() + 1, data.max() + 1)) + for line in data: + net[line[0], line[1]] = 1.0 + return net + + @staticmethod + def _transform_feature(self) -> object: + users = np.loadtxt(args.users, dtype=np.int64) + features = np.loadtxt(args.profile, dtype=np.int64) + group = sparse.dok_matrix((users.max() + 1 , int(args.dimension))) + for index, user in enumerate(users): + for f_id, feature in enumerate(features[index]): + group[user, f_id] = feature + return group + + def create_mat_file(self, file_path): + net = self._transform_graph() + group = self._transform_feature() + scio.savemat(file_path, {"net": net, "group": group}) + + @staticmethod + def load_mat_file(self, file_path): + data = scio.loadmat(file_path) + for key, value in data.items(): + print(key) + print(value) + +if __name__ == "__main__": + mat_file = MatFile() + mat_file.create_mat_file(args.output) + # mat_file.load_mat_file(args.output) \ No newline at end of file From cd05616b943ef1c89b296cd91b57d5f1436bc3fe Mon Sep 17 00:00:00 2001 From: XuSShuai <18235417377@163.com> Date: Fri, 2 Nov 2018 12:47:26 +0800 Subject: [PATCH 2/3] add file Python/data/txt2mat.py --- Python/data/txt2mat.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/data/txt2mat.py b/Python/data/txt2mat.py index bb52c12..11db387 100644 --- a/Python/data/txt2mat.py +++ b/Python/data/txt2mat.py @@ -6,9 +6,9 @@ parse = argparse.ArgumentParser() parse.add_argument("-g", "--graph", help="each row is a training link") -parse.add_argument("-u", "--users", help="users list") -parse.add_argument("-n", "--dimension", help="dimension of user profile") -parse.add_argument("-f", "--profile", help="profile information of nodes, corresponding to users list") +parse.add_argument("-u", "--users", help="users list, each row is a node") +parse.add_argument("-n", "--dimension", help="dimension of node profile") +parse.add_argument("-f", "--profile", help="profile information of nodes, corresponding to node list") parse.add_argument("-o", "--output", help="output .mat file") args = parse.parse_args() From 51f7c073b1d5621244ce37e1b51e4163de4fff5f Mon Sep 17 00:00:00 2001 From: XuSShuai <18235417377@163.com> Date: Fri, 2 Nov 2018 12:50:07 +0800 Subject: [PATCH 3/3] add file Python/data/txt2mat.py --- Python/data/txt2mat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/data/txt2mat.py b/Python/data/txt2mat.py index 11db387..776e572 100644 --- a/Python/data/txt2mat.py +++ b/Python/data/txt2mat.py @@ -6,9 +6,9 @@ parse = argparse.ArgumentParser() parse.add_argument("-g", "--graph", help="each row is a training link") -parse.add_argument("-u", "--users", help="users list, each row is a node") +parse.add_argument("-u", "--users", help="users/nodes list, each row is a node") parse.add_argument("-n", "--dimension", help="dimension of node profile") -parse.add_argument("-f", "--profile", help="profile information of nodes, corresponding to node list") +parse.add_argument("-f", "--profile", help="profile information of nodes, the ordering corresponding to nodes list") parse.add_argument("-o", "--output", help="output .mat file") args = parse.parse_args()