Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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 Variational Autoencoder for Collaborative Filtering for Implicit Feedback Datasets (Citeulike)"""
import cornac
from cornac.datasets import citeulike
from cornac.eval_methods import RatioSplit
data = citeulike.load_feedback()
ratio_split = RatioSplit(data=data, test_size=0.2, exclude_unknowns=True,
verbose=True, seed=123, rating_threshold=0.5)
vaecf = cornac.models.VAECF(k=10, h=20, n_epochs=100, batch_size=100, learning_rate=0.001, beta=1.0, seed=123)
rec_20 = cornac.metrics.Recall(k=20)
ndcg_20 = cornac.metrics.NDCG(k=20)
auc = cornac.metrics.AUC()
cornac.Experiment(eval_method=ratio_split,
models=[vaecf],
metrics=[rec_20, ndcg_20, auc],
user_based=True).run()
import cornac
from cornac.datasets import amazon_toy
from cornac.data import SentimentModality
from cornac.eval_methods import RatioSplit
rating = amazon_toy.load_feedback()
sentiment = amazon_toy.load_sentiment()
md = SentimentModality(data=sentiment)
split_data = RatioSplit(data=rating,
test_size=0.15,
exclude_unknowns=True, verbose=True,
sentiment=md, seed=123)
efm = cornac.models.EFM(num_explicit_factors=40, num_latent_factors=60, num_most_cared_aspects=15,
rating_scale=5.0, alpha=0.85,
lambda_x=1, lambda_y=1, lambda_u=0.01, lambda_h=0.01, lambda_v=0.01,
max_iter=100, num_threads=1,
trainable=True, verbose=True, seed=123)
rmse = cornac.metrics.RMSE()
ndcg_50 = cornac.metrics.NDCG(k=50)
auc = cornac.metrics.AUC()
exp = cornac.Experiment(eval_method=split_data,
models=[efm],
metrics=[rmse, ndcg_50, auc])
exp.run()
# 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)
cornac.Experiment(eval_method=ratio_split,
models=[gmf, mlp, neumf1, neumf2],
metrics=[ndcg_50, rec_50]).run()
# 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 Matrix Factorization with biases"""
import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
ratio_split = RatioSplit(data=movielens.load_feedback(variant='1M'),
test_size=0.2,
exclude_unknowns=False,
verbose=True)
global_avg = cornac.models.GlobalAvg()
mf = cornac.models.MF(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02,
use_bias=True, early_stop=True, verbose=True)
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
exp = cornac.Experiment(eval_method=ratio_split,
models=[global_avg, mf],
metrics=[mae, rmse],
user_based=True)
exp.run()
# ============================================================================
"""Example for Collaborative Filtering for Implicit Feedback Datasets (Citeulike)"""
import cornac
from cornac.data import Reader
from cornac.datasets import citeulike
from cornac.eval_methods import RatioSplit
_, item_ids = citeulike.load_text()
data = citeulike.load_feedback(reader=Reader(item_set=item_ids))
ratio_split = RatioSplit(data=data, test_size=0.2, exclude_unknowns=True,
verbose=True, seed=123, rating_threshold=0.5)
cf = cornac.models.WMF(k=50, max_iter=50, learning_rate=0.001, lambda_u=0.01, lambda_v=0.01, verbose=True, seed=123)
rec_300 = cornac.metrics.Recall(k=300)
cornac.Experiment(eval_method=ratio_split,
models=[cf],
metrics=[rec_300],
user_based=True).run()
# 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)
cornac.Experiment(eval_method=ratio_split,
models=[gmf, mlp, neumf1, neumf2],
metrics=[ndcg_50, rec_50]).run()
#
# 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()
from cornac.datasets import citeulike
from cornac.eval_methods import RatioSplit
from cornac.data import TextModality
from cornac.data.text import BaseTokenizer
docs, item_ids = citeulike.load_text()
data = citeulike.load_feedback(reader=Reader(item_set=item_ids))
# build text modality
item_text_modality = TextModality(corpus=docs, ids=item_ids,
tokenizer=BaseTokenizer(stop_words='english'),
max_vocab=8000, max_doc_freq=0.5)
ratio_split = RatioSplit(data=data, test_size=0.2, exclude_unknowns=True,
item_text=item_text_modality, verbose=True, seed=123, rating_threshold=0.5)
cdl = cornac.models.CDL(k=50, autoencoder_structure=[200], max_iter=30,
lambda_u=0.1, lambda_v=1, lambda_w=0.1, lambda_n=1000)
rec_300 = cornac.metrics.Recall(k=300)
exp = cornac.Experiment(eval_method=ratio_split,
models=[cdl],
metrics=[rec_300])
exp.run()
from cornac.eval_methods import RatioSplit
from cornac.data import TextModality
from cornac.data.text import BaseTokenizer
docs, item_ids = citeulike.load_text()
data = citeulike.load_feedback(reader=Reader(item_set=item_ids))
# build text modality
item_text_modality = TextModality(corpus=docs, ids=item_ids,
tokenizer=BaseTokenizer(sep=' ', stop_words='english'),
max_vocab=8000, max_doc_freq=0.5)
ratio_split = RatioSplit(data=data, test_size=0.2, exclude_unknowns=True,
item_text=item_text_modality, verbose=True, seed=123, rating_threshold=0.5)
ctr = cornac.models.CTR(k=50, max_iter=50, lambda_v=1)
rec_300 = cornac.metrics.Recall(k=300)
exp = cornac.Experiment(eval_method=ratio_split,
models=[ctr],
metrics=[rec_300])
exp.run()