Factorization Machines(论文笔记)

样例一:

一个简单的例子,train是一个字典,先将train进行“one-hot” coding,然后输入相关特征向量,可以预测相关性。

from pyfm import pylibfm
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)
print(X.toarray())
y = np.repeat(1.0,X.shape[0])
#print(X.shape[0])
fm = pylibfm.FM()
fm.fit(X,y)
fm.predict(v.transform({"user": "1", "item": "10", "age": 40}))
输出:
[[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.]]
4
Creating validation dataset of 0.01 of training for adaptive regularization
-- Epoch 1
Training log loss: 0.37518
array([0.9999684])

样例二:

是基于真实的电影评分数据来训练。数据集点击下载即可。

import numpy as np
from sklearn.feature_extraction import DictVectorizer
from pyfm import pylibfm# Read in data
def loadData(filename,path="ml-100k/"):data = []y = []users=set()items=set()with open(path+filename) as f:for line in f:(user,movieid,rating,ts)=line.split('\t')data.append({ "user_id": str(user), "movie_id": str(movieid)})y.append(float(rating))users.add(user)items.add(movieid)return (data, np.array(y), users, items)(train_data, y_train, train_users, train_items) = loadData("ua.base")
(test_data, y_test, test_users, test_items) = loadData("ua.test")
v = DictVectorizer()
X_train = v.fit_transform(train_data)
X_test = v.transform(test_data)# Build and train a Factorization Machine
fm = pylibfm.FM(num_factors=10, num_iter=100, verbose=True, task="regression", initial_learning_rate=0.001, learning_rate_schedule="optimal")fm.fit(X_train,y_train)# Evaluate
preds = fm.predict(X_test)
from sklearn.metrics import mean_squared_error
print("FM MSE: %.4f" % mean_squared_error(y_test,preds))
输出:
Creating validation dataset of 0.01 of training for adaptive regularization
-- Epoch 1
Training MSE: 0.59525
-- Epoch 2
Training MSE: 0.51804
-- Epoch 3
Training MSE: 0.49046
-- Epoch 4
Training MSE: 0.47458
-- Epoch 5
Training MSE: 0.46416
-- Epoch 6
Training MSE: 0.45662
-- Epoch 7
Training MSE: 0.45099
-- Epoch 8
Training MSE: 0.44639
-- Epoch 9
Training MSE: 0.44264
-- Epoch 10
Training MSE: 0.43949
-- Epoch 11
Training MSE: 0.43675
-- Epoch 12
Training MSE: 0.43430
-- Epoch 13
Training MSE: 0.43223
-- Epoch 14
Training MSE: 0.43020
-- Epoch 15
Training MSE: 0.42851
-- Epoch 16
Training MSE: 0.42691
-- Epoch 17
Training MSE: 0.42531
-- Epoch 18
Training MSE: 0.42389
-- Epoch 19
Training MSE: 0.42255
-- Epoch 20
Training MSE: 0.42128
-- Epoch 21
Training MSE: 0.42003
-- Epoch 22
Training MSE: 0.41873
-- Epoch 23
Training MSE: 0.41756
-- Epoch 24
Training MSE: 0.41634
-- Epoch 25
Training MSE: 0.41509
-- Epoch 26
Training MSE: 0.41391
-- Epoch 27
Training MSE: 0.41274
-- Epoch 28
Training MSE: 0.41149
-- Epoch 29
Training MSE: 0.41032
-- Epoch 30
Training MSE: 0.40891
-- Epoch 31
Training MSE: 0.40774
-- Epoch 32
Training MSE: 0.40635
-- Epoch 33
Training MSE: 0.40495
-- Epoch 34
Training MSE: 0.40354
-- Epoch 35
Training MSE: 0.40203
-- Epoch 36
Training MSE: 0.40047
-- Epoch 37
Training MSE: 0.39889
-- Epoch 38
Training MSE: 0.39728
-- Epoch 39
Training MSE: 0.39562
-- Epoch 40
Training MSE: 0.39387
-- Epoch 41
Training MSE: 0.39216
-- Epoch 42
Training MSE: 0.39030
-- Epoch 43
Training MSE: 0.38847
-- Epoch 44
Training MSE: 0.38655
-- Epoch 45
Training MSE: 0.38461
-- Epoch 46
Training MSE: 0.38269
-- Epoch 47
Training MSE: 0.38068
-- Epoch 48
Training MSE: 0.37864
-- Epoch 49
Training MSE: 0.37657
-- Epoch 50
Training MSE: 0.37459
-- Epoch 51
Training MSE: 0.37253
-- Epoch 52
Training MSE: 0.37045
-- Epoch 53
Training MSE: 0.36845
-- Epoch 54
Training MSE: 0.36647
-- Epoch 55
Training MSE: 0.36448
-- Epoch 56
Training MSE: 0.36254
-- Epoch 57
Training MSE: 0.36067
-- Epoch 58
Training MSE: 0.35874
-- Epoch 59
Training MSE: 0.35690
-- Epoch 60
Training MSE: 0.35511
-- Epoch 61
Training MSE: 0.35333
-- Epoch 62
Training MSE: 0.35155
-- Epoch 63
Training MSE: 0.34992
-- Epoch 64
Training MSE: 0.34829
-- Epoch 65
Training MSE: 0.34675
-- Epoch 66
Training MSE: 0.34538
-- Epoch 67
Training MSE: 0.34393
-- Epoch 68
Training MSE: 0.34258
-- Epoch 69
Training MSE: 0.34129
-- Epoch 70
Training MSE: 0.34006
-- Epoch 71
Training MSE: 0.33885
-- Epoch 72
Training MSE: 0.33773
-- Epoch 73
Training MSE: 0.33671
-- Epoch 74
Training MSE: 0.33564
-- Epoch 75
Training MSE: 0.33468
-- Epoch 76
Training MSE: 0.33375
-- Epoch 77
Training MSE: 0.33292
-- Epoch 78
Training MSE: 0.33211
-- Epoch 79
Training MSE: 0.33131
-- Epoch 80
Training MSE: 0.33065
-- Epoch 81
Training MSE: 0.33002
-- Epoch 82
Training MSE: 0.32930
-- Epoch 83
Training MSE: 0.32882
-- Epoch 84
Training MSE: 0.32813
-- Epoch 85
Training MSE: 0.32764
-- Epoch 86
Training MSE: 0.32722
-- Epoch 87
Training MSE: 0.32677
-- Epoch 88
Training MSE: 0.32635
-- Epoch 89
Training MSE: 0.32591
-- Epoch 90
Training MSE: 0.32550
-- Epoch 91
Training MSE: 0.32513
-- Epoch 92
Training MSE: 0.32481
-- Epoch 93
Training MSE: 0.32451
-- Epoch 94
Training MSE: 0.32421
-- Epoch 95
Training MSE: 0.32397
-- Epoch 96
Training MSE: 0.32363
-- Epoch 97
Training MSE: 0.32341
-- Epoch 98
Training MSE: 0.32319
-- Epoch 99
Training MSE: 0.32293
-- Epoch 100
Training MSE: 0.32268
FM MSE: 0.8873

 样例三:是一个分类的样例

