FM xlearn实战

这篇博客介绍了如何使用xLearn库在Python中训练Field-Aware Factorization Machine(FFM)模型。通过示例代码展示了设置训练数据、参数配置、模型训练以及模型保存和预测的过程。此外,还提及了模型可以转换为人类可读的TXT格式。
摘要由CSDN通过智能技术生成

xlearn文档:xLearn Python API 使用指南 — xLearn 0.4.0 documentation

 

xlearn github:xlearn: High performance, easy-to-use, and scalable machine learning (ML) package, including linear model (LR), factorization machines (FM), and field-aware factorization machines (FFM) for Python and CLI interface. - Gitee.com

fm推导公式:(转自FM算法解析及Python实现 - Bo_hemian - 博客园

结论:

 

代码:

import xlearn as xl

# Training task
ffm_model = xl.create_ffm()                # Use field-aware factorization machine (ffm)
ffm_model.setTrain("./small_train.txt")    # Set the path of training data

# parameter:
#  0. task: binary classification
#  1. learning rate : 0.2
#  2. regular lambda : 0.002
param = {'task':'binary', 'lr':0.2, 'lambda':0.002}

# Train model
ffm_model.fit(param, "./model.out")

###xLearn 训练过后在当前文件夹下产生了一个叫 model.out 的新文件
###这个文件用来存储训练后的模型,我们可以用这个模型在未来进行预测:
ffm_model.setTest("./small_test.txt")
ffm_model.predict("./model.out", "./output.txt")

###用户还可以通过 setTXTModel() API 将模型输出成人类可读的 TXT 格式,例如:
ffm_model.setTXTModel("./model.txt")
ffm_model.fit(param, "./model.out")

运行上述命令后,我们发现在当前文件夹下生成了一个新的文件 model.txt,这个文件存储着 TXT 格式的输出模型:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值