(T)
T = 0
    folder = 'trainT-0'
T = 0
    folder = 'trainT-0'
T = 0 folder = trainT - 0
def make(T):
    """
    T = 0
    folder = 'trainT-0'
    """
    if T==-1:
        folder = 'test'
    else:
        folder = 'trainT-'+str(T)
    print("start T:{} folder:{}".format(T, folder))
    order_tbl_ = order_tbl[order_tbl.order_number_rev>T].dropna() # drop first order
    
    item2item = []
    item_bunbo = Counter()
    for item_prior, item_now in order_tbl_[['t-1_product_name', 'product_name']].values:
        item2item  += [i1+' -> '+i2 for i1, i2 in list(product(item_prior, item_now))]
        item_bunbo += Counter(item_prior)
    item2item = Counter(item2item)

    df = pd.DataFrame.from_dict(item2item, orient='index').reset_index()
    df.columns = ['item', 'cnt']
    del item2item; gc.collect()

    df_ = pd.DataFrame.from_dict(item_bunbo, orient='index').reset_index()
    df_.columns = ['before', 'total_cnt']
    del item_bunbo; gc.collect()
    
    df.sort_values('cnt', ascending=False, inplace=True)
    
    df['before'] = df.item.map(lambda x: x.split(' -> ')[0])
    df['after'] = df.item.map(lambda x: x.split(' -> ')[1])
    df = df[df.before!=df.after]
    
    df = pd.merge(df, df_, on='before', how='left')
    
    df['before_to_after_ratio'] = df.cnt / df.total_cnt
    df = df[['before', 'after', 'before_to_after_ratio']]
    gc.collect()

    df = pd.merge(df, prods.rename(columns={'product_name':'before', 'product_id':'before_id'}), 
                   on='before', how='left')
    df = pd.merge(df, prods.rename(columns={'product_name':'after', 'product_id':'after_id'}), 
                   on='after', how='left')
    
    df = df[['before_id', 'after_id', 'before_to_after_ratio']]
    gc.collect()
    """
    df.head()
          before_id  after_id  before_to_after_ratio
    0      47209     13176               0.288618
    1      13176     47209               0.175736
    2      13176     21137               0.148974
    3      21137     13176               0.188769
    """
    #==============================================================================
    print('Merge', T)
    #==============================================================================
    label = pd.read_pickle('../feature/{}/label_reordered.p'.format(folder))
    label = pd.merge(label, order_tbl[['order_id', 't-1_order_id']], 
                     on='order_id', how='left')
    print('今まで買ったitem and t-1に買ったitem')
    order_b4after = pd.merge(label, order_item_array.add_prefix('t-1_'), 
                             on='t-1_order_id', how='left')
    gc.collect()
    
    col = ['order_id', 't-1_product_id', 'product_id']
    order_b4after = order_b4after[col]
    gc.collect()
    """
    order_b4after.head()
    Out[9]:
       order_id                                     t-1_product_id  product_id
    0   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...         196
    1   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       10258
    2   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       10326
    3   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       12427
    4   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       13032
    """
    #==============================================================================
    print('search max ratio',T)
    #==============================================================================
    df['key'] = df.before_id.map(str) + 'to' + df.after_id.map(str)
    
    ratio_tbl = {}
    for k,v in df[['key','before_to_after_ratio']].values:
        ratio_tbl[k] = v
    
    del df; gc.collect()
    
    def get_ratio(key):
        try:
            return ratio_tbl[key]
        except:
            return -1
    
    def search_max_ratio(before_items, item):
        """
        before_items = order_tr.loc[0,'t-1_product_id']
        item    = order_tr.loc[0,'product_id']    
        """
        comb = list(product(before_items, [item]))
        comb = [str(x) + 'to' + str(y) for x,y in sorted(comb, key=itemgetter(1))]
        return np.max([get_ratio(k) for k in comb])


    print('== before_to_after_ratio ==', T)
    ret = []
    for before_items, item in order_b4after[['t-1_product_id', 'product_id']].values:
        ret.append(search_max_ratio(before_items, item))
    order_b4after['before_to_after_ratio'] = ret
    
    col = ['order_id', 'product_id', 'before_to_after_ratio']
    order_b4after[col].to_pickle('../feature/{}/f205_order_product.p'.format(folder))
