Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_trust(reader: Reader = None) -> List:
"""Load the user trust information (undirected network)
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 (source_user, target_item, trust_value).
"""
fpath = cache(url='http://www.trustlet.org/datasets/downloaded_epinions/trust_data.txt.bz2',
unzip=True, relative_path='trust_data.txt', cache_dir=_get_cache_dir())
reader = Reader() if reader is None else reader
return reader.read(fpath, sep=' ')
reader: `obj:cornac.data.Reader`, optional, default: None
Reader object used to read the data.
Returns
-------
data: array-like
Data in the form of a list of tuples depending on the given data format.
"""
fmt = validate_format(fmt, VALID_DATA_FORMATS)
if variant not in VARIANTS:
raise ValueError('variant must be one of {}.'.format(VARIANTS))
fpath = cache(url=URL[variant], unzip=UNZIP[variant], relative_path=RELATIVE_PATH[variant])
reader = Reader() if reader is None else reader
return reader.read(fpath, fmt, sep=SEP[variant])
def load_feedback(reader: Reader = None) -> List:
"""Load the implicit feedback between users and items
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, 1).
"""
fpath = cache(url='https://static.preferred.ai/cornac/datasets/citeulike/users.zip',
relative_path='citeulike/users.dat', unzip=True)
reader = Reader() if reader is None else reader
return reader.read(fpath, fmt='UI', sep=' ', id_inline=True)
def load_feedback(reader: Reader = None) -> List:
"""Load the user-item ratings, scale: [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='https://static.preferred.ai/cornac/datasets/amazon_clothing/rating.zip',
unzip=True, relative_path='amazon_clothing/rating.txt')
reader = Reader() if reader is None else reader
return reader.read(fpath, sep='\t')
Data file name.
fmt: str, default: 'UIR'
Data format to be returned.
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 depending on the given data format.
"""
fmt = validate_format(fmt, VALID_DATA_FORMATS)
fpath = cache(url='https://static.preferred.ai/cornac/datasets/netflix/{}.zip'.format(fname),
unzip=True, relative_path='netflix/{}.csv'.format(fname))
reader = Reader() if reader is None else reader
return reader.read(fpath, fmt, sep=',')
def load_text():
"""Load item texts including tile and abstract joined together into one document per item.
Returns
-------
texts: List
List of text documents, one per item.
ids: List
List of item ids aligned with indices in `texts`.
"""
import csv
texts, ids = [], []
fpath = cache(url='https://static.preferred.ai/cornac/datasets/citeulike/text.zip',
relative_path='citeulike/raw-data.csv', unzip=True)
with open(fpath, 'r', encoding='utf-8', errors='ignore') as f:
next(f)
for row in csv.reader(f, delimiter=',', quotechar='"'):
ids.append(row[0])
texts.append(row[3] + '. ' + row[4])
return texts, ids
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, [(aspect, opinion, sentiment), (aspect, opinion, sentiment), ...]).
References
----------
Gao, J., Wang, X., Wang, Y., & Xie, X. (2019). Explainable Recommendation Through Attentive Multi-View Learning. AAAI.
"""
fpath = cache(url='https://static.preferred.ai/cornac/datasets/amazon_toy/sentiment.zip',
unzip=True, relative_path='amazon_toy/sentiment.txt')
reader = Reader() if reader is None else reader
return reader.read(fpath, fmt='UITup', sep=',', tup_sep=':')
def load_feature():
"""Load the item visual feature
Returns
-------
features: numpy.ndarray
Feature matrix with shape (n, 4096) with n is the number of items.
item_ids: List
List of item ids aligned with indices in `features`.
"""
features = np.load(cache(url='https://static.preferred.ai/cornac/datasets/tradesy/item_features.zip',
unzip=True, relative_path='tradesy/item_features.npy'))
item_ids = read_text(cache(url='https://static.preferred.ai/cornac/datasets/tradesy/item_ids.zip',
unzip=True, relative_path='tradesy/item_ids.txt'))
return features, item_ids
# 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 to train and evaluate a model with given data"""
from cornac.data import Reader
from cornac.eval_methods import BaseMethod
from cornac.models import MF
from cornac.metrics import MAE, RMSE
from cornac.utils import cache
# Download MovieLens 100K provided training and test splits
reader = Reader()
train_data = reader.read(cache(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.base'))
test_data = reader.read(cache(url='http://files.grouplens.org/datasets/movielens/ml-100k/u1.test'))
eval_method = BaseMethod.from_splits(train_data=train_data, test_data=test_data,
exclude_unknowns=False, verbose=True)
mf = MF(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02,
use_bias=True, early_stop=True, verbose=True)
# Evaluation
result = eval_method.evaluate(model=mf, metrics=[MAE(), RMSE()], user_based=True)
print(result)
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_office/context.zip',
unzip=True, relative_path='amazon_office/context.txt')
reader = Reader() if reader is None else reader
return reader.read(fpath, sep=' ')