kaggle学习笔记-otto-baseline10-实现拉狄克简单共访矩阵极坐标

这篇具有很好参考价值的文章主要介绍了kaggle学习笔记-otto-baseline10-实现拉狄克简单共访矩阵极坐标。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

DEBUG = False
!pip install polars
!pip install snoop
from collections import defaultdict, Counter
import gc
from snoop import pp
import polars as pl
import pandas as pd
import numpy as np
import random
from polars.testing import assert_frame_equal, assert_series_equal
from datetime import datetime

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
pd.set_option('display.max_colwidth', None)
cfg = pl.Config.restore_defaults()  
pl.Config.set_tbl_rows(50)  
pl.Config.set_fmt_str_lengths(1000)
if DEBUG: fraction_of_sessions_to_use = 0.00001
else: fraction_of_sessions_to_use = 1
train_ms = pl.scan_parquet('/kaggle/input/otto-radek-style-polars/train_ms.parquet')
test_ms = pl.scan_parquet('/kaggle/input/otto-radek-style-polars/test_ms.parquet')
sample_sub = pl.scan_csv('/kaggle/input/otto-recommender-system/sample_submission.csv')

子集训练和测试并将它们连接在一起

%%time
lucky_sessions_train = (
    train_ms
    .select([
        pl.col('session').unique().sample(frac=fraction_of_sessions_to_use, seed=42)
    ])
    .collect()
    .to_series().to_list()
)

lucky_sessions_test = (
    test_ms
    .select([
        pl.col('session').unique().sample(frac=fraction_of_sessions_to_use, seed=42)
    ])
    .collect()
    .to_series().to_list()
)


subset_of_train = (
    train_ms
    .filter(pl.col('session').is_in(lucky_sessions_train))

)

subset_of_test = (
    test_ms
    .filter(pl.col('session').is_in(lucky_sessions_test))

)

subsets = pl.concat([subset_of_train, subset_of_test]).collect()
sessions = subsets.select('session').unique().to_series().to_list()
pp(lucky_sessions_train[:3], len(lucky_sessions_train), lucky_sessions_test[:3], len(lucky_sessions_test),
   subset_of_train.collect().height, subset_of_test.collect().height, subsets.height)

创建共同访问矩阵
共同访问矩阵只是探索以下想法/问题的名称
一个辅助设备/产品与同一会话中或所有会话中的其他辅助设备/产品之间是否存在任何关系?
是否有一些辅助工具与某些辅助工具更相似,而与其他辅助工具更不同?
当查看此辅助工具时,是否有可能,某些辅助工具比其他辅助工具更有可能被点击/购物车/订购?
我们能否为每个会话将辅助工具配对在一起并计算配对的次数?
由于一个辅助(例如,“122”)可以有许多配对伙伴,通过计算配对(“122”,配对伙伴)的出现次数,我们能否找到援助“122”最常见的配对伙伴?
下一次点击、购物车或订单可能是测试会话的最后一次辅助(或所有辅助)最常见的配对伙伴吗?
建立共同访问矩阵的第一个挑战是如何配对
在拉狄克的笔记本中,配对逻辑如下

仅使用每个会话的最后 30 个辅助工具相互配对
删除相同伙伴的对
保留一天内右伴侣在左伴侣之后的配对
我们可以调整配对逻辑来改变我们的共同访问矩阵

%%time 
next_AIDs = defaultdict(Counter)
chunk_size = 300000
for i in range(0, len(sessions), chunk_size):
    current_chunk = (
        subsets
        .filter(pl.col('session').is_between(sessions[i], sessions[np.min([i+chunk_size-1, len(sessions)-1])], closed='both'))
        .unique() # no duplicates
        .groupby('session').tail(30)
    )
    current_chunk = (
        current_chunk
        .join(current_chunk, on='session', suffix='_right')
        .sort(['session', 'aid', 'aid_right']) # nice view
        .filter(pl.col('aid') != pl.col('aid_right')) # no need for pairs of themselves
        .with_columns([
            ((pl.col('ts_right') - pl.col('ts'))/(24*60*60*1000)).alias('days_elapsed') # differentiate aid_right is after or before aid in days
        ])
        .filter((pl.col('days_elapsed')>=0) & (pl.col('days_elapsed') <=1)) # only pairs whose aid_rights are after aid within 24 hrs
    )

    # defaultdict + Counter is super faster than pure polars solution
    for aid_x, aid_y in zip(current_chunk.select('aid').to_series().to_list(), current_chunk.select('aid_right').to_series().to_list()):
        next_AIDs[aid_x][aid_y] += 1

    print(f'{int(np.ceil(i/chunk_size))} out of {int(np.ceil(len(sessions)/chunk_size))} - {np.min([i+chunk_size-1, len(sessions)-1])} sessions are done')
len(next_AIDs)

polars版本和pandas版本差不多,在这里看拉狄克的速度

del train_ms, subset_of_train, subsets
gc.collect()

使用共同访问矩阵在测试会话中辅助人员少于 20 人时提供帮助候选人
拉狄克在这里向我们展示了两件事:

