kaggle学习笔记-otto-baseline6-使用 RAPIDS TSNE 和项目矩阵分解可视化用户行为

本文主要是介绍kaggle学习笔记-otto-baseline6-使用 RAPIDS TSNE 和项目矩阵分解可视化用户行为,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

在Kaggle的Otto推荐系统竞赛中,项目ID是匿名的。所以我们不知道每个项目 id 指的是哪个。但是,通过项目矩阵分解,我们可以将匿名项目 ID 转换为有意义的嵌入。那么类似的嵌入将是类似的项目。如果我们将嵌入投影到 2D 平面(使用 TSNE、UMAP、PCA 等)并绘制它们,我们可以看到类似项目的集群。然后一个集群可能是服装,另一个集群可能是电子产品等。

使用项目嵌入的 2D 平面,我们可以绘制点和线,显示每个用户活动的进展。这使我们能够了解用户如何购物。用户是否浏览类似的项目,然后查看不同的项目,然后返回购买原始项目。用户主要探索类似项目吗?还是用户探索各种项目?下面的图将阐明用户行为,帮助我们理解数据并构建更好的功能和模型。

数据处理

import cudf
print('RAPIDS cuDF version',cudf.__version__)train = cudf.read_parquet('../input/otto-full-optimized-memory-footprint/train.parquet')
test = cudf.read_parquet('../input/otto-full-optimized-memory-footprint/test.parquet')train_pairs = cudf.concat([train, test])[['session', 'aid']]
del train, testtrain_pairs['aid_next'] = train_pairs.groupby('session').aid.shift(-1)
train_pairs = train_pairs[['aid', 'aid_next']].dropna().reset_index(drop=True)cardinality_aids = max(train_pairs['aid'].max(), train_pairs['aid_next'].max())
print('Cardinality of items is',cardinality_aids)

安装 Merlin 下载器

!pip install merlin-dataloader==0.0.2
from merlin.loader.torch import Loader train_pairs.to_pandas().to_parquet('train_pairs.parquet') # TRAIN WITH ALL DATA
train_pairs[-10_000_000:].to_pandas().to_parquet('valid_pairs.parquet')from merlin.loader.torch import Loader 
from merlin.io import Datasettrain_ds = Dataset('train_pairs.parquet')
train_dl_merlin = Loader(train_ds, 65536, True)

使用 PyTorch 矩阵分解模型学习项目嵌入

import torch
from torch import nnclass MatrixFactorization(nn.Module):def __init__(self, n_aids, n_factors):super().__init__()self.aid_factors = nn.Embedding(n_aids, n_factors, sparse=True)def forward(self, aid1, aid2):aid1 = self.aid_factors(aid1)aid2 = self.aid_factors(aid2)return (aid1 * aid2).sum(dim=1)class AverageMeter(object):"""Computes and stores the average and current value"""def __init__(self, name, fmt=':f'):self.name = nameself.fmt = fmtself.reset()def reset(self):self.val = 0self.avg = 0self.sum = 0self.count = 0def update(self, val, n=1):self.val = valself.sum += val * nself.count += nself.avg = self.sum / self.countdef __str__(self):fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'return fmtstr.format(**self.__dict__)valid_ds = Dataset('valid_pairs.parquet')
valid_dl_merlin = Loader(valid_ds, 65536, True)
from torch.optim import SparseAdamnum_epochs = 10
lr=0.1model = MatrixFactorization(cardinality_aids+1, 32)
optimizer = SparseAdam(model.parameters(), lr=lr)
criterion = nn.BCEWithLogitsLoss()model.to('cuda')
for epoch in range(num_epochs):for batch, _ in train_dl_merlin:model.train()losses = AverageMeter('Loss', ':.4e')aid1, aid2 = batch['aid'], batch['aid_next']aid1 = aid1.to('cuda')aid2 = aid2.to('cuda')output_pos = model(aid1, aid2)output_neg = model(aid1, aid2[torch.randperm(aid2.shape[0])])output = torch.cat([output_pos, output_neg])targets = torch.cat([torch.ones_like(output_pos), torch.zeros_like(output_pos)])loss = criterion(output, targets)losses.update(loss.item())optimizer.zero_grad()loss.backward()optimizer.step()model.eval()with torch.no_grad():accuracy = AverageMeter('accuracy')for batch, _ in valid_dl_merlin:aid1, aid2 = batch['aid'], batch['aid_next']output_pos = model(aid1, aid2)output_neg = model(aid1, aid2[torch.randperm(aid2.shape[0])])accuracy_batch = torch.cat([output_pos.sigmoid() > 0.5, output_neg.sigmoid() < 0.5]).float().mean()accuracy.update(accuracy_batch, aid1.shape[0])print(f'{epoch+1:02d}: * TrainLoss {losses.avg:.3f}  * Accuracy {accuracy.avg:.3f}')

提取项目嵌入

# EXTRACT EMBEDDINGS FROM MODEL EMBEDDING TABLE
embeddings = model.aid_factors.weight.detach().cpu().numpy()
print('Item Matrix Factorization embeddings have shape',embeddings.shape)

使用 RAPIDS TSNE 可视化用户行为

# IMPORT RAPIDS TSNE
from cuml import UMAP, TSNE, PCA
import matplotlib.pyplot as plt, numpy as np
import matplotlib.patches as mpatches, cuml
print('RAPIDS cuML version',cuml.__version__)# FIT TRANSFORM TSNE
em_2d = TSNE(n_components=2).fit_transform(embeddings)
print('TSNE embeddings have shape',em_2d.shape)
# LOAD TEST DATA
test = cudf.read_parquet('../input/otto-full-optimized-memory-footprint/test.parquet')
tmp = test.groupby('session').aid.agg('count').rename('n')
test = test.merge(tmp, left_on='session', right_index=True, how='left')
active_users = test.loc[test.n>20,'session'].unique().to_array()
test = test.sort_values(['session','ts'])
print('Test data shape:', test.shape )
test.head()