def make ( T ) : if T == - 1 : folder = 'test' else : folder = 'trainT-' + str ( T ) print ( "start T:{} folder:{}" . format ( T , folder ) ) order_tbl_ = order_tbl [ order_tbl . order_number_rev > T ] . dropna ( ) # drop first order item2item = [ ] item_bunbo = Counter ( ) for item_prior , item_now in order_tbl_ [ [ 't-1_product_name' , 'product_name' ] ] . values : item2item += [ i1 + ' -> ' + i2 for i1 , i2 in list ( product ( item_prior , item_now ) ) ] item_bunbo += Counter ( item_prior ) item2item = Counter ( item2item ) df = pd . DataFrame . from_dict ( item2item , orient = 'index' ) . reset_index ( ) df . columns = [ 'item' , 'cnt' ] del item2item gc . collect ( ) df_ = pd . DataFrame . from_dict ( item_bunbo , orient = 'index' ) . reset_index ( ) df_ . columns = [ 'before' , 'total_cnt' ] del item_bunbo gc . collect ( ) df . sort_values ( 'cnt' , ascending = False , inplace = True ) df [ 'before' ] = df . item . map ( lambda x : x . split ( ' -> ' ) [ 0 ] ) df [ 'after' ] = df . item . map ( lambda x : x . split ( ' -> ' ) [ 1 ] ) df = df [ df . before != df . after ] df = pd . merge ( df , df_ , on = 'before' , how = 'left' ) df [ 'before_to_after_ratio' ] = df . cnt / df . total_cnt df = df [ [ 'before' , 'after' , 'before_to_after_ratio' ] ] gc . collect ( ) df = pd . merge ( df , prods . rename ( columns = { 'product_name' : 'before' , 'product_id' : 'before_id' } ) , on = 'before' , how = 'left' ) df = pd . merge ( df , prods . rename ( columns = { 'product_name' : 'after' , 'product_id' : 'after_id' } ) , on = 'after' , how = 'left' ) df = df [ [ 'before_id' , 'after_id' , 'before_to_after_ratio' ] ] gc . collect ( ) """
    df.head()
          before_id  after_id  before_to_after_ratio
    0      47209     13176               0.288618
    1      13176     47209               0.175736
    2      13176     21137               0.148974
    3      21137     13176               0.188769
    """ #============================================================================== print ( 'Merge' , T ) #============================================================================== label = pd . read_pickle ( '../feature/{}/label_reordered.p' . format ( folder ) ) label = pd . merge ( label , order_tbl [ [ 'order_id' , 't-1_order_id' ] ] , on = 'order_id' , how = 'left' ) print ( '今まで買ったitem and t-1に買ったitem') order_b4after = pd . merge ( label , order_item_array . add_prefix ( 't-1_' ) , on = 't-1_order_id' , how = 'left' ) gc . collect ( ) col = [ 'order_id' , 't-1_product_id' , 'product_id' ] order_b4after = order_b4after [ col ] gc . collect ( ) """
    order_b4after.head()
    Out[9]:
       order_id                                     t-1_product_id  product_id
    0   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...         196
    1   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       10258
    2   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       10326
    3   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       12427
    4   1187899  [46149, 39657, 38928, 25133, 10258, 35951, 130...       13032
    """ #============================================================================== print ( 'search max ratio' , T ) #============================================================================== df [ 'key' ] = df . before_id . map ( str ) + 'to' + df . after_id . map ( str ) ratio_tbl = { } for k , v in df [ [ 'key' , 'before_to_after_ratio' ] ] . values : ratio_tbl [ k ] = v del df gc . collect ( ) def get_ratio ( key ) : try : return ratio_tbl [ key ] except : return - 1 def search_max_ratio ( before_items , item ) : """
        before_items = order_tr.loc[0,'t-1_product_id']
        item    = order_tr.loc[0,'product_id']    
        """ comb = list ( product ( before_items , [ item ] ) ) comb = [ str ( x ) + 'to' + str ( y ) for x , y in sorted ( comb , key = itemgetter ( 1 ) ) ] return np . max ( [ get_ratio ( k ) for k in comb ] ) print ( '== before_to_after_ratio ==' , T ) ret = [ ] for before_items , item in order_b4after [ [ 't-1_product_id' , 'product_id' ] ] . values : ret . append ( search_max_ratio ( before_items , item ) ) order_b4after [ 'before_to_after_ratio' ] = ret col = [ 'order_id' , 'product_id' , 'before_to_after_ratio' ] order_b4after [ col ] . to_pickle ( '../feature/{}/f205_order_product.p' . format ( folder ) )