Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_filter(self):
reader = Reader(bin_threshold=4.0)
data = reader.read(self.data_file)
self.assertEqual(len(data), 8)
self.assertListEqual([x[2] for x in data], [1] * len(data))
reader = Reader(min_user_freq=2)
self.assertEqual(len(reader.read(self.data_file)), 0)
reader = Reader(min_item_freq=2)
self.assertEqual(len(reader.read(self.data_file)), 0)
reader = Reader(user_set=['76'], item_set=['93'])
self.assertEqual(len(reader.read(self.data_file)), 1)
reader = Reader(user_set=['76', '768'])
self.assertEqual(len(reader.read(self.data_file)), 2)
reader = Reader(item_set=['93', '257', '795'])
self.assertEqual(len(reader.read(self.data_file)), 3)
def test_filter(self):
reader = Reader(bin_threshold=4.0)
data = reader.read(self.data_file)
self.assertEqual(len(data), 8)
self.assertListEqual([x[2] for x in data], [1] * len(data))
reader = Reader(min_user_freq=2)
self.assertEqual(len(reader.read(self.data_file)), 0)
reader = Reader(min_item_freq=2)
self.assertEqual(len(reader.read(self.data_file)), 0)
reader = Reader(user_set=['76'], item_set=['93'])
self.assertEqual(len(reader.read(self.data_file)), 1)
reader = Reader(user_set=['76', '768'])
self.assertEqual(len(reader.read(self.data_file)), 2)
reader = Reader(item_set=['93', '257', '795'])
self.assertEqual(len(reader.read(self.data_file)), 3)
def test_get_node_degree(self):
data = Reader().read('./tests/graph_data.txt', sep=' ')
gmd = GraphModality(data=data)
global_iid_map = OrderedDict()
for raw_iid, raw_jid, val in data:
global_iid_map.setdefault(raw_iid, len(global_iid_map))
gmd.build(id_map=global_iid_map)
degree = gmd.get_node_degree()
self.assertEqual(degree.get(0)[0], 4)
self.assertEqual(degree.get(0)[1], 1)
self.assertEqual(degree.get(1)[0], 2)
self.assertEqual(degree.get(1)[1], 1)
self.assertEqual(degree.get(5)[0], 0)
self.assertEqual(degree.get(5)[1], 1)
def test_from_splits(self):
data = Reader().read('./tests/data.txt')
try:
BaseMethod.from_splits(train_data=None, test_data=None)
except ValueError:
assert True
try:
BaseMethod.from_splits(train_data=data, test_data=None)
except ValueError:
assert True
try:
BaseMethod.from_splits(train_data=data, test_data=[], exclude_unknowns=True)
except ValueError:
assert True
bm = BaseMethod.from_splits(train_data=data[:-1], test_data=data[-1:])
def setUp(self):
self.data = Reader().read('./tests/data.txt')
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Example for Social Bayesian Personalized Ranking with Epinions dataset"""
import cornac
from cornac.data import Reader, GraphModality
from cornac.datasets import epinions
from cornac.eval_methods import RatioSplit
ratio_split = RatioSplit(data=epinions.load_feedback(Reader(bin_threshold=4.0)),
test_size=0.1, rating_threshold=0.5,
exclude_unknowns=True, verbose=True,
user_graph=GraphModality(data=epinions.load_trust()))
sbpr = cornac.models.SBPR(k=10, max_iter=50, learning_rate=0.001,
lambda_u=0.015, lambda_v=0.025, lambda_b=0.01,
verbose=True)
rec_10 = cornac.metrics.Recall(k=10)
cornac.Experiment(eval_method=ratio_split,
models=[sbpr],
metrics=[rec_10]).run()
"""Load user-item ratings, rating value is in [1,5]
Parameters
----------
reader: `obj:cornac.data.Reader`, default: None
Reader object used to read the data.
Returns
-------
data: array-like
Data in the form of a list of tuples (user, item, rating).
"""
fpath = cache(url='http://www.trustlet.org/datasets/downloaded_epinions/ratings_data.txt.bz2',
unzip=True, relative_path='ratings_data.txt', cache_dir=_get_cache_dir())
reader = Reader() if reader is None else reader
return reader.read(fpath, sep=' ')
def load_graph(reader: Reader = None) -> List:
"""Load the item-item interactions (symmetric network), built from the Amazon Also-Viewed information
Parameters
----------
reader: `obj:cornac.data.Reader`, default: None
Reader object used to read the data.
Returns
-------
data: array-like
Data in the form of a list of tuples (item, item, 1).
"""
fpath = cache(url='https://static.preferred.ai/cornac/datasets/amazon_clothing/context.zip',
unzip=True, relative_path='amazon_clothing/context.txt')
reader = Reader() if reader is None else reader
return reader.read(fpath, fmt='UI', sep='\t')