import numpy as np
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split
from pyfm import pylibfmfrom sklearn.datasets import make_classificationX, y = make_classification(n_samples=1000,n_features=100, n_clusters_per_class=1)
data = [ {v: k for k, v in dict(zip(i, range(len(i)))).items()}  for i in X]X_train, X_test, y_train, y_test = train_test_split(data, y, test_size=0.1, random_state=42)v = DictVectorizer()
X_train = v.fit_transform(X_train)
X_test = v.transform(X_test)fm = pylibfm.FM(num_factors=50, num_iter=10, verbose=True, task="classification", initial_learning_rate=0.0001, learning_rate_schedule="optimal")fm.fit(X_train,y_train)from sklearn.metrics import log_loss
print("Validation log loss: %.4f" % log_loss(y_test,fm.predict(X_test)))
输出:
Creating validation dataset of 0.01 of training for adaptive regularization
-- Epoch 1
Training log loss: 2.12467
-- Epoch 2
Training log loss: 1.74185
-- Epoch 3
Training log loss: 1.42232
-- Epoch 4
Training log loss: 1.16085
-- Epoch 5
Training log loss: 0.94964
-- Epoch 6
Training log loss: 0.78052
-- Epoch 7
Training log loss: 0.64547
-- Epoch 8
Training log loss: 0.53758
-- Epoch 9
Training log loss: 0.45132
-- Epoch 10
Training log loss: 0.38187
Validation log loss: 1.3678

