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_with_ratio_split(self):
Experiment(eval_method=RatioSplit(self.data + [(self.data[0][0], self.data[1][1], 5.0)],
exclude_unknowns=True, seed=123, verbose=True),
models=[PMF(1, 0)],
metrics=[MAE(), RMSE()],
verbose=True).run()
try:
Experiment(None, None, None)
except ValueError:
assert True
try:
Experiment(None, [PMF(1, 0)], None)
except ValueError:
assert True
# limitations under the License.
# ============================================================================
"""
Example for Visual Bayesian Personalized Ranking
Original data: http://jmcauley.ucsd.edu/data/tradesy/
"""
import cornac
from cornac.datasets import tradesy
from cornac.data import ImageModality
from cornac.eval_methods import RatioSplit
features, item_ids = tradesy.load_feature() # BIG file
item_image_modality = ImageModality(features=features, ids=item_ids, normalized=True)
ratio_split = RatioSplit(data=tradesy.load_feedback(),
test_size=0.1, rating_threshold=0.5,
exclude_unknowns=True, verbose=True,
item_image=item_image_modality)
vbpr = cornac.models.VBPR(k=10, k2=20, n_epochs=50, batch_size=100, learning_rate=0.005,
lambda_w=1, lambda_b=0.01, lambda_e=0.0, use_gpu=True)
auc = cornac.metrics.AUC()
rec_50 = cornac.metrics.Recall(k=50)
exp = cornac.Experiment(eval_method=ratio_split,
models=[vbpr],
metrics=[auc, rec_50])
exp.run()
# You may obtain a copy of the License at
#
# 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.
# ============================================================================
import cornac as cn
ml_100k = cn.datasets.movielens.load_feedback()
ratio_split = cn.eval_methods.RatioSplit(data=ml_100k, test_size=0.2,
rating_threshold=4.0, verbose=True)
bo = cn.models.BaselineOnly(max_iter=30, learning_rate=0.01, lambda_reg=0.02, verbose=True)
svd = cn.models.SVD(k=10, max_iter=30, learning_rate=0.01, lambda_reg=0.02, verbose=True)
mae = cn.metrics.MAE()
rmse = cn.metrics.RMSE()
cn.Experiment(eval_method=ratio_split,
models=[bo, svd],
metrics=[mae, rmse]).run()
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Example for Multi-Task Explainable Recommendation"""
from cornac.datasets import amazon_toy
from cornac.data import SentimentModality
from cornac.eval_methods import RatioSplit
from cornac.metrics import NDCG, RMSE
from cornac.models import MTER
from cornac import Experiment
data = amazon_toy.load_feedback()
sentiment = amazon_toy.load_sentiment()
md = SentimentModality(data=sentiment)
eval_method = RatioSplit(data, test_size=0.2, rating_threshold=1.0,
sentiment=md, exclude_unknowns=True, verbose=True, seed=123)
mter = MTER(n_user_factors=15, n_item_factors=15, n_aspect_factors=12, n_opinion_factors=12,
n_bpr_samples=1000, n_element_samples=50, lambda_reg=0.1, lambda_bpr=10,
n_epochs=2000, lr=0.1, verbose=True, seed=123)
exp = Experiment(eval_method=eval_method,
models=[mter],
metrics=[RMSE(), NDCG(k=10), NDCG(k=20), NDCG(k=50), NDCG(k=100)])
exp.run()
# 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.
# ============================================================================
import cornac
from cornac.eval_methods import RatioSplit
from cornac.datasets import amazon_clothing
from cornac.data import Reader
ratio_split = RatioSplit(data=amazon_clothing.load_feedback(reader=Reader(bin_threshold=1.0)),
test_size=0.2, rating_threshold=1.0, seed=123,
exclude_unknowns=True, verbose=True)
gmf = cornac.models.GMF(num_factors=8, num_epochs=10, learner='adam',
batch_size=256, lr=0.001, num_neg=50, seed=123)
mlp = cornac.models.MLP(layers=[64, 32, 16, 8], act_fn='tanh', learner='adam',
num_epochs=10, batch_size=256, lr=0.001, num_neg=50, seed=123)
neumf1 = cornac.models.NeuMF(num_factors=8, layers=[64, 32, 16, 8], act_fn='tanh', learner='adam',
num_epochs=10, batch_size=256, lr=0.001, num_neg=50, seed=123)
neumf2 = cornac.models.NeuMF(name='NeuMF_pretrained', learner='adam',
num_epochs=10, batch_size=256, lr=0.001, num_neg=50, seed=123,
num_factors=gmf.num_factors, layers=mlp.layers, act_fn=mlp.act_fn).pretrain(gmf, mlp)
ndcg_50 = cornac.metrics.NDCG(k=50)
rec_50 = cornac.metrics.Recall(k=50)
#
# 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.
# ============================================================================
"""Your very first example with Cornac"""
import cornac as cn
# Load MovieLens 100K dataset
ml_100k = cn.datasets.movielens.load_feedback()
# Split data based on ratio
ratio_split = cn.eval_methods.RatioSplit(data=ml_100k, test_size=0.2, rating_threshold=4.0, seed=123)
# Here we are comparing biased MF, PMF, and BPR
mf = cn.models.MF(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02, use_bias=True, seed=123)
pmf = cn.models.PMF(k=10, max_iter=100, learning_rate=0.001, lamda=0.001, seed=123)
bpr = cn.models.BPR(k=10, max_iter=200, learning_rate=0.001, lambda_reg=0.01, seed=123)
# Define metrics used to evaluate the models
mae = cn.metrics.MAE()
rmse = cn.metrics.RMSE()
rec_20 = cn.metrics.Recall(k=20)
ndcg_20 = cn.metrics.NDCG(k=20)
auc = cn.metrics.AUC()
# Put it together into an experiment and run
exp = cn.Experiment(eval_method=ratio_split,
models=[mf, pmf, bpr],
# 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.
# ============================================================================
import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
from cornac.models import IBPR
# Load the MovieLens 1M dataset
ml_1m = movielens.load_feedback(variant='1M')
# Instantiate an evaluation method.
ratio_split = RatioSplit(data=ml_1m, test_size=0.2, rating_threshold=1.0,
exclude_unknowns=True, verbose=True)
# Instantiate a IBPR recommender model.
ibpr = IBPR(k=10, init_params={'U': None, 'V': None}, verbose=True)
# Instantiate evaluation metrics.
rec_20 = cornac.metrics.Recall(k=20)
pre_20 = cornac.metrics.Precision(k=20)
# Instantiate and then run an experiment.
exp = cornac.Experiment(eval_method=ratio_split,
models=[ibpr],
metrics=[rec_20, pre_20],
user_based=True)
exp.run()
# 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 Bayesian Personalized Ranking with Netflix dataset"""
import cornac
from cornac.data import Reader
from cornac.datasets import netflix
from cornac.eval_methods import RatioSplit
ratio_split = RatioSplit(data=netflix.load_feedback(variant='small', reader=Reader(bin_threshold=1.0)),
test_size=0.1, rating_threshold=1.0,
exclude_unknowns=True, verbose=True)
most_pop = cornac.models.MostPop()
bpr = cornac.models.BPR(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.01, verbose=True)
auc = cornac.metrics.AUC()
rec_20 = cornac.metrics.Recall(k=20)
cornac.Experiment(eval_method=ratio_split,
models=[most_pop, bpr],
metrics=[auc, rec_20],
user_based=True).run()
"""
from cornac.data import GraphModality
from cornac.eval_methods import RatioSplit
from cornac.experiment import Experiment
from cornac import metrics
from cornac.models import PCRL
from cornac.datasets import amazon_office as office
# Load office ratings and item contexts
ratings = office.load_feedback()
contexts = office.load_graph()
item_graph_modality = GraphModality(data=contexts)
ratio_split = RatioSplit(data=ratings,
test_size=0.2, rating_threshold=3.5,
exclude_unknowns=True, verbose=True,
item_graph=item_graph_modality)
pcrl = PCRL(k=100, z_dims=[300],
max_iter=300,
learning_rate=0.001)
# Evaluation metrics
nDgc = metrics.NDCG(k=-1)
rec = metrics.Recall(k=20)
pre = metrics.Precision(k=20)
# Instantiate and run your experiment
exp = Experiment(eval_method=ratio_split,
import cornac
from cornac.data import Reader
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
from cornac.data import TextModality
from cornac.data.text import BaseTokenizer
plots, movie_ids = movielens.load_plot()
ml_1m = movielens.load_feedback(variant='1M', reader=Reader(item_set=movie_ids))
# build text modality
item_text_modality = TextModality(corpus=plots, ids=movie_ids,
tokenizer=BaseTokenizer(sep='\t', stop_words='english'),
max_vocab=8000, max_doc_freq=0.5)
ratio_split = RatioSplit(data=ml_1m, test_size=0.2, exclude_unknowns=True,
item_text=item_text_modality, verbose=True, seed=123)
convmf = cornac.models.ConvMF(n_epochs=5, verbose=True, seed=123)
rmse = cornac.metrics.RMSE()
exp = cornac.Experiment(eval_method=ratio_split,
models=[convmf],
metrics=[rmse],
user_based=True)
exp.run()