Quick Start¶
Installation¶
On MacOS/Linux First try:
pip install myfm
If it works, you can now try the examples.
If there is something nasty, then read the detailed installation guide and figure out what went wrong. Of course, feel free to create an issue on GitHub!
A toy example¶
Let us first look at how myfm.MyFMClassifier
works for a toy example provided in pyFM.
import myfm
from sklearn.feature_extraction import DictVectorizer
import numpy as np
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},
]
v = DictVectorizer()
X = v.fit_transform(train)
# Note that X is a sparse matrix
print(X.toarray())
# The target variable to be classified.
y = np.asarray([0, 1, 1, 0])
fm = myfm.MyFMClassifier(rank=4)
fm.fit(X,y)
# It also supports prediction for new unseen items.
fm.predict_proba(v.transform([{"user": "1", "item": "10", "age": 24}]))
As the example suggests, myfm.MyFMClassifier
takes
sparse matrices of scipy.sparse as its input.
In the above example, sklearn’s DictVectorizer
transforms the categorical variables (user id and movie id) into a one-hot encoded vectors.
As you can see, :py:class:MyFMClassifier: can make predictions against new (unseen) items despite the fact that it is an MCMC solver. This is possible because it simply retains all the intermediate (noisy) samples.
For more practical example with larger data, move on to Movielens examples .