如何创建特征(基于时间、类型和出现的权重)以在测试会话中选择 20 个辅助工具
如何从共同访问矩阵中选择候选人?
在测试会话中为每种辅助工具选取 20 种最常见的辅助工具,并将它们放入候选列表中
从候选列表中选择 40 种最常见的辅助工具,如果它们是新参加会话的,请将它们添加到测试会话的辅助工具中
然后选择前 20 个辅助工具

%%time
lists_aids_types = (
    test_ms
    .unique() #
    .groupby('session')
    .agg([
        pl.col('aid').list().alias('test_session_AIDs'),
        pl.col('type').list().alias('test_session_types'),        
    ])
    .collect()
)

lists_aids_types.head()

%%time
labels = []
session_types = ['clicks', 'carts', 'orders']
no_data = 0
no_data_all_aids = 0
type_weight_multipliers = {0: 1, 1: 6, 2: 3}
test_session_AIDs = lists_aids_types.select('test_session_AIDs').to_series().to_list()
test_session_types = lists_aids_types.select('test_session_types').to_series().to_list()

# take each session's aids and types
for AIDs, types in zip(test_session_AIDs, test_session_types):

    # if the session has more than 20 aids
    if len(AIDs) >= 20: 
        # np.logspace: Return numbers spaced evenly on a log scale.
        # `-1` is to ensure the weights ranges between [0,1]
        # the weights is given to AIDs based on the time order or chronological order
        weights=np.logspace(start=0.1,stop=1,num=len(AIDs),base=2, endpoint=True)-1 
        
        # create a defaultdict for this session only
        # anything added into this dict will have a default value 0
        # try `aids_temp[1]` and `aids_temp`
        aids_temp=defaultdict(lambda: 0)
        
        # in each sess, an aid may occur multiples in multiple types at different time, 
        # the line below is to take all 3 factors into account to value the importance of this aid to the session
        # each unique aid and its aggregated weight are stored in a defaultdict
        for aid,w,t in zip(AIDs,weights,types): 
            aids_temp[aid]+= w * type_weight_multipliers[t]
          
        # let's 
        sorted_aids=[k for k, v in sorted(aids_temp.items(), key=lambda item: -item[1])]

        # when using the polars below to replace the line above, it is actually 2 times slower
        # aid = [key for (key, value) in aids_temp.items()]
        # adwt = [value for (key, value) in aids_temp.items()]
        # sorted_aids = (
        #     pl.DataFrame([aid, adwt], columns=['aid', 'weight'])
        #     .sort('weight', reverse=True)
        #     .select('aid').to_series().to_list()
        # )

        # take the 20 aids with the largest weights from this session as one list and append it into a new list `labels`
        labels.append(sorted_aids[:20])
    
    # when this session has less than 20 aids
    else:
        # reverse the order of AIDs (a list of aids of this session) and remove the duplicated aids
        AIDs = list(dict.fromkeys(AIDs[::-1])) # python version
        
        # If using this polars below to replace the line above, it is infinitely slower
        # AIDs = pl.Series('aid', AIDs).unique().reverse().to_list() # polars version

        # keep track of the length of new AIDs above
        AIDs_len_start = len(AIDs)
        

        candidates = []
        # take each unique aid of this session, access its the 20 most common pair-partners and their counts
        # insert the list of the 20 most common pair-partner aids into another list `candidates` (only a pure list )
        # in the end, this `candidates` list is a lot and has many duplicates too
        for AID in AIDs:
            if AID in next_AIDs: candidates += [aid for aid, count in next_AIDs[AID].most_common(20)]
                
        # take the 40 most common aids from `candidates`, and if they are already inside AIDs of this session, 
        # then insert them into AIDs (still a pure list because of `+`, and `append` can't do it)
        AIDs += [AID for AID, cnt in Counter(candidates).most_common(40) if AID not in AIDs]
        
        # but we still only take the first 20 aids from AIDs as this session's prediction and store it in `labels`
        labels.append(AIDs[:20])
        
        # if no candidates are generated, count 1 to `no_data`
        # if candidates == []: no_data += 1 # this variable is actually not used by Radek
        
        # keep an account of the num of aids in this session and all sessions which adding no candidates
        if AIDs_len_start == len(AIDs): no_data_all_aids += 1
sample_sub.fetch().head()

创建提交标签文章来源地址https://www.toymoban.com/news/detail-400243.html

%%time
(
    pl.DataFrame({'session': lists_aids_types.select('session').to_series().to_list(), 
                  'labels': labels})
    .with_columns([
        pl.col('labels').arr.eval(pl.element().cast(pl.Utf8)).arr.join(' '),
        (pl.col('session')+"_clicks").alias('clicks'),
        (pl.col('session')+"_carts").alias('carts'),
        (pl.col('session')+"_orders").alias('orders'),        
    ])
    .select([
        'session',
        pl.concat_list(['clicks', 'carts', 'orders']).alias('session_type'),
        'labels'
    ])
    .explode('session_type')
    .sort('session')
    .select(pl.exclude('session'))
    .write_csv('submission.csv')
)
print(f'Test sessions that we did not manage to extend based on the co-visitation matrix: {no_data_all_aids}')
pl.read_csv('submission.csv').shape
sample_sub.collect().shape
# from matplotlib import pyplot as plt

