天池竞赛-淘宝穿衣搭配(数据预处理部分)

赛题简介

淘宝网是中国深受欢迎的网购零售平台,其中服饰鞋包行业占据市场的绝大部分份额,围绕着淘宝诞生了一大批优秀的服饰鞋包导购类的产品。穿衣搭配是服饰鞋包导购中非常重要的课题,它所延伸出的技术、算法能广泛应用到大数据营销几乎所有场景中,如搜索、推荐和营销服务。淘宝穿衣搭配算法竞赛将为参赛者提供搭配专家和达人生成的搭配组合数据,百万级别的淘宝商品的文本和图像数据,同时还将提供用户的脱敏行为数据。期待参赛者能从以上行为、文本和图像数据中挖掘穿衣搭配模型,为用户提供个性化、优质的、专业的穿衣搭配方案。

数据格式

搭配套餐数据:dim_fashion_match_sets

coll_id || bigint || 搭配套餐ID || 1000

item_list || string || 搭配套餐中的商品ID列表(分号分隔,每个分号下可能会有不只一个商品,后面为可替换商品,逗号分隔)|| 1002,1003,1004;439201;1569773,234303;19836

dim_fashion_match_sets 样例:

1 160870;3118604
2 1842610;2741506
3 893028;993019,1375599,1913565,3036503;2849440;2546147;2329974,2661094,347849;884801,127779,3122713;2338561
4 2612866;1272124;2181942
5 3128145;2683359;855149

商品信息表:dim_items

item_id || bigint || 商品ID || 439201
cat_id || bigint || 商品所属类目ID || 16
terms || string || 商品标题分词后的结果,顺序被打乱 || 5263,2541,2876263
img_data || string || 商品图片(注:初赛图片直接以文件形式提供,图片文件命名为item_id.jpg,表中无该字段)

dim_items样例:

29 155 123950,53517,106068,59598,7503,171811,25618,147905,203432,123580,178091,154365,127004,31897,82406
49 228 73035,33202,116593,48909,92233,181255,127004,38910,182506,181709,207662,154365,103661,24893
59 284 123950,38910,22837,5026,15459,47776,158346,101881,131272,176333,196079,23211,148988,144893,167633

用户历史行为表:user_bought_history

user_id || bigint || 用户ID || 62378843278
item_id || bigint || 商品ID || 439201
create_at || string || 行为日期(购买)|| 20140911

user_bought_history样本:

1915871 8 20150417
4371603 8 20150418
8034236 8 20150516
6135829 8 20150405
11650079 8 20150404
5324797 23 20150509
7969649 23 20150413

待预测的商品列表:test_items

item_id || bigint || 商品ID || 90832747

test_items样例:

1417
2227
3967
7237
8467
10477
10777
12547

Data Loading

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 03 13:53:48 2015
@author: Zhang_Jun
"""

import pandas as pd

# load data
dim_fashion_matchsets = pd.read_table('.\data\dim_fashion_matchsets.txt',\
sep='\s+',names = ['coll_id','item_list'])

dim_items = pd.read_table('.\data\dim_items.txt',\
sep = '\s+' , names = ['item_id','cat_id','terms','img_data'])

user_bought_history = pd.read_table('.\data\user_bought_history.txt',\
sep = '\s+' , names = ['user_id','item_id','create_at'])

test_items = pd.read_table('.\data\items.txt', names = ['test_items_id'])

Tool Preparation

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 03 15:49:46 2015

@author: Zhang_Jun
"""
from collections import Counter
import itertools

#-----------------------------------------------------------

class item(object):
    def __init__(self,ID):
        self.id = ID
        self.match = []
        self.replacement = []
        self.title = []
        self.category = []
        self.buyer = [] # obj
        self.buy_date = []
        self.img_data = []
        self.match_counter = []
        self.replace_counter =[]
        self.also_buy_counter = []

class buyer(object):
    def __init__(self,user_id,user_bought_history,items):
        self.id = user_id
        self.items = []
    def get_buy_items(self,user_bought_history,items):
        item_id = get_item_id_from_user_history(user_bought_history,self.id)
        return [get_item(items,i) for i in item_id if i in [item.id for item in items]]

