can you explain which other features you would create on top of the below, and also why are these approaches valuable and how would this also incorporate new customers for example where there may be only 1 or 2 months of data, so would this also work for new customers:
- Create aggregated features:
def calculate_aggregates(df, col, window_sizes=[3, 6, 12]):
for window_size in window_sizes:
df[f"{col}mean{window_size}m"] = df[col].rolling(window_size).mean()
df[f"{col}std{window_size}m"] = df[col].rolling(window_size).std()
df[f"{col}min{window_size}m"] = df[col].rolling(window_size).min()
df[f"{col}max{window_size}m"] = df[col].rolling(window_size).max()
columns_to_aggregate = [‘BAL_CUR_H01’, ‘AMOUNT PURCHASES LIFETIME’]
for col in columns_to_aggregate:
calculate_aggregates(df, col)
- Difference features:
def add_month_to_month_diff(df, col):
df[f"{col}_m2m_diff"] = df[col].diff()
columns_to_diff = [‘BAL_CUR_H01’, ‘AMOUNT PURCHASES LIFETIME’]
for col in columns_to_diff:
add_month_to_month_diff(df, col)
- Trend-based features:
import numpy as np
from sklearn.linear_model import LinearRegression
def calculate_trend(df, col):
for unique_id in df.index.unique():
subset = df.loc[unique_id]
X = np.array(range(len(subset))).reshape(-1, 1)
y = subset[col].values
model = LinearRegression()
model.fit(X, y)
df.loc[unique_id, f"{col}trend"] = model.coef[0]
columns_to_trend = [‘BAL_CUR_H01’, ‘AMOUNT PURCHASES LIFETIME’]
for col in columns_to_trend:
calculate_trend(df, col)
- Interaction features:
def create_interaction_feature(df, col1, col2):
df[f"{col1}x{col2}“] = df[col1] * df[col2]
interaction_columns = [(‘BAL_CUR_H01’, ‘CRDSSCR’), (‘AMOUNT_PURCHASES_LIFETIME’, ‘CR_LIM’)]
for col1, col2 in interaction_columns:
create_interaction_feature(df, col1, col2)
- Lagged features:
def add_lagged_features(df, col, lags=[2, 3]):
for lag in lags:
df[f”{col}lag{lag}"] = df[col].shift(lag)
columns_to_lag = [‘BAL_CUR_H01’, ‘AMOUNT PURCHASES LIFETIME’]
for col in columns_to_lag:
add_lagged_features(df, col)