# plt.hist([len(l) for l in labels]);
# plt.suptitle('Distribution of predicted sequence lengths');

到了这里,关于kaggle学习笔记-otto-baseline10-实现拉狄克简单共访矩阵极坐标的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • AI量化模型预测——baseline学习笔记

    1. 赛题名称         AI量化模型预测 2. 赛题理解         本赛事是一个量化金融挑战,旨在通过大数据与机器学习的方法,使用给定的训练集和测试集数据,预测未来中间价的移动方向。参赛者需要理解市场行为的原理,创建量化策略,并利用过去不超过100个数据点的

    2024年02月14日
    浏览(30)
  • kaggle学习笔记-情感和地理空间分析

    秘鲁食品评论中的情绪和地理空间分析 自然语言处理 (NLP) 是人工智能的一个分支,致力于让计算机能够像人类一样理解文本和口语单词。 另一方面,地理空间分析是对图像、GPS、卫星摄影和历史数据的收集、显示和操作,这些数据以地理坐标明确描述,或以街道地址、邮政

    2024年02月16日
    浏览(28)
  • 第十二届“中国软件杯”大赛:A10-基于机器学习的分布式系统故障诊断系统——baseline(一)

    在分布式系统中某个节点发生故障时,故障会沿着分布式系统的拓扑结构进行传播,造成自身节点及其邻接节点相关的KPI指标和发生大量日志异常。本次比赛提供分布式数据库的故障特征数据和标签数据,其中特征数据是系统发生故障时的KPI指标数据,KPI指标包括由feature0、

    2024年02月11日
    浏览(35)
  • 深度学习笔记(kaggle课程《Intro to Deep Learning》)

    深度学习是一种机器学习方法,通过构建和训练深层神经网络来处理和理解数据。它模仿人脑神经系统的工作方式,通过多层次的神经网络结构来学习和提取数据的特征。深度学习在图像识别、语音识别、自然语言处理等领域取得了重大突破,并被广泛应用于人工智能技术中

    2024年02月13日
    浏览(34)
  • AI绘画基于 Kaggle 10 分钟搭建 Stable Diffusion(保姆级教程)

    当前最火的、也是日常绘画最常用两个 AI 绘画工具就属 Midjourney 和 Stable Diffusion 了。 而相对于 Midjourney(基础版也要 $10 / month)来说,Stable Diffusion 最大的好处就是: 完全免费! (免费啊,宝子们) 完全开源! 但是 Stable Diffusion 的 安装部署比较复杂 ,而且 对电脑配置要求

    2024年02月11日
    浏览(43)
  • 机器学习数据集:Kaggle

    Kaggle成立于2010年,是一个进行数据发掘和预测竞赛的在线平台。从公司的角度来讲,可以提供一些数据,进而提出一个实际需要解决的问题;从参赛者的角度来讲,他们将组队参与项目,针对其中一个问题提出解决方案,最终由公司选出的最佳方案可以获得5K-10K美金的奖金。

    2024年02月08日
    浏览(28)
  • AI绘画StableDiffusion:云端在线版使用笔记分享(Kaggle版)

    玩AI绘画(SD),自己电脑配置不够?今天给大家介绍一下如何baipiao在线版AI绘画StableDiffusion。 Kaggle 是世界上最大的数据科学社区,拥有强大的工具和资源,可帮助您实现数据科学目标。(每周可以免费使用30个小时)。 打开如下链接,复制并编辑后,创建你自己的代码 ht

    2024年02月15日
    浏览(39)
  • 李宏毅机器学习 hw7 boss baseline分享

    使用bert来做问答任务 答案是都是可以在 Document 找到的,输入 Document 和 Query 输出两个数字分别表示答案在Document中的开始和结束位置。 输入格式如下: doc stride ,初始时 Doc stride 等于 max_paragraph_len ,这样会导致在测试时如果答案在边界附近就会被切割到两个不同的 window 中

    2024年02月06日
    浏览(26)
  • 李宏毅机器学习 hw2 boss baseline 解析

    Multiclass Classification ,让你判断给定的向量是属于哪一个 phoneme ,由于一个 phoneme 可能包含好多个向量,所以要对数据进行处理,对向量进行拼接。 不同baseline 要求 先给出我最终使用的过boss baseline的方法,后面再介绍我一步步的思考过程。 助教提示过boss baseline要使用RNN模型

    2023年04月09日
    浏览(28)
  • 李宏毅 2022机器学习 HW2 strong baseline 上分路线

    baseline 增加concat_nframes (提升明显) 增加batchnormalization 和 dropout 增加hidden layer宽度至512 (提升明显) 提交文件命名规则为 prediction_{concat_nframes} [{n_hidden_layers} {dropout}_bn].csv (2%) Implement 2 models with approximately the same number of parameters, (A) one narrower and deeper (e.g. hidden_layers=6, hidden

    2024年02月10日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包