#-----------------------------------------------------------

def get_matchset(dim_fashion_matchsets,coll_id): # coll_id  套餐 ID
    """ return the match set of coll_id """ 
    return dim_fashion_matchsets.item_list[dim_fashion_matchsets.coll_id \
    == coll_id].values[0].split(';')   


def get_replace_matchset(dim_fashion_matchsets,coll_id):
    """ return the match set of coll_id (dealed with replace items)""" 
    return [content.split(',') for content in get_matchset(dim_fashion_matchsets,coll_id)]


def get_match_list(dim_fashion_matchsets,coll_id):
    """ return all the matched combinations of coll_id""" 
    matchset_combine = get_replace_matchset(dim_fashion_matchsets,coll_id)
    prodcut_list = itertools.product(*matchset_combine)
    match_list = [match for match in prodcut_list]
    return match_list

def get_category(dim_items,item_id):
    """ return the category ID of this item_id (cat_id)"""
    return dim_items.cat_id[dim_items.item_id == item_id].values[0]


def get_term_title(dim_items,item_id):
    """ return term [the title of this term ]"""
    return dim_items.terms[dim_items.item_id == item_id].values[0].split(',')    


def get_term_img_data(dim_items,item_id):
    """ return image data"""
    return dim_items.img_data[dim_items.item_id == item_id].values   


def get_user_id(user_bought_history,item_id):
    """  return who bought this item """
    return list(user_bought_history.user_id[user_bought_history.item_id == item_id].values)    

def get_buy_date(user_bought_history,item_id):
    """ return the time of buying this item """
    return list(user_bought_history.create_at[user_bought_history.item_id == item_id].values)    

def get_detail_buy_date(buy_date_list):
    """ get the year , month , day of buying """
    #detail_buy_date=[]
    year = []
    month =[]
    day =[]
    for i in range(len(buy_date_list)):
        date = str(buy_date_list[i])
        #detail_buy_date.append((date[:4],date[4:6],date[6:]))
        year.append(date[:4])
        month.append(date[4:6])
        day.append(date[6:])
    #return detail_buy_date
    return year , month , day


def get_item(items,item_id):
    """ use item_id to get item in the set of items(obj set)""" 
    return [obj for obj in items if obj.id == item_id][0]


def add_replacement_to_item(items,dim_fashion_matchsets):
    """ add replacement item to item in the set of items(obj set)"""
    for i in dim_fashion_matchsets.coll_id:
        match_replace = get_replace_matchset(dim_fashion_matchsets,i)
        for j in range(len(match_replace)):
            for k in range(len(match_replace[j])):
                if int(match_replace[j][k]) in [obj.id for obj in items]:
                    get_item(items,int(match_replace[j][k])).replacement += match_replace[j]
                    if len(set(get_item(items,int(match_replace[j][k])).replacement)) == 1:
                        get_item(items,int(match_replace[j][k])).replacement = []


def add_replacement_counter_to_item(items):
    """ counter the frequency of replacement item"""
    for item in items:
        item.replace_counter = Counter(item.replacement)


def add_match_to_item(items,dim_fashion_matchsets):
    """ add matched item to to item in the set of items(obj set)"""
    for i in dim_fashion_matchsets.coll_id:
        match = get_match_list(dim_fashion_matchsets,i)
        for j in range(len(match)):
            for k in range(len(match[j])):
                if int(match[j][k]) in [obj.id for obj in items]:
                    get_item(items,int(match[j][k])).match += match[j]


def add_match_counter_to_item(items):
    """ counter the frequency of match item"""
    for item in items:
        item.match_counter = sorted(Counter(item.match).items(),key= lambda d: d[1],reverse=True)


def get_item_id_from_user_history(user_bought_history,user_id):
    """  return item_id based on user_id """
    return list(user_bought_history.item_id[user_bought_history.user_id == user_id].values)    


