<jupyter_start><jupyter_text>Explore Merchant & Products<jupyter_code>import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
merchant_order_df = pd.read_pickle('merchant_order.pkl')
print(merchant_order_df.shape)

merchant_order_df.head()
merchant_product_df = pd.read_pickle('merchant_product.pkl')
print(merchant_product_df.shape)

merchant_product_df.head()
all_order_df = merchant_order_df.merge(merchant_product_df[['merchant', 'product_id', 'product_name', 'price', 'aisle', 'department']], 
                                       on=['merchant', 'product_id'])
print(all_order_df.shape)

all_order_df.head()<jupyter_output>(33819106, 11)<jupyter_text>Purchase Frequency OverviewThe purpose of this check is just to understand an overview about purchase pattern, to see whether there is any abnormal pattern that might prevent a certain types of recommendations.<jupyter_code>df_count = all_order_df[['product_id', 'order_number']].astype('str').drop_duplicates()\
          .groupby(['product_id'], as_index=False)['order_number']\
          .agg(['count']).reset_index()\
          .sort_values(['count'], ascending=False)

df_count.head()
df_ct_ct = pd.DataFrame(df_count['count'].value_counts()).sort_index()
df_ct_ct['frequency'] = df_ct_ct.index

ax = df_ct_ct.plot.bar(x='frequency', y='count', rot=0, figsize=(16,7), xticks = np.arange(0, 100, step=10),
                      title='Distribution of product purchase frequency - All History')<jupyter_output><empty_output><jupyter_text>The distribution here is showing:* Most products are purchased within 10 times* The number of products got purchased 100 times is also high<jupyter_code># to get the week of the year
all_order_df['week_number'] = all_order_df['purchase_date'].apply(lambda dt: dt.isocalendar()[1])
all_order_df.head()
# check purchase frequency
popularity_df = all_order_df[['product_id', 'order_id', 'week_number']].astype('str').drop_duplicates()\
                                                  .groupby(['product_id'], as_index=False)[['order_id', 'week_number']]\
                                                  .agg({'order_id': 'count', 'week_number': 'count'}).reset_index()
popularity_df.head()
popularity_df['purchase_frequency'] = popularity_df['order_id']/popularity_df['week_number']
print(min(popularity_df['purchase_frequency']), max(popularity_df['purchase_frequency']))<jupyter_output>1.0 1.0<jupyter_text>I tried both weekly and daily purchase frequency, all are 1. This data is a bit special (hehe). So I cannot use purchase frequency to calculate the popularity.<jupyter_code>sample_df1 = pd.read_pickle('sample_test_df1.pkl')
sample_df2 = pd.read_pickle('sample_test_df2.pkl')

print(min(sample_df1.index), max(sample_df1.index))
print(min(sample_df2.index), max(sample_df2.index))
date_threshold = datetime.datetime.strptime('2019-05-22', "%Y-%m-%d").date()

# all_order_train = all_order_df.loc[all_order_df['purchase_date'] < date_threshold]
all_order_test = all_order_df.loc[all_order_df['purchase_date'] >= date_threshold]

print(min(all_order_train['purchase_date']), max(all_order_train['purchase_date']))
print(min(all_order_test['purchase_date']), max(all_order_test['purchase_date']))
all_order_train.to_pickle('all_order_train.pkl')
all_order_test.to_pickle('all_order_test.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13426615064007144, 'fraction_numerical': 0.013992259601071748, 'mean_word_length': 5.871165644171779, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '471924', 'license': 'MIT', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1112, 'n_words': 199, 'path': 'Bank_Fantasy/Golden_Bridge/explore_merchant_products.ipynb', 'repo_name': 'hanhanwu/Hanhan_Break_the_Limits', 'size': '3359'}