在这里插入图片描述

使用 TSNE 项目嵌入显示用户活动

对于 50 个随机用户(有 20 个或更多活动),我们将绘制他们的活动随时间的变化。顶部图显示与之交互的项目的类别,底部图显示项目交互的时间。当顶部图中的点彼此靠近时,则项目相似。例如,两件衣服将接近,而一件衣服将与一件电子产品相距甚远。

图中的数字按时间排序。数字 1 是用户与之交互的第一个项目,而数字 10 是第 10 个项目,依此类推。我们只显示一些数字,以便数字可读。橙色点是点击,绿色点是购物车,红点是买入。180万个蓝点都是独一无二的物品。我们观察到代表不同类别物品的蓝点簇。

x-y 平面表示不同的项目类别。如果用户呆在同一区域,那么他们正在购买类似的物品,例如服装部门。当用户的绘图从 x-y 平面的一个区域更改为另一个区域时,用户将更改为不同类别的项目,例如从服装购物转移到电子产品购物。我们观察不同类型的用户。一些用户浏览一个项目类别,而其他用户浏览各种项目类别。

# DISPLAY EDA FOR 50 USERS
for k in range(50):# SELECT ONE USER WITH 20+ CLICKSu = np.random.choice(active_users)dff = test.loc[test.session==u].to_pandas().reset_index(drop=True)tmp = dff.aid.valuesclicks = test.loc[(test.session==u)&(test['type']==0)].to_pandas().aid.valuescarts = test.loc[(test.session==u)&(test['type']==1)].to_pandas().aid.valuesorders = test.loc[(test.session==u)&(test['type']==2)].to_pandas().aid.values############## PLOT HISTORY BY ITEM CATEGORY############# PLOT CLICKS, CARTS, ORDERS OVER TSNE ITEM EMBEDDING PLOTplt.figure(figsize=(15,15))plt.scatter(em_2d[::25,0],em_2d[::25,1],s=1,label='All 1.8M items!')plt.plot(em_2d[tmp][:,0],em_2d[tmp][:,1],'-',color='orange')plt.scatter(em_2d[tmp][:,0],em_2d[tmp][:,1],color='orange',s=25,label='Click')plt.scatter(em_2d[carts][:,0],em_2d[carts][:,1],color='green',s=100,label='Cart')plt.scatter(em_2d[orders][:,0],em_2d[orders][:,1],color='red',s=250,label='Order')# PLOT NUMBERS OF ORDER VISITEDold_xy = []; pos = []for i,(x,y) in enumerate(zip(em_2d[tmp][:,0],em_2d[tmp][:,1])):new_location = Truefor j in old_xy:if (np.abs(x-j[0])<5) & (np.abs(y-j[1])<5):new_location = Falseif new_location:plt.text(x,y,f'{i+1}',size=18)old_xy.append( (x,y) ); pos.append(i)# LABEL PLOTplt.legend()plt.title(f'Test User {u} - {len(clicks)} clicks, {len(carts)} carts, {len(orders)} orders:',size=18)#plt.xlabel('Item category',size=16)plt.ylabel('\n\nItem category',size=16)plt.xticks([], [])plt.yticks([], [])plt.show()############## PLOT HISTORY BY DAY AND HOUR############mn = test.ts.min()dff['day'] = (dff.ts - mn) // (60*60*24)dff['hour'] = ((dff.ts - mn) % (60*60*24)) // (60*60)plt.figure(figsize=(15,3))xx = np.random.uniform(-0.2,0.2,len(dff))yy = np.random.uniform(-0.5,0.5,len(dff))plt.scatter(dff.day.values+xx, dff.hour.values+yy, s=25, color='orange')cidx = dff.loc[dff['type']==1].index.valuesoidx = dff.loc[dff['type']==2].index.valuesplt.scatter(dff.day.values[cidx]+xx[cidx], dff.hour.values[cidx]+yy[cidx], s=50, color='green')plt.scatter(dff.day.values[oidx]+xx[oidx], dff.hour.values[oidx]+yy[oidx], s=100, color='red')old_xy = []for i in range(len(dff)):if 1: #i in pos:x = dff.day.values[i]+xx[i]y = dff.hour.values[i]+yy[i]new_location = Truefor j in old_xy:if (np.abs(x-j[0])<0.5) & (np.abs(y-j[1])<4):new_location = Falseif new_location:plt.text(x, y, f'{i+1}', size=18)old_xy.append( (x,y) )plt.ylim((-1,25))plt.xlim((-1,7))plt.ylabel('Hour of Day',size=16)plt.xlabel('Day of Month',size=16)plt.yticks([0,4,8,12,16,20,24],['12am','4am','8am','noon','4pm','8pm','12am'])plt.xticks([0,1,2,3,4,5,6],['Mon\nAug 29th','Tue\nAug 30rd','Wed\nAug 31st','Thr\nSep 1st','Fri\nSep 2nd','Sat\nSep 3rd','Sun\nSep 4th'])c1 = mpatches.Patch(color='orange', label='Click')c2 = mpatches.Patch(color='green', label='Cart')c3 = mpatches.Patch(color='red', label='Order')plt.legend(handles=[c1,c2,c3])plt.show()print('\n\n\n\n\n\n\n')

这段代码产生的图片较多,这里只放两张作为示意
在这里插入图片描述
在这里插入图片描述

这篇关于kaggle学习笔记-otto-baseline6-使用 RAPIDS TSNE 和项目矩阵分解可视化用户行为的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/404838

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符