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_load_feedback(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_100k = movielens.load_feedback()
self.assertEqual(len(ml_100k), 100000)
if random.random() > 0.8:
ml_1m = movielens.load_feedback(variant='1M')
self.assertEqual(len(ml_1m), 1000209)
def test_load_feedback(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_100k = movielens.load_feedback()
self.assertEqual(len(ml_100k), 100000)
if random.random() > 0.8:
ml_1m = movielens.load_feedback(variant='1M')
self.assertEqual(len(ml_1m), 1000209)
#
# 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 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()
# 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 to run Non-negative Matrix Factorization (NMF) model with Ratio Split evaluation strategy"""
import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
# Load the MovieLens 100K dataset
ml_100k = movielens.load_feedback()
# Instantiate an evaluation method.
eval_method = RatioSplit(data=ml_100k, test_size=0.2, rating_threshold=4.0,
exclude_unknowns=True, verbose=True, seed=123)
# Instantiate a NMF recommender model.
nmf = cornac.models.NMF(k=15, max_iter=50, learning_rate=.005,
lambda_u=.06, lambda_v=.06, lambda_bu=.02, lambda_bi=.02,
use_bias=False, verbose=True, seed=123)
# Instantiate evaluation metrics.
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
rec_20 = cornac.metrics.Recall(k=20)
pre_20 = cornac.metrics.Precision(k=20)
#
# 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 to run Probabilistic Matrix Factorization (PMF) model with Ratio Split evaluation strategy"""
import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
from cornac.models import PMF
# Load the MovieLens 100K dataset
ml_100k = movielens.load_feedback()
# Instantiate an evaluation method.
ratio_split = RatioSplit(data=ml_100k, test_size=0.2, rating_threshold=4.0, exclude_unknowns=False)
# Instantiate a PMF recommender model.
pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lamda=0.001)
# Instantiate evaluation metrics.
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
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=[pmf],
# 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.
# ============================================================================
"""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()
# 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 Convolutional Matrix Factorization"""
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],
# 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.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],