天池競賽-淘寶穿衣搭配(數據預處理部分)

賽題簡介

淘寶網是中國深受歡迎的網購零售平臺,其中服飾鞋包行業佔據市場的絕大部分份額,圍繞着淘寶誕生了一大批優秀的服飾鞋包導購類的產品。穿衣搭配是服飾鞋包導購中非常重要的課題,它所延伸出的技術、算法能廣泛應用到大數據營銷幾乎所有場景中,如搜索、推薦和營銷服務。淘寶穿衣搭配算法競賽將爲參賽者提供搭配專家和達人生成的搭配組合數據,百萬級別的淘寶商品的文本和圖像數據,同時還將提供用戶的脫敏行爲數據。期待參賽者能從以上行爲、文本和圖像數據中挖掘穿衣搭配模型,爲用戶提供個性化、優質的、專業的穿衣搭配方案。

數據格式

搭配套餐數據: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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章