(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)
    
    order_tbl_ = order_tbl[order_tbl.order_number_rev>T]
    
    pid_cnt    = defaultdict(int)
    pid_chance = defaultdict(int)
    
#    for pids_bk3, pids_bk2, pids_bk1 in tqdm(order_tbl_[['t-3_product_id', 't-2_product_id', 't-1_product_id']].values):
#    for uid, onb, pid in tqdm(log_[['user_id', 'order_number', 'product_name']].head(1999999).values):
#    for uid, onb, pid in tqdm(log_[['user_id', 'order_number', 'product_id']].values, miniters=99999):
    for pids_bk3, pids_bk2, pids_bk1 in tqdm(order_tbl_[['t-3_product_id', 't-2_product_id', 't-1_product_id']].values, miniters=99999):
        
        pids_3notin2 = pids_bk3 - pids_bk2
        pids_2notin3 = pids_bk2 - pids_bk3
        pids_hub  = pids_bk2 - pids_bk3 - pids_bk1
        pids_skip = (pids_bk3 & pids_bk1) - pids_bk2
        
        li = []
        for i1, i2 in list(product(pids_3notin2, pids_2notin3)):
            key = str(i1)+' -> '+str(i2)
            li.append(key)
            pid_chance[key] +=1
            
        li = []
        for i1, i2 in list(product(pids_skip, pids_hub)):
            key = str(i1)+' -> '+str(i2)
            li.append(key)
            pid_cnt[key] +=1
        
        
    
    pid_chance = pd.DataFrame.from_dict(pid_chance, orient='index').reset_index()
    pid_chance.columns = ['pids', 'chance']
    
    pid_cnt = pd.DataFrame.from_dict(pid_cnt, orient='index').reset_index()
    pid_cnt.columns = ['pids', 'back']
    
    df = pd.merge(pid_chance, pid_cnt, on='pids', how='left').fillna(0)
    
    df['ratio'] = df.back/df.chance
    df.sort_values('ratio', ascending=False, inplace=True)
    
    df.reset_index(drop=True, inplace=True)
    df['pid1'] = df.pids.map(lambda x: x.split(' -> ')[0]).astype(int)
    df['pid2'] = df.pids.map(lambda x: x.split(' -> ')[1]).astype(int)
    df[['pid1', 'pid2', 'back', 'chance', 'ratio']].to_pickle('../input/mk/replacement.p')
def make ( T ) : if T == - 1 : folder = 'test' else : folder = 'trainT-' + str ( T ) order_tbl_ = order_tbl [ order_tbl . order_number_rev > T ] pid_cnt = defaultdict ( int ) pid_chance = defaultdict ( int ) #    for pids_bk3, pids_bk2, pids_bk1 in tqdm(order_tbl_[['t-3_product_id', 't-2_product_id', 't-1_product_id']].values): #    for uid, onb, pid in tqdm(log_[['user_id', 'order_number', 'product_name']].head(1999999).values): #    for uid, onb, pid in tqdm(log_[['user_id', 'order_number', 'product_id']].values, miniters=99999): for pids_bk3 , pids_bk2 , pids_bk1 in tqdm ( order_tbl_ [ [ 't-3_product_id' , 't-2_product_id' , 't-1_product_id' ] ] . values , miniters = 99999 ) : pids_3notin2 = pids_bk3 - pids_bk2 pids_2notin3 = pids_bk2 - pids_bk3 pids_hub = pids_bk2 - pids_bk3 - pids_bk1 pids_skip = ( pids_bk3 & pids_bk1 ) - pids_bk2 li = [ ] for i1 , i2 in list ( product ( pids_3notin2 , pids_2notin3 ) ) : key = str ( i1 ) + ' -> ' + str ( i2 ) li . append ( key ) pid_chance [ key ] += 1 li = [ ] for i1 , i2 in list ( product ( pids_skip , pids_hub ) ) : key = str ( i1 ) + ' -> ' + str ( i2 ) li . append ( key ) pid_cnt [ key ] += 1 pid_chance = pd . DataFrame . from_dict ( pid_chance , orient = 'index' ) . reset_index ( ) pid_chance . columns = [ 'pids' , 'chance' ] pid_cnt = pd . DataFrame . from_dict ( pid_cnt , orient = 'index' ) . reset_index ( ) pid_cnt . columns = [ 'pids' , 'back' ] df = pd . merge ( pid_chance , pid_cnt , on = 'pids' , how = 'left' ) . fillna ( 0 ) df [ 'ratio' ] = df . back / df . chance df . sort_values ( 'ratio' , ascending = False , inplace = True ) df . reset_index ( drop = True , inplace = True ) df [ 'pid1' ] = df . pids . map ( lambda x : x . split ( ' -> ' ) [ 0 ] ) . astype ( int ) df [ 'pid2' ] = df . pids . map ( lambda x : x . split ( ' -> ' ) [ 1 ] ) . astype ( int ) df [ [ 'pid1' , 'pid2' , 'back' , 'chance' , 'ratio' ] ] . to_pickle ( '../input/mk/replacement.p' )