Python安装 libFM

编译libFm

主页: http://www.libfm.org/
源代码:https://github.com/srendle/libfm
libFm手册(libFM 1.4.2 manual):http://www.libfm.org/libfm-1.42.manual.pdf
LibFM使用手册中文版: https://blog.csdn.net/Chloezhao/article/details/53462411

下载源码,进入文件夹编译:make all
Python安装 libFM_第1张图片

Python 中使用FM

python工具包

  • PyFM: https://github.com/coreylynch/pyFM
    安装 pip install git+https://github.com/coreylynch/pyFM
    pip install pyFM 会安装了另一个同名库,注意!

  • Cython - https://pypi.python.org/pypi/Cython/
    安装 pip install Cython

  • 实例(更多例子见PyFM的github网站)

In [1]: from pyfm import pylibfm
In [2]: from sklearn.feature_extraction import DictVectorizer
In [3]: import numpy as np

In [4]: train = [
	{"user": "1", "item": "5", "age": 19},
	{"user": "2", "item": "43", "age": 33},
	{"user": "3", "item": "20", "age": 55},
	{"user": "4", "item": "10", "age": 20},
]
In [5]: v = DictVectorizer()
In [6]: X = v.fit_transform(train)

In [7]: print(X.toarray())
[[ 19.   0.   0.   0.   1.   1.   0.   0.   0.]
 [ 33.   0.   0.   1.   0.   0.   1.   0.   0.]
 [ 55.   0.   1.   0.   0.   0.   0.   1.   0.]
 [ 20.   1.   0.   0.   0.   0.   0.   0.   1.]]

In [8]:  y = np.repeat(1.0,X.shape[0])
In [9]:  fm = pylibfm.FM()
In [10]:  fm.fit(X,y) 
Creating validation dataset of 0.01 of training for adaptive regularization
-- Epoch 1
Training log loss: 0.37194

In [11]:  m.predict(v.transform({"user": "1", "item": "10", "age": 24}))
Out[11]: array([ 0.99795679])

你可能感兴趣的:(机器学习,Python)