# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd

hist_data = pd.read_csv("hist_data.csv")
class_hist_data = pd.read_csv('user_class_df_all_data.csv')

train = train.merge(class_train, how='left', on='buyer_id')
hist_data = hist_data.merge(class_hist_data, how='left', on='buyer_id')

train

class_hist_data_dict = {i[0]: i[1] for i in class_hist_data[['buyer_id', 'group_id']].values}

# +
import json
from os.path import join
import pandas as pd
import numpy as np
import gc
from collections import Counter 
from tqdm import tqdm 


def make_coocurs_dict(data):
    tmp = (
        data[['item_id', 'pav_order_id']]
        .sort_values(['item_id', 'pav_order_id'])
        .merge(data[['item_id', 'pav_order_id']], how='left', on=['pav_order_id'], suffixes=('', '_left'))
    )
    tmp = tmp[tmp['item_id'] != tmp['item_id_left']].copy()
    tmp1 = tmp.groupby(['item_id'])['item_id_left'].agg(lambda x: Counter(x).most_common(20))
    tmp2 = data.groupby('item_id')['pav_order_id'].count().reset_index()
    base = dict(zip(tmp2.item_id, tmp2.pav_order_id))
    most_freq_dict = {k: [(x[0], (x[1]+0.3)/(50+base[k])) for x in v] for (k, v) in tmp1.iteritems()}
    del tmp1, tmp
    gc.collect()
    return most_freq_dict

def freq_by_person(data, class_dict):
    js = {}
    for group_id in tqdm(data['group_id'].unique()):
        
        data1 = data.loc[data.group_id == group_id]
        js[int(group_id)] = make_coocurs_dict(data1)
        #print('next buyer {}'.format(buyer))
    return js
    

personal_test = freq_by_person(hist_data, class_hist_data_dict)
with open('class_rec_hist.json', 'w', encoding='utf8') as json_file:
    json.dump(personal_test, json_file, ensure_ascii=False, indent=4)
# -







StarcoderdataJupyterScriptsDedupFiltered