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

# +
import pandas as pd

train = pd.read_csv("../../data/interim/train.csv")

train.head()
# -

from sklearn.preprocessing import KBinsDiscretizer
kbins = KBinsDiscretizer(n_bins=5, encode='ordinal', strategy='quantile')  # ‘uniform’, ‘quantile’, ‘kmeans’
train['amount_discretized'] = kbins.fit_transform(train[['amount']].values)
agg_values = train.groupby(by=['amount_discretized']).mean()
columns_to_agg = ['v1']
agg_values = agg_values[columns_to_agg]
agg_values.columns = [x + "_mean_given_amount" for x in agg_values.columns]
train = train.merge(agg_values, how='left', on=['amount_discretized'])
train.drop(['amount_discretized'], axis=1, inplace=True)
print(train.shape)
train.head()


from sklearn.base import BaseEstimator, TransformerMixin
class AggByAmount(BaseEstimator, TransformerMixin):
    # Inputs: bins, encode, strategy ('uniform', 'quantile', 'kmeans'), number of top features, mean/max/min
    # Top features order: ['v1', 'v4', 'v10', 'v7', 'v18', 'v11', 'v20', 'amount', 'v3', 'v16', 'v13', 'v14', 'v8', 'v9', 'v19', 'v2', 'v5', 'v12', 'v26', 'v24', 'v25', 'v27', 'v17', 'v22', 'v23', 'v6', 'v15', 'v21']
    def __init__(self, n_bins=5, encode='ordinal', strategy='quantile', columns_to_agg=['v1']):
        self.n_bins = n_bins
        self.encode = encode
        self.strategy = strategy
        self.columns_to_agg = columns_to_agg
        self.kbins = None
        self.initial_columns = None
    def fit(self, X, y=None):
        self.kbins = KBinsDiscretizer(n_bins=self.n_bins, encode=self.encode, strategy=self.strategy)
        self.kbins.fit(X[['amount']].values)
        self.initial_columns = list(X.columns)
        return self
    def transform(self, X, y=None):
        X['amount_discretized'] = self.kbins.transform(X[['amount']].values)
        agg_values = X.groupby(by=['amount_discretized']).mean()
        agg_values = agg_values[self.columns_to_agg]
        agg_values.columns = [x + "_mean_given_amount" for x in agg_values.columns]
        X = X.merge(agg_values, how='left', on=['amount_discretized'])
        X.drop(self.initial_columns + ['amount_discretized'], axis=1, inplace=True)
        return X



agg_by_amount = AggByAmount()

agg_by_amount.fit(train)

agg_by_amount.transform(train).head()



StarcoderdataJupyterScriptsDedupFiltered