concept current customer dataset in category churn

appears as: current customer dataset
Fighting Churn with Data

This is an excerpt from Manning's book Fighting Churn with Data.

Listing 4.6 shows an SQL SELECT statement that creates a current customer dataset suitable for segmentation. It uses two tricks: first, a CTE selects the most recent date with metrics using MAX aggregation. You also have the option of simply hardcoding a date, but presumably the most recent date with any metrics would be the last day you updated your data. Then listing 4.6 uses the flattening aggregation trick you learned in the last section. That's all there is to it! Run the SQL and check that the result is similar to that from listing 4.5, but with all the accounts observed on a single date: the most recent.

But there is a key difference repeating the process for the current customer dataset: You don't want to calculate new statistics to convert the metric into scores, nor do you want to create a new loading matrix to group the correlated metrics. For the current dataset you want to repeat the process using exactly the same statistics and loading matrix as you derived from the historical customer dataset. You have to reuse the exact same parameters and loading matrix for the current customers to ensure that every column in the current customer dataset that you put into the regression has exactly the same meaning as the same column had in the historical dataset.

For example, consider what would happen if you calculated a new loading matrix on the current customer dataset and found a different number of groups. (That could happen if the the current dataset metrics had correlations different from the historical dataset metrics.) Then there would be no way to map a current customers grouped metrics to those that the regression expected from fitting on the historical dataset. Even the mean and standard deviation used for scoring should be those calculated on the historical data. Because the mean and standard deviation used for the scaling were not derived from the current customer data, for the current customer scores the mean may not be exactly zero and the standard deviation may not be exactly 1. But this is correct as long as those differences reflect the true difference between the current customers and the historical customers.

So the scoring process for the current customer dataset is going to have the following four steps:

Listing 10.7 Preparing a current customer dataset with demographic fields
import pandas as pd
from listing_7_5_fat_tail_scores import transform_fattail_columns, transform_skew_columns
from listing_8_4_rescore_metrics import score_current_data, group_current_data, reload_churn_data
from listing_10_4_dummy_variables import dummy_variables
 
def rescore_wcats(data_set_path,categories,groups):
 
   current_path = data_set_path.replace('.csv', '_current.csv')
   dummy_variables(current_path,groups, current=True)    #A
   current_dummies = reload_churn_data(data_set_path,'current_dummies_groupscore', 
                                       '10.7',is_customer_data=True)
   align_dummies(current_dummies,data_set_path)    #B
 
   nocat_path = data_set_path.replace('.csv', '_nocat.csv')     #C
   load_mat_df = reload_churn_data(nocat_path,'load_mat','6.4',is_customer_data=False)
   score_df = reload_churn_data(nocat_path,'score_params','7.5',is_customer_data=False)
   current_nocat = reload_churn_data(data_set_path,'current_nocat','10.7',is_customer_data=True)
   assert set(score_df.index.values)==set(current_nocat.columns.values),
          "Data to re-score does not match transform params"
   assert set(load_mat_df.index.values)==set(current_nocat.columns.values),
          "Data to re-score does not match loading matrix"
   transform_skew_columns(current_nocat,score_df[score_df['skew_score']].index.values)
   transform_fattail_columns(current_nocat,score_df[score_df['fattail_score']].index.values)
   scaled_data = score_current_data(current_nocat,score_df,data_set_path)
   grouped_data = group_current_data(scaled_data, load_mat_df,data_set_path)
 
   group_dum_df = grouped_data.merge(current_dummies,left_index=True,right_index=True) #D
   group_dum_df.to_csv(data_set_path.replace('.csv', '_current_groupscore.csv'),header=True)    #E
 
   current_df = reload_churn_data(data_set_path,'current','10.7',is_customer_data=True)
   save_segment_data_wcats(grouped_data,current_df,load_mat_df,data_set_path, categories)    #F
 
def align_dummies(current_data,data_set_path):
   current_groupments=pd.read_csv(data_set_path.replace('.csv', '_current_dummies_groupmets.csv'),
                                                        index_col=0)    #G
   new_dummies = set(current_groupments['metrics'])
   original_groupmets = pd.read_csv(data_set_path.replace('.csv', '_dummies_groupmets.csv'),
                                                          index_col=0)    #H
   old_dummies = set(original_groupmets['metrics'])
   missing_in_new = old_dummies.difference(new_dummies)    #I
   for col in missing_in_new:    #J
       current_data[col]=0.0
   missing_in_old = new_dummies.difference(old_dummies)    #K
   for col in missing_in_old:    #L
       current_data.drop(col,axis=1,inplace=True)
 
def save_segment_data_wcats(current_data_grouped, current_data, load_mat_df, data_set_path, categories):
   group_cols =  load_mat_df.columns[load_mat_df.astype(bool).sum(axis=0) > 1]    #M
   no_group_cols = list(load_mat_df.columns[load_mat_df.astype(bool).sum(axis=0) == 1])   #N
   no_group_cols.extend(categories)   #O
   segment_df = current_data_grouped[group_cols].join(current_data[no_group_cols])   #P
   segment_df.to_csv(data_set_path.replace('.csv', '_current_groupmets_segment.csv'),header=True)