|
@@ -54,51 +54,67 @@ class BayesFeatures(object):
|
|
|
''' % (self.file_name, self.table, self.signature, self.file_name)
|
|
|
df = pd.read_sql(sql, test_engine)
|
|
|
|
|
|
- if self.dim != 'city':
|
|
|
- if self.target_type == 'action_ratio':
|
|
|
- # 计算行为率(bclick/aclick)的组合特征值
|
|
|
- df['unbclick'] = df['aclick'] - df['bclick']
|
|
|
- sig_pos_pct = df['bclick'].sum() / df['aclick'].sum() # 素材曝光-点击的概率
|
|
|
- sig_neg_pct = 1 - sig_pos_pct # 素材曝光-未点击的概率
|
|
|
- self.bayes_feature['sig_pos_pct'] = sig_pos_pct
|
|
|
- self.bayes_feature['sig_neg_pct'] = sig_neg_pct
|
|
|
-
|
|
|
- for sub_combine in self.window_combine:
|
|
|
- if sub_combine != ['不限']:
|
|
|
- key = self.dim + '_' + '|'.join(sub_combine)
|
|
|
+ if self.target_type == 'action_ratio':
|
|
|
+ # 计算行为率(bclick/aclick)的组合特征值
|
|
|
+ df['unbclick'] = df['aclick'] - df['bclick']
|
|
|
+ sig_pos_pct = df['bclick'].sum() / df['aclick'].sum() # 素材曝光-点击的概率
|
|
|
+ sig_neg_pct = 1 - sig_pos_pct # 素材曝光-未点击的概率
|
|
|
+ self.bayes_feature['sig_pos_pct'] = sig_pos_pct
|
|
|
+ self.bayes_feature['sig_neg_pct'] = sig_neg_pct
|
|
|
+ for sub_combine in self.window_combine:
|
|
|
+ if sub_combine != ['不限']:
|
|
|
+ key = self.dim + '_' + '|'.join(sub_combine)
|
|
|
+ if self.dim != 'city':
|
|
|
pos_pct = df[df[self.file_name].isin(sub_combine)].bclick.sum() / df.bclick.sum()
|
|
|
neg_pct = df[df[self.file_name].isin(sub_combine)].unbclick.sum() / df.unbclick.sum()
|
|
|
- self.bayes_feature[key] = {'pos_pct': pos_pct, 'neg_pct': neg_pct}
|
|
|
- else:
|
|
|
- self.bayes_feature[self.dim + '_' + '不限'] = {'pos_pct': 1, 'neg_pct': 1}
|
|
|
-
|
|
|
- if self.target_type == 'convert_ratio':
|
|
|
- # 计算转化率(activation/bclick)的组合特征值
|
|
|
- df['unactivation'] = df['bclick'] - df['activation']
|
|
|
- sig_pos_pct = df['activation'].sum() / df['bclick'].sum() # 素材点击-转化的概率
|
|
|
- sig_neg_pct = 1 - sig_pos_pct # 素材点击-未转化的概率
|
|
|
- self.bayes_feature['sig_pos_pct'] = sig_pos_pct
|
|
|
- self.bayes_feature['sig_neg_pct'] = sig_neg_pct
|
|
|
-
|
|
|
- for sub_combine in self.window_combine:
|
|
|
- if sub_combine != '不限':
|
|
|
- key = self.dim + '_' + '|'.join(sub_combine)
|
|
|
+ if self.dim == 'city':
|
|
|
+ # 获取 city_level 对应的 city_name
|
|
|
+ city_lst = []
|
|
|
+ for city_level in sub_combine:
|
|
|
+ get_city_sql = """
|
|
|
+ select city_name from ctop_kuaishou_city_level where city_level = '%s'
|
|
|
+ """ % city_level
|
|
|
+ city_df = pd.read_sql(get_city_sql, engine)
|
|
|
+ city_lst.extend(city_df['city_name'].values)
|
|
|
+ pos_pct = df[df[self.file_name].isin(city_lst)].bclick.sum() / df.bclick.sum()
|
|
|
+ neg_pct = df[df[self.file_name].isin(city_lst)].unbclick.sum() / df.unbclick.sum()
|
|
|
+ self.bayes_feature[key] = {'pos_pct': pos_pct, 'neg_pct': neg_pct}
|
|
|
+ else:
|
|
|
+ self.bayes_feature[self.dim + '_' + '不限'] = {'pos_pct': 1, 'neg_pct': 1}
|
|
|
+
|
|
|
+ if self.target_type == 'convert_ratio':
|
|
|
+ # 计算转化率(activation/bclick)的组合特征值
|
|
|
+ df['unactivation'] = df['bclick'] - df['activation']
|
|
|
+ sig_pos_pct = df['activation'].sum() / df['bclick'].sum() # 素材点击-转化的概率
|
|
|
+ sig_neg_pct = 1 - sig_pos_pct # 素材点击-未转化的概率
|
|
|
+ self.bayes_feature['sig_pos_pct'] = sig_pos_pct
|
|
|
+ self.bayes_feature['sig_neg_pct'] = sig_neg_pct
|
|
|
+ for sub_combine in self.window_combine:
|
|
|
+ if sub_combine != '不限':
|
|
|
+ key = self.dim + '_' + '|'.join(sub_combine)
|
|
|
+ if self.dim != 'city':
|
|
|
pos_pct = df[df[self.file_name].isin(sub_combine)].activation.sum() / df.activation.sum()
|
|
|
neg_pct = df[df[self.file_name].isin(sub_combine)].unactivation.sum() / df.unactivation.sum()
|
|
|
- self.bayes_feature[key] = {'pos_pct': pos_pct, 'neg_pct': neg_pct}
|
|
|
- else:
|
|
|
- self.bayes_feature[self.dim + '_' + '不限'] = {'pos_pct': 1, 'neg_pct': 1}
|
|
|
- elif self.dim == 'city':
|
|
|
- pass
|
|
|
- else:
|
|
|
- pass
|
|
|
+ if self.dim == 'city':
|
|
|
+ # 获取 city_level 对应的 city_name
|
|
|
+ city_lst = []
|
|
|
+ for city_level in sub_combine:
|
|
|
+ get_city_sql = """
|
|
|
+ select city_name from ctop_kuaishou_city_level where city_level = '%s'
|
|
|
+ """ % city_level
|
|
|
+ city_df = pd.read_sql(get_city_sql, engine)
|
|
|
+ city_lst.extend(city_df['city_name'].values)
|
|
|
+ pos_pct = df[df[self.file_name].isin(city_lst)].activation.sum() / df.activation.sum()
|
|
|
+ neg_pct = df[df[self.file_name].isin(city_lst)].unactivation.sum() / df.unactivation.sum()
|
|
|
+ self.bayes_feature[key] = {'pos_pct': pos_pct, 'neg_pct': neg_pct}
|
|
|
+ else:
|
|
|
+ self.bayes_feature[self.dim + '_' + '不限'] = {'pos_pct': 1, 'neg_pct': 1}
|
|
|
|
|
|
|
|
|
class BayesCombine(object):
|
|
|
"""
|
|
|
依据特征值,计算多维度的组合预估值
|
|
|
"""
|
|
|
-
|
|
|
def __init__(self, signature, project_id, target_type, dim_features):
|
|
|
self.signature = signature
|
|
|
self.project_id = project_id
|