代码:pyFM/pyfm/pylibfm.py at master · coreylynch/pyFM (github.com)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/6431.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【MATLAB第59期】基于MATLAB的混沌退火粒子群CSAPSO-BP、SAPSO-BP、PSO-BP优化BP神经网络非线性函数拟合预测/回归预测对比

【MATLAB第59期】基于MATLAB的混沌退火粒子群CSAPSO-BP、SAPSO-BP、PSO-BP优化BP神经网络非线性函数拟合预测/回归预测对比 注意事项 不同版本matlab 不同电脑 加上数据集随机,BP权值阈值随机,进化算法种群随机,所以运行结果不一定和我运行…

集成了32位Cortex®M0内核XMC1302T038X0200AB、XMC1302Q040X0200AB 32MHz 200KB 闪存 工业MCU

XMC1000 32位工业 MCU 将 ARM Cortex™-M0 核心与领先的 65nm 制造工艺相结合,克服了目前 8 位设计的局限。XMC1000系列让目前的 8 位用户有机会享受 32 位的功耗,同时不在价格或易用性上做出妥协。XMC1000 在其细分市场提供最为广泛的闪存产品线&#x…

3分钟,快速上手Postman接口测试

Postman是一个用于调试HTTP请求的工具,它提供了友好的界面帮助分析、构造HTTP请求,并分析响应数据。实际工作中,开发和测试基本上都有使用Postman来进行接口调试工作。有一些其他流程的工具,也是模仿的Postman的风格进行接口测试工…

下载|GitLab 2023 年 DevSecOps 全球调研报告:安全左移深入人心、AI/ML 蔚然成风

目录 谁应该对应用程序安全负主要责任? 安全实践的最大挑战 AI 驱动研发,提升研发效率 各个角色使用的工具数量是多少? 一体化 DevSecOps 平台有哪些优势? 56%、74%、71%、65%、57% 这些数字和 DevSecOps 结合在一起&#xf…

android adb命令获取处于当前屏幕的Activity

android adb命令获取处于当前屏幕的Activity 使用adb命令: adb shell dumpsys activity activities 输出,例如: ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities) Display #0 (activities from top to bottom): * Task{38ef601 #5281 typ…

Java当中的栈

栈的理解 栈(Stack)是一种受限的线性数据结构,所谓受限是指栈只暴露栈顶和栈底的操作,其底层是由数组实现的。栈的特性是先进后出。 常用方法 注意上面的peek()方法和pop()方法的区别! 实例 import java.util.Stack…

grpc中间件之链路追踪(otel+jaeger)

参考文档 https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/examples/client/main.go https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/examples/server/main.go https://github.com/open-telemetry/opentelemetry-go/blob/main/example/jaeg…

【Rust 基础篇】Rust 通道实现单个消费者多个生产者模式

导言 在 Rust 中,我们可以使用通道(Channel)来实现单个消费者多个生产者模式,简称为 MPMC。MPMC 是一种常见的并发模式,适用于多个线程同时向一个通道发送数据,而另一个线程从通道中消费数据的场景。本篇博…

搭建elasticsearch过程中遇到问题记录

