数据分析
首先还是对数据集进行解释,以及简单验证数据集的正确性。信用卡交易记录包括了两个数据集,分别是historical_transactions和new_merchant_transactions。两个数据集字段类似,只是记录了不同时间区间的信用卡消费情况:
这里的数据存在两个 一个18以前的数据集 一个18以后的
数据解读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   |  history_transaction = pd.read_csv('./eloData/historical_transactions.csv', header=0) history_transaction.head(5) history_transaction.info() ''' <class 'pandas.core.frame.DataFrame'> RangeIndex: 29112361 entries, 0 to 29112360 Data columns (total 14 columns):  #   Column                Dtype   ---  ------                -----    0   authorized_flag       object   1   card_id               object   2   city_id               int64    3   category_1            object   4   installments          int64    5   category_3            object   6   merchant_category_id  int64    7   merchant_id           object   8   month_lag             int64    9   purchase_amount       float64  10  purchase_date         object   11  category_2            float64  12  state_id              int64    13  subsector_id          int64   dtypes: float64(2), int64(6), object(6) memory usage: 3.0+ GB '''
  new_transaction = pd.read_csv('./eloData/new_merchant_transactions.csv', header=0) new_transaction.head(5) pd.read_csv('./eloData/new_merchant_transactions.csv', header=0).info() ''' 该数据中总共有将近200万条数据。并且我们发现,该数据集中有较多字段和商家数据merchant重复,我们可以对其进行简单检验。 '''
 
  | 
 
首先简单查看有哪些字段一致:
1 2 3 4 5 6 7
   | duplicate_cols = []
  for col in merchant.columns:     if col in new_transaction.columns:         duplicate_cols.append(col)          print(duplicate_cols)
   | 
 
并且我们进一步发现,交易记录中的merhcant_id信息并不唯一:
1 2 3 4
   |  new_transaction[duplicate_cols].drop_duplicates().shape
  new_transaction['merchant_id'].nunique()
 
  | 
 
造成该现象的原因可能是商铺在逐渐经营过程动态变化,而基于此,在后续的建模过程中,我们将优先使用交易记录中表中的相应记录。
数据预处理
首先也是一样,需要对其连续/离散变量进行标注。当然该数据集中比较特殊的一点,是存在一个时间列,我们将其单独归为一类:
1 2 3 4 5 6 7
   | numeric_cols = ['installments', 'month_lag', 'purchase_amount'] category_cols = ['authorized_flag', 'card_id', 'city_id', 'category_1',        'category_3', 'merchant_category_id', 'merchant_id', 'category_2', 'state_id',        'subsector_id'] time_cols = ['purchase_date']
  assert len(numeric_cols) + len(category_cols) + len(time_cols) == new_transaction.shape[1]
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   |  new_transaction[category_cols].dtypes ''' authorized_flag          object card_id                  object city_id                   int64 category_1               object category_3               object merchant_category_id      int64 merchant_id              object category_2              float64 state_id                  int64 subsector_id              int64 dtype: object ''' new_transaction[category_cols].isnull().sum() ''' authorized_flag              0 card_id                      0 city_id                      0 category_1                   0 category_3               55922 merchant_category_id         0 merchant_id              26216 category_2              111745 state_id                     0 subsector_id                 0 dtype: int64 ''' 和此前的merchant处理类似,我们对其object类型对象进行字典编码(id除外),并对利用-1对缺失值进行填补: for col in ['authorized_flag', 'category_1', 'category_3']:     new_transaction[col] = change_object_cols(new_transaction[col].fillna(-1).astype(str))      new_transaction[category_cols] = new_transaction[category_cols].fillna(-1) new_transaction[category_cols].dtypes
 
  |