xlearn文档:xLearn Python API 使用指南 — xLearn 0.4.0 documentation
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
格式的输出模型: