<jupyter_start><jupyter_text>User-Product Level<jupyter_code>#.agg(new_col_name=('col_name','agg_func')
user_product_level=df_feature.groupby(['user_id','product_id']).agg(avg_add_to_cart_by_user=('add_to_cart_order','mean'),
                                                                    avg_day_since_prior_order=('days_since_prior_order','mean'),
                                                                    user_total_order_product=('reordered','count'),
                                                                    user_total_reorder_product=('reordered','sum'),
                                                                    user_percentage_reorder_product=('reordered','mean'),
                                                                    last_order_in=('order_number','max')
                                                                   )
                                                                               
user_product_level.reset_index(inplace = True)
user_product_level.head()
last_order=df_feature.merge(last_three_orders,on=['user_id','order_number'],how='inner')
last_order.head()
last_order['rank'] = last_order.groupby(['user_id', 'product_id'])['order_number'].rank("dense", ascending=True)
product_purchase_history = last_order.pivot_table(index = ['user_id', 'product_id'],\
                                                   columns='rank', values = 'reordered').reset_index()
product_purchase_history.columns = ['user_id', 'product_id', 'is_reorder_3', 'is_reorder_2', 'is_reorder_1']
product_purchase_history.fillna(0, inplace = True)
product_purchase_history.head()
user_product_feats = user_product_level.merge(product_purchase_history, on=['user_id', 'product_id'], how = 'left')
user_product_feats.head()
user_product_feats.isnull().sum()
user_product_feats.fillna(0, inplace = True)<jupyter_output><empty_output><jupyter_text>saving features<jupyter_code>prod_level.to_pickle(root + 'prod_level.pkl')
user_level.to_pickle(root +'user_level.pkl')
user_product_feats.to_pickle(root +'user_product_features.pkl')
df = pd.read_pickle(root+'prod_level.pkl')
df.head()
df = pd.read_pickle(root+'prod_level.pkl')
df.head()
df = pd.read_pickle(root+'user_product_features.pkl')
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 13307953 entries, 0 to 13307952
Data columns (total 11 columns):
 #   Column                           Dtype  
---  ------                           -----  
 0   user_id                          int64  
 1   product_id                       uint64 
 2   avg_add_to_cart_by_user          float64
 3   avg_day_since_prior_order        float32
 4   user_total_order_product         int64  
 5   user_total_reorder_product       int8   
 6   user_percentage_reorder_product  float64
 7   last_order_in                    int16  
 8   is_reorder_3                     float64
 9   is_reorder_2                     float64
 10  is_reorder_1                     float64
dtypes: float32(1), float64(5), int16(1), int64(2), int8(1), uint64(1)
memory usage: 1002.6 MB
{'fraction_non_alphanumeric': 0.10787172011661808, 'fraction_numerical': 0.027210884353741496, 'mean_word_length': 2.1191919191919193, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '337239', 'license': 'MIT', 'n_tokens_mistral': 963, 'n_tokens_neox': 902, 'n_words': 119, 'path': 'FeatureExtraction.ipynb', 'repo_name': 'luthfiraditya/Market-Basket-Analysis', 'size': '3087'}