def add_buyer_to_items(items,user_bought_history):
    """ add buyer obj [id / items] to to item in the set of items(obj set)"""
    for item in items:
        if item.id in user_bought_history.item_id:            
            buyer_id = get_user_id(user_bought_history,item.id)
            item.buyer = [buyer(i,user_bought_history,items) for i in buyer_id]


def get_also_buy_item_id(user_bought_history,items,item_id):
    """ get all the also_buy_items'id of item who's id is item_id"""
    item_list = [get_item(items,item_id).buyer[j].get_buy_items(user_bought_history,items) for j in range(len(get_item(items,item_id).buyer))]
    also_buy = []
    for i in range(len(item_list)):
        for j in range(len(item_list[i])):
            also_buy.append(item_list[i][j].id)
    also_buy_counter = Counter(also_buy)
    return also_buy_counter


def add_also_buy_counter_to_items(user_bought_history,items):
    """ counter the frequency of also_buy_id"""
    for item_id in [item.id for item in items]:
        get_item(items,item_id).also_buy_counter = get_also_buy_item_id(user_bought_history,items,item_id)

Test

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 04 23:26:52 2015
@author: Zhang_Jun
"""
from tools_preparation import *

items = []
for i in list(test_items.test_items_id):
    obj = item(i)    
    obj.title = get_term_title(dim_items,i)
    obj.category = get_category(dim_items,i)
    obj.buy_date = get_buy_date(user_bought_history,i)
    items.append(obj)
add_replacement_to_item(items,dim_fashion_matchsets)
add_match_to_item(items,dim_fashion_matchsets)
add_buyer_to_items(items,user_bought_history)
add_match_counter_to_item(items)
add_also_buy_counter_to_items(user_bought_history,items)
add_replacement_counter_to_item(items)

例如查看

In [161]: get_match_list(dim_fashion_matchsets,11)
Out[161]: 
[('1463018', '1596334', '2226122'),
 ('1463018', '1596334', '284814'),
 ('1463018', '1596334', '36278'),
 ('1463018', '1596334', '480281'),
 ('1463018', '1704853', '2226122'),
 ('1463018', '1704853', '284814'),
 ('1463018', '1704853', '36278'),
 ('1463018', '1704853', '480281'),
 ('230955', '1596334', '2226122'),
 ('230955', '1596334', '284814'),
 ('230955', '1596334', '36278'),
 ('230955', '1596334', '480281'),
 ('230955', '1704853', '2226122'),
 ('230955', '1704853', '284814'),
 ('230955', '1704853', '36278'),
 ('230955', '1704853', '480281')



In [162]: get_buy_date(user_bought_history,33547)
Out[162]: 
[20150531,
 20150525,
 20150506,
 20150527,
 20150528,
 20150523,
 20150526,
 20150609,
 20150428,
 20150510,
 20150608,
 20150523,
 ...]




In [160]: get_also_buy_item_id(user_bought_history,items,33547)
Out[160]: Counter({33547: 81, 40867: 1}


In [159]: [a.also_buy_counter for a in items]
Out[159]: 
[Counter(),
 Counter(),
 Counter(),
 Counter({33517: 97}),
 Counter({33547: 81, 40867: 1}),
 Counter(),
 Counter({39667: 32}),
 Counter(),
 Counter({33547: 1, 40867: 139}),
 Counter(),
 Counter({42217: 501, 51367: 1}),
 Counter(),
 Counter({45517: 1}),
 Counter({45817: 85}),
 Counter({50377: 108}),
 Counter(),
 Counter({42217: 1, 51367: 165}),
 Counter(),
 Counter({55117: 15}),
 Counter()
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
### 回答1: 阿里天池2017-11用户行为数据分析是基于Hive进行的。Hive是一个构建在Hadoop之上的数据仓库基础架构,提供了类似于SQL的查询语言HiveQL,使用户能够在大规模数据集上进行数据查询和分析。 在进行用户行为数据分析时,首先需要将原始数据导入Hive数据仓库中。数据源可以是来自的用户行为日志文件,其中包含了用户在平台上的各种行为,例如浏览商品、点击广告、添加购物车、购买等等。 然后,使用HiveQL语言编写查询语句,通过Hive进行数据分析。数据分析的目标可能包括但不限于:用户行为的频率分布、用户购买转化率、热门商品排行、用户购买决策的时间分布等等。通过对用户行为数据进行分析,阿里天池可以洞察用户行为的规律,发现用户需求和购物习惯,从而为优化产品和推广策略提供参考。 Hive的优势之一是可以处理大规模的数据,因此对于这样拥有海量用户和数据的平台而言,使用Hive进行用户行为数据分析非常合适。此外,Hive还提供了数据仓库的概念,可以通过不同的方式将数据进行结构化和存储,以方便后续的查询和分析。 综上所述,阿里天池2017-11用户行为数据分析基于Hive,通过将用户行为数据导入Hive数据仓库,利用HiveQL进行查询和分析,从而洞察用户行为规律,为产品和推广策略优化提供依据。Hive作为一个大数据处理工具,对于处理这样海量用户和数据的平台来说是非常适用的。 ### 回答2: 阿里巴巴天池是一个面向数据科学家和机器学习爱好者的在线数据科学竞赛平台,提供丰富多样的数据集和竞赛任务。其中,用户行为数据分析是天池平台的一个竞赛任务。在这个竞赛中,参赛者需要使用Hive来完成对2017年11月的用户行为数据进行分析。 Hive是基于Hadoop的数据仓库系统,它可以处理大规模数据,并提供了类似于SQL的查询语言,使得用户可以通过编写SQL式的语句来查询和分析数据。在用户行为数据分析任务中,Hive可以帮助分析师和数据科学家从大量数据中提取有用的信息。 通过Hive,我们可以利用用户行为数据进行各种分析,如用户购买行为、浏览行为、搜索行为等。我们可以使用Hive的查询语句来筛选、聚合和统计数据,以得出用户行为的关键指标。 一种常见的使用Hive进行用户行为数据分析的方法是利用Hive提供的内置函数和操作符来进行数据的转换和计算。通过使用Hive的内置函数,我们可以对用户行为数据进行预处理,如将日期格式化、提取关键字等。然后,我们可以使用Hive的聚合函数和操作符来计算用户行为的各种指标,如总购买金额、平均浏览次数等。 此外,Hive还支持用户自定义函数和UDAF(用户自定义聚合函数),这使得分析师和数据科学家可以根据自己的需求来扩展Hive的功能。通过编写自定义函数,我们可以在Hive中实现更加复杂的计算和分析。 总的来说,通过Hive,我们可以使用SQL式的查询语言对阿里天池2017年11月的用户行为数据进行分析。通过Hive的内置函数和操作符,以及用户自定义函数和UDAF,我们可以从大规模的数据中提取有用的信息,并计算出用户行为的各项指标。 ### 回答3: 阿里天池2017-11用户行为数据分析基于Hive,可以使用Hive这个大数据存储和计算框架对2017年11月的用户行为数据进行分析。 Hive是一个基于Hadoop的数据仓库基础架构,可以将大规模数据集存储在Hadoop集群中,并同时提供类似于关系型数据库的查询和分析功能。通过Hive,可以利用SQL的方式对大规模数据进行查询和分析,使得数据分析师更加方便地处理和分析海量数据。 对于2017-11用户行为数据,可以将其导入Hive中进行分析。首先,可以创建一个Hive表,定义各个字段的名称和数据类型,然后将用户行为数据导入到这个表中。接着,可以使用Hive提供的SQL语句进行各种查询和分析。 例如,可以通过查询语句统计每个用户的购买次数、浏览次数、加入购物车次数等行为情况,从而分析用户的购买意向和行为模式。也可以对用户的购买行为进行细分,比如按照地区、商品类别等进行分组,以了解不同用户群体的购物习惯和喜好。此外,还可以对用户行为的时间分布进行分析,了解用户在不同时间段的活跃度和购买偏好。 通过Hive的数据分析功能,可以深入挖掘2017-11用户行为数据中潜在的商业价值,为企业的市场营销和业务决策提供重要参考依据。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值