##1.新建用户 解压es之前,新建用户做后续操作,root用户无法启动es。 ##2.增大普通用户可打开最大文件数 ##3.增大普通用户可启动线程数 ##4.增大用户可使用虚拟内存 编辑 /etc/sysctl.conf,追加以下内容: vm.max_map_count262144…

sql中group by 的使用

1、概述 Group By 从字面意义上理解就是根据By指定的规则对数据进行分组,所谓的分组就是将一个数据集划分为若干个小区域,然后针对若干个小区域进行数据处理 2、原始表 3、简单的Group By 示例1 select 类别,数量 as 数量之和 from A gro…

android studio 离线打包配置push模块

1.依赖引入 SDK\libs aps-release.aar, aps-unipush-release.aar, gtc.aar, gtsdk-3.2.11.0.aar, 从android studio的sdk中找到对应的包放到HBuilder-Integrate-AS\simpleDemo\libs下面 2.打开build.gradle,在defaultConfig添加manifestPlaceholders节点&#xff0c…

【代码随想录 | Leetcode | 第十天】哈希表 | 三数之和 | 四数之和

前言 欢迎来到小K的Leetcode|代码随想录|专题化专栏,今天将为大家带来哈希法~三数之和 | 四数之和的分享✨ 目录 前言15. 三数之和18. 四数之和总结 15. 三数之和 ✨题目链接点这里 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], num…

JVM运行时区域——对象创建内存分配过程

新创建的对象,都存放在伊甸园区域,当垃圾回收时,将伊甸园区域的垃圾数据销毁,然后将存活的对象转移到幸存者0区域,之后创建的新的对象还是存放在伊甸园区域,等到再次垃圾回收后,将伊甸园区域和幸…

RabbitMQ帮助类的封装

RabbitMQ帮助类的封装 基本部分 public class RabbitMQInvoker {#region Identy private static IConnection _CurrentConnection null;private readonly string _HostName null;private readonly string _UserName null;private readonly string _Password null;#endreg…

开篇词 | 「安卓学习路线」

操作系统 Java 集合、反射、泛型、并发编程(线程安全,锁机制、线程隔离),IO 流,JVM(内存结构、垃圾回收) 安卓(通过实践项目来学习基础知识) 基础知识 Activity 和 Ser…

HTML5——基础知识及使用

HTML 文件基本结构 <html><head><title>第一个页面</title></head><body>hello world</body> </html> html 标签是整个 html 文件的根标签(最顶层标签).head 标签中写页面的属性.body 标签中写的是页面上显示的内容.title 标…

Ansible自动化运维学习——综合练习

目录 (一)练习一 1.新建一个role——app 2.创建文件 3.删除之前安装的httpd服务和apache用户 4.准备tasks任务 (1)创建组group.yml (2)创建用户user.yml (3)安装程序yum.yml (4)修改模板httpd.conf.j2 (5)编写templ.yml (6)编写start.yml (7)编写copyfile.yml (8…

Python爬虫技术及其原理详解

概要 随着互联网的发展&#xff0c;大量的数据被存储在网络上&#xff0c;而我们需要从中获取有用的信息。Python作为一种功能强大且易于学习的编程语言&#xff0c;被广泛用于网络爬虫的开发。本文将详细介绍Python爬虫所需的技术及其原理&#xff0c;并提供相关的代码案例。 …

【牛客】4. 01串修改

腾讯音乐娱乐集团2023校园招聘技术类岗位编程题四 【牛客】4. 01串修改 给定一个只包含’0’和’1’两种字符的字符串&#xff0c;每次操作可以选择相邻的两个字符&#xff0c;将它们同时变成’0’或者同时变成’1’。请问最少多少次操作后&#xff0c;所有的字符都相同&…

Mac电脑文件夹无权限问题

sudo cp 16.5.zip /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 走到之前的folder &#xff0c;右键选择get info更改權限, 再應用到所有子文件夹 右下解鎖再加自己Read & Write, -右邊拉下應該可以應用到所有子文件 这样就可以…