# ---
# 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 os
import numpy as np
import pandas as pd

from tqdm import tqdm
from sklearn.preprocessing import LabelEncoder
# -

pd.options.display.max_columns = None

os.listdir('../input')

train = pd.read_csv('../input/train.csv.gz')

train.head()

col = [c for c in train.columns if c not in ['card_id']]

col

hist = pd.read_csv('../input/historical_transactions.csv.gz', parse_dates=['purchase_date'])

hist.nunique()

hist.head()

hist[hist['merchant_id'].isnull()].head()

hist['category_3'] = hist['category_3'].fillna('D')
for c in tqdm(['authorized_flag', 'category_1', 'category_3']):
    print(c)
    le = LabelEncoder()
    le.fit(list(set(hist[c].values)))
    hist[c] = le.transform(hist[c].values)

hist.head()

hist['authorized_flag'].unique()

stats = ['min', 'max', 'mean', 'median', 'std', 'var', 'skew']
num_aggregations = {
#     'authorized_flag': 'sum',
#     'category_1': stats,
    'installments': stats,
#     'category_3': stats,
    'month_lag': stats,
    'purchase_amount': stats,
#     'category_2': stats,
}
feature = hist_train.groupby(['card_id']).agg(num_aggregations).reset_index()

feature.head()

feature.columns = [f'{c[0]}_{c[1]}' for c in feature.columns]
feature = feature.rename(columns={'card_id_': 'card_id'})

feature.head()


feature = hist_train.groupby(['card_id', 'mer']).agg(num_aggregations).reset_index()


def get_dummies(df):
    """
    binary would be drop_first
    """
    col = df.select_dtypes('O').columns.tolist()
    nunique = df[col].nunique()
    col_binary = nunique[nunique==2].index.tolist()
    [col.remove(c) for c in col_binary]
    df = pd.get_dummies(df, columns=col)
    df = pd.get_dummies(df, columns=col_binary, drop_first=True)
    df.columns = [c.replace(' ', '-') for c in df.columns]
    return df


hist_category = hist[['card_id', 'category_1', 'category_2', 'category_3', 'authorized_flag',]]

hist_category.head()

col = hist.select_dtypes('O').columns.tolist()

col

col = ['category_2', 'category_3']
col_binary = ['category_1', 'authorized_flag']
feature = pd.get_dummies(hist_category, columns=col)
feature = pd.get_dummies(feature, columns=col_binary, drop_first=True)

feature.head()

 argss = [
            {'col': ['category_2', 'category_3'],
             'col_binary': ['authorized_flag', 'category_1']}
    ]

argss[0]['col']

feature = feature.add_prefix('pref_').rename(columns={'pref_pref_card_id': 'card_id'})

feature.head()

f102 = pd.read_pickle('../feature/f102_train.pkl')

f102.head()

f101 = pd.read_pickle('../feature/f101_train.pkl')
f101.head()

f101.head()

 args = {
            'col': ['category_2', 'category_3'],
            'col_binary': ['authorized_flag', 'category_1']
        }

argss[0]

# +
KEY = 'card_id'

train = pd.read_csv('../input/train.csv.gz')[[KEY]]
test = pd.read_csv('../input/test.csv.gz')[[KEY]]

hist = pd.read_csv('../input/historical_transactions.csv.gz')

# +
# col, col_binary = args['col'], args['col_binary']

# feature = hist[[KEY] + col + col_binary]

# feature = pd.get_dummies(feature, columns=col)
feature = pd.get_dummies(feature, columns=col_binary, drop_first=True)
# feature = feature.add_prefix(PREF).rename(columns={PREF+KEY: KEY})

# feature[feature.card_id.isin(train)].to_pickle(f'../feature/{PREF}train.pkl')
# feature[feature.card_id.isin(test)].to_pickle(f'../feature/{PREF}test.pkl')
# -

col

col_binary

feature.head()

train.head()

hist.columns

train.head()



StarcoderdataJupyterScriptsDedupFiltered