<jupyter_start><jupyter_text>Data Prep We have used a synthetic data to demonstrate the implementation. This data preparation can be achieved in SQL in needed.This example demonstrate the model using two features such as deposit and withdrawal amount.<jupyter_code>data_cm = pd.read_csv('synth_data_aml_cm.csv')
# data_cm.columns
## selecting fetures related to deposit and withdrawals only as an example
data_cm = data_cm.filter(['Customer_id', 'deposit_amount_24_hours', 'deposit_amount_14_days',
                          'deposit_amount_3_months', 'deposit_amount_lifetime',
                          'n_deposit_days_14_days', 'n_deposit_days_3_months',
                          'n_deposit_days_lifetime', 'wtd_amount_24_hours', 'wtd_amount_14_days', 
                          'wtd_amount_3_months', 'wtd_amount_lifetime', 'n_wtd_days_14_days', 
                          'n_wtd_days_3_months', 'n_wtd_days_lifetime'])
data_cm.head()
## data prep stage - can be done via sql 
data_cm['avg_dep_14_days'] = data_cm.deposit_amount_14_days/data_cm.n_deposit_days_14_days
data_cm['avg_dep_3_months'] = data_cm.deposit_amount_3_months /data_cm.n_deposit_days_3_months
data_cm['avg_dep_lifetime'] = data_cm.deposit_amount_lifetime /data_cm.n_deposit_days_lifetime
data_cm['avg_wtd_14_days'] = data_cm.wtd_amount_14_days/data_cm.n_wtd_days_14_days
data_cm['avg_wtd_3_months'] = data_cm.wtd_amount_3_months /data_cm.n_wtd_days_3_months
data_cm['avg_wtd_lifetime'] = data_cm.wtd_amount_lifetime /data_cm.n_wtd_days_lifetime
data_cm.head()
data_results = data_cm.set_index(['Customer_id']).filter(regex=('24|avg'))
data_results.head()
data_results.dtypes<jupyter_output><empty_output><jupyter_text>Model Implementation<jupyter_code>def anomaly_score_feature(df):
    # compute f(x)
    newdf = df.filter(['Customer_id'])
    newdf['flag1'] = df.filter(regex='24').iloc[:,0] > df.filter(regex='14').iloc[:,0]
    newdf['flag2'] = df.filter(regex='24').iloc[:,0] > df.filter(regex='3_months').iloc[:,0]
    newdf['flag3'] = df.filter(regex='24').iloc[:,0] > df.filter(regex='lifetime').iloc[:,0]

    ## computer anomaly score per feature
    newdf = pd.DataFrame(newdf.apply('mean', axis=1))

    return newdf
## get anomaly score for all available features 
output=[]
feature_list = ['dep', 'wtd']
for value in feature_list:
    df = data_results.filter(regex=value)
    output.append(anomaly_score_feature(df))
anomaly_score = pd.concat(output, axis=1)
anomaly_score = pd.DataFrame(anomaly_score.apply('mean', axis=1))
anomaly_score.reset_index(level=[0], inplace=True)
anomaly_score.rename(columns={0: 'Anomaly score'}, inplace=True)
anomaly_score.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12458038045505408, 'fraction_numerical': 0.022006713912719134, 'mean_word_length': 5.557457212713937, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '481117', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 967, 'n_words': 150, 'path': 'condition_monitoring/condition_monitoring.ipynb', 'repo_name': 'rajat-bhardwaj/anomaly_detection_financial_risk', 'size': '2681'}