-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from saghiles/master
Add: GraphModule, c2pf example. Change: c2pf, pcrl, results. Remove: which_ function
- Loading branch information
Showing
20 changed files
with
331 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
@author: Quoc-Tuan Truong <tuantq.vnu@gmail.com> | ||
@author: Aghiles Salah <asalah@smu.edu.sg> | ||
""" | ||
|
||
from . import Module | ||
import scipy.sparse as sp | ||
import numpy as np | ||
|
||
|
||
class GraphModule(Module): | ||
"""Graph module | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.raw_data = kwargs.get('data', None) | ||
self.matrix = None | ||
self.map_data = [] | ||
|
||
def _build_triplet(self, ordered_ids): | ||
"""Build adjacency matrix in sparse triplet format using maped ids | ||
""" | ||
|
||
for i, j, val in self.raw_data: | ||
self.map_data.append([ordered_ids[i], ordered_ids[j], val]) | ||
self.map_data = np.asanyarray(self.map_data) | ||
self.raw_data = None | ||
|
||
def _build_sparse_matrix(self, triplet): | ||
"""Build sparse adjacency matrix | ||
""" | ||
|
||
n_rows = max(triplet[:, 0]) + 1 | ||
n_cols = max(triplet[:, 1]) + 1 | ||
self.matrix = sp.csc_matrix((triplet[:, 2], (triplet[:, 0], triplet[:, 1])), shape=(n_rows, n_cols)) | ||
|
||
def get_train_triplet(self, train_row_ids, train_col_ids): | ||
"""Get the training tuples | ||
""" | ||
train_triplet = [] | ||
# this makes operations much more efficient | ||
train_row_ids = np.asanyarray(list(train_row_ids)) | ||
train_col_ids = np.asanyarray(list(train_col_ids)) | ||
for i, j, val in self.map_data: | ||
if (i not in train_row_ids) or (j not in train_col_ids): | ||
continue | ||
train_triplet.append([i, j, val]) | ||
|
||
return np.asarray(train_triplet) | ||
|
||
def build(self, ordered_ids): | ||
pass | ||
self._build_triplet(ordered_ids) | ||
self._build_sparse_matrix(self.map_data) | ||
|
||
def batch(self, batch_ids): | ||
|
||
"""Collaborative Context Poisson Factorization. | ||
Parameters | ||
---------- | ||
batch_ids: array, required | ||
An array conting the ids of rows to be returned from the sparse adjacency matrix. | ||
""" | ||
|
||
return self.matrix[batch_ids] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.