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:
data = citeulike.load_feedback()
self.assertEqual(len(data), 210537)
# 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 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()
# 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 Collaborative Topic Modeling"""
import cornac
from cornac.data import Reader
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(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])
# 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 Collaborative Deep Ranking"""
import cornac
from cornac.data import Reader
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 module
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)
cdr = cornac.models.CDR(k=50, autoencoder_structure=[200], max_iter=100, batch_size=128,
lambda_u=0.01, lambda_v=0.1, lambda_w=0.0001, lambda_n=5,
learning_rate=0.001, vocab_size=8000)
rec_300 = cornac.metrics.Recall(k=300)
exp = cornac.Experiment(eval_method=ratio_split,
# 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 Collaborative Deep Learning"""
import cornac
from cornac.data import Reader
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()
# 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 Collaborative Variational Autoencoder"""
import cornac
from cornac.data import Reader
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,
rating_threshold=0.5, verbose=True, seed=123,
item_text=item_text_modality)
cvae = cornac.models.CVAE(z_dim=50, vae_layers=[200, 100], act_fn='sigmoid',
input_dim=8000, lr=0.001, batch_size=128, n_epochs=100,
lambda_u=1e-4, lambda_v=0.001, lambda_r=10, lambda_w=1e-4,
seed=123, verbose=True)
rec_300 = cornac.metrics.Recall(k=300)
#
# 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 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()