# ---
# 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 numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import gc

import category_encoders as ce

root='C:/Users/luthf/Documents/DS/BasketAnalysis/'

# +
orders = pd.read_csv(root + 'orders.csv', 
                 dtype={
                        'order_id': np.int32,
                        'user_id': np.int64,
                        'eval_set': 'category',
                        'order_number': np.int16,
                        'order_dow': np.int8,
                        'order_hour_of_day': np.int8,
                        'days_since_prior_order': np.float32})


order_products_train = pd.read_csv(root + 'order_products__train.csv', 
                                 dtype={
                                        'order_id': np.int32,
                                        'product_id': np.uint16,
                                        'add_to_cart_order': np.int16,
                                        'reordered': np.int8})

order_products_prior = pd.read_csv(root + 'order_products__prior.csv', 
                                 dtype={
                                        'order_id': np.int32,
                                        'product_id': np.uint16,
                                        'add_to_cart_order': np.int16,
                                        'reordered': np.int8})

product_level = pd.read_pickle(root + 'prod_level.pkl')

user_level = pd.read_pickle(root + 'user_level.pkl')

user_product_features = pd.read_pickle(root + 'user_product_features.pkl')

products = pd.read_csv(root +'products.csv')

aisles = pd.read_csv(root + 'aisles.csv')

departments = pd.read_csv(root + 'departments.csv')
# -

user_level.head()

train_order=orders.merge(order_products_train,on='order_id',how='inner')

train_order.head()

train_order.drop(columns=['add_to_cart_order','eval_set','order_id'],axis=1,inplace=True)

train_user=train_order.user_id.unique()

df=user_product_features[user_product_features.user_id.isin(train_user)]

df.head()

df=df.merge(train_order, on = ['user_id', 'product_id'], how = 'outer')

df.head()

df.isnull().sum()

df.order_number.fillna(df.groupby('user_id')['order_number'].transform('mean'), inplace = True)
df.order_dow.fillna(df.groupby('user_id')['order_dow'].transform('mean'), inplace = True)
df.order_hour_of_day.fillna(df.groupby('user_id')['order_hour_of_day'].transform('mean'), inplace = True)
df.days_since_prior_order.fillna(df.groupby('user_id')['days_since_prior_order'].\
                                                             transform('mean'), inplace = True)

df.isnull().sum()

df.reordered.value_counts()

df = df[df.reordered != 0]

# +
df.reordered.fillna(0, inplace = True)

df.isnull().sum()
# -

# ### merging user and product features

df=df.merge(product_level,on='product_id',how='left')
df=df.merge(user_level,on='user_id',how='left')

df.head()

df.info()

product_level.head()

user_level.head()

df.shape

df.head()

df.to_pickle(root + 'datafinal.pkl')
df = pd.read_pickle(root+'datafinal.pkl')
df.head()



StarcoderdataJupyterScriptsDedupFiltered