{"id":13742839,"url":"https://github.com/coreylynch/pyFM","last_synced_at":"2025-05-09T00:31:51.572Z","repository":{"id":5961116,"uuid":"7182417","full_name":"coreylynch/pyFM","owner":"coreylynch","description":"Factorization machines in python","archived":false,"fork":false,"pushed_at":"2020-10-01T09:59:33.000Z","size":1406,"stargazers_count":925,"open_issues_count":44,"forks_count":310,"subscribers_count":44,"default_branch":"master","last_synced_at":"2025-04-19T04:18:32.510Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coreylynch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-15T18:20:11.000Z","updated_at":"2025-04-03T23:27:03.000Z","dependencies_parsed_at":"2022-06-25T23:02:19.242Z","dependency_job_id":null,"html_url":"https://github.com/coreylynch/pyFM","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreylynch%2FpyFM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreylynch%2FpyFM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreylynch%2FpyFM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreylynch%2FpyFM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coreylynch","download_url":"https://codeload.github.com/coreylynch/pyFM/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253171026,"owners_count":21865275,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T05:00:36.822Z","updated_at":"2025-05-09T00:31:51.308Z","avatar_url":"https://github.com/coreylynch.png","language":"Python","readme":"# Factorization Machines in Python\n\nThis is a python implementation of Factorization Machines [1]. This uses stochastic gradient descent with adaptive regularization as a learning method, which adapts the regularization automatically while training the model parameters. See [2] for details. From libfm.org: \"Factorization machines (FM) are a generic approach that allows to mimic most factorization models by feature engineering. This way, factorization machines combine the generality of feature engineering with the superiority of factorization models in estimating interactions between categorical variables of large domain.\"\n\n[1] Steffen Rendle (2012): Factorization Machines with libFM, in ACM Trans. Intell. Syst. Technol., 3(3), May.\n[2] Steffen Rendle: Learning recommender systems with adaptive regularization. WSDM 2012: 133-142\n\n## Installation\n```\npip install git+https://github.com/coreylynch/pyFM\n```\n\n## Dependencies\n* numpy\n* sklearn\n\n## Training Representation\nThe easiest way to use this class is to represent your training data as lists of standard Python dict objects, where the dict elements map each instance's categorical and real valued variables to its values. Then use a [sklearn DictVectorizer](http://scikit-learn.org/dev/modules/generated/sklearn.feature_extraction.DictVectorizer.html#sklearn.feature_extraction.DictVectorizer) to convert them to a design matrix with a one-of-K or “one-hot” coding.\n\nHere's a toy example\n```python\nfrom pyfm import pylibfm\nfrom sklearn.feature_extraction import DictVectorizer\nimport numpy as np\ntrain = [\n\t{\"user\": \"1\", \"item\": \"5\", \"age\": 19},\n\t{\"user\": \"2\", \"item\": \"43\", \"age\": 33},\n\t{\"user\": \"3\", \"item\": \"20\", \"age\": 55},\n\t{\"user\": \"4\", \"item\": \"10\", \"age\": 20},\n]\nv = DictVectorizer()\nX = v.fit_transform(train)\nprint(X.toarray())\n[[ 19.   0.   0.   0.   1.   1.   0.   0.   0.]\n [ 33.   0.   0.   1.   0.   0.   1.   0.   0.]\n [ 55.   0.   1.   0.   0.   0.   0.   1.   0.]\n [ 20.   1.   0.   0.   0.   0.   0.   0.   1.]]\ny = np.repeat(1.0,X.shape[0])\nfm = pylibfm.FM()\nfm.fit(X,y)\nfm.predict(v.transform({\"user\": \"1\", \"item\": \"10\", \"age\": 24}))\n```\n\n## Getting Started\nHere's an example on some real  movie ratings data.\n\nFirst get the smallest movielens ratings dataset from http://www.grouplens.org/system/files/ml-100k.zip.\nml-100k contains the files u.item (list of movie ids and titles) and u.data (list of user_id, movie_id, rating, timestamp).\n```python\nimport numpy as np\nfrom sklearn.feature_extraction import DictVectorizer\nfrom pyfm import pylibfm\n\n# Read in data\ndef loadData(filename,path=\"ml-100k/\"):\n    data = []\n    y = []\n    users=set()\n    items=set()\n    with open(path+filename) as f:\n        for line in f:\n            (user,movieid,rating,ts)=line.split('\\t')\n            data.append({ \"user_id\": str(user), \"movie_id\": str(movieid)})\n            y.append(float(rating))\n            users.add(user)\n            items.add(movieid)\n\n    return (data, np.array(y), users, items)\n\n(train_data, y_train, train_users, train_items) = loadData(\"ua.base\")\n(test_data, y_test, test_users, test_items) = loadData(\"ua.test\")\nv = DictVectorizer()\nX_train = v.fit_transform(train_data)\nX_test = v.transform(test_data)\n\n# Build and train a Factorization Machine\nfm = pylibfm.FM(num_factors=10, num_iter=100, verbose=True, task=\"regression\", initial_learning_rate=0.001, learning_rate_schedule=\"optimal\")\n\nfm.fit(X_train,y_train)\nCreating validation dataset of 0.01 of training for adaptive regularization\n-- Epoch 1\nTraining MSE: 0.59477\n-- Epoch 2\nTraining MSE: 0.51841\n-- Epoch 3\nTraining MSE: 0.49125\n-- Epoch 4\nTraining MSE: 0.47589\n-- Epoch 5\nTraining MSE: 0.46571\n-- Epoch 6\nTraining MSE: 0.45852\n-- Epoch 7\nTraining MSE: 0.45322\n-- Epoch 8\nTraining MSE: 0.44908\n-- Epoch 9\nTraining MSE: 0.44557\n-- Epoch 10\nTraining MSE: 0.44278\n...\n-- Epoch 98\nTraining MSE: 0.41863\n-- Epoch 99\nTraining MSE: 0.41865\n-- Epoch 100\nTraining MSE: 0.41874\n\n# Evaluate\npreds = fm.predict(X_test)\nfrom sklearn.metrics import mean_squared_error\nprint(\"FM MSE: %.4f\" % mean_squared_error(y_test,preds))\nFM MSE: 0.9227\n\n```\n## Classification example\n```python\nimport numpy as np\nfrom sklearn.feature_extraction import DictVectorizer\nfrom sklearn.cross_validation import train_test_split\nfrom pyfm import pylibfm\n\nfrom sklearn.datasets import make_classification\n\nX, y = make_classification(n_samples=1000,n_features=100, n_clusters_per_class=1)\ndata = [ {v: k for k, v in dict(zip(i, range(len(i)))).items()}  for i in X]\n\nX_train, X_test, y_train, y_test = train_test_split(data, y, test_size=0.1, random_state=42)\n\nv = DictVectorizer()\nX_train = v.fit_transform(X_train)\nX_test = v.transform(X_test)\n\nfm = pylibfm.FM(num_factors=50, num_iter=10, verbose=True, task=\"classification\", initial_learning_rate=0.0001, learning_rate_schedule=\"optimal\")\n\nfm.fit(X_train,y_train)\n\nCreating validation dataset of 0.01 of training for adaptive regularization\n-- Epoch 1\nTraining log loss: 1.91885\n-- Epoch 2\nTraining log loss: 1.62022\n-- Epoch 3\nTraining log loss: 1.36736\n-- Epoch 4\nTraining log loss: 1.15562\n-- Epoch 5\nTraining log loss: 0.97961\n-- Epoch 6\nTraining log loss: 0.83356\n-- Epoch 7\nTraining log loss: 0.71208\n-- Epoch 8\nTraining log loss: 0.61108\n-- Epoch 9\nTraining log loss: 0.52705\n-- Epoch 10\nTraining log loss: 0.45685\n\n# Evaluate\nfrom sklearn.metrics import log_loss\nprint \"Validation log loss: %.4f\" % log_loss(y_test,fm.predict(X_test))\nValidation log loss: 1.5025\n\n```\n","funding_links":[],"categories":["Machine Learning","Table of Contents"],"sub_categories":["Kernel Methods"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreylynch%2FpyFM","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoreylynch%2FpyFM","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreylynch%2FpyFM/lists"}