|
@@ -2,7 +2,7 @@ import yaml
|
|
|
import os
|
|
|
import pandas as pd
|
|
|
import numpy as np
|
|
|
-from utils.commonFunc import update_dict, get_db_engine, city_code_transform,age_code_transform, gender_code_transform
|
|
|
+from utils.commonFunc import update_dict, get_db_engine, city_code_transform, age_code_transform, gender_code_transform
|
|
|
from functools import reduce
|
|
|
from itertools import product
|
|
|
from datetime import datetime
|
|
@@ -58,11 +58,6 @@ class BayesFeatures(object):
|
|
|
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
|
|
|
- self.bayes_feature['sample_size'] = df['aclick'].sum() # 素材曝光量,后续计算显著差异用
|
|
|
for sub_combine in self.window_combine:
|
|
|
if sub_combine != ['不限']:
|
|
|
key = self.dim + '_' + '|'.join(sub_combine)
|
|
@@ -87,11 +82,6 @@ class BayesFeatures(object):
|
|
|
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
|
|
|
- self.bayes_feature['sample_size'] = df['bclick'].sum() # 素材行为量,后续计算显著差异用
|
|
|
for sub_combine in self.window_combine:
|
|
|
if sub_combine != '不限':
|
|
|
key = self.dim + '_' + '|'.join(sub_combine)
|
|
@@ -127,14 +117,30 @@ class BayesCombine(object):
|
|
|
self.bayes_combine_df = pd.DataFrame()
|
|
|
self.actual_prob = None
|
|
|
self.sample_size = None
|
|
|
+ self.sig_pos_pct = None
|
|
|
+ self.sig_neg_pct = None
|
|
|
|
|
|
def get_bayes_estimate(self):
|
|
|
- dim_combine_lst = [[key for key in fea.keys() if key not in ['sig_pos_pct', 'sig_neg_pct', 'sample_size']]
|
|
|
- for fea in self.dim_features]
|
|
|
+ dim_combine_lst = [[key for key in fea.keys()] for fea in self.dim_features]
|
|
|
self.dim_features = reduce(update_dict, self.dim_features)
|
|
|
|
|
|
- self.actual_prob = self.dim_features['sig_pos_pct']
|
|
|
- self.sample_size = self.dim_features['sample_size']
|
|
|
+ # 通过 年龄对应的表: ctop_kuaishou_audience_daily_report_by_signature_age 来获取样本量,和正负比例值(其他表的数据可能会丢失导致不全)
|
|
|
+ get_sample_and_pct_sql = """
|
|
|
+ select sum(aclick) aclick, sum(bclick) bclick, sum(activation) activation from %s where
|
|
|
+ signature = '%s'
|
|
|
+ -- and datediff(now(),stat_date)<=30
|
|
|
+ """ % (config['bayesDim']['age']['table'],self.signature)
|
|
|
+ sample_pct_df = pd.read_sql(get_sample_and_pct_sql, engine)
|
|
|
+ if self.target_type == 'action_ratio':
|
|
|
+ self.sig_pos_pct = sample_pct_df['bclick'].sum() / sample_pct_df['aclick'].sum()
|
|
|
+ self.sig_neg_pct = 1 - self.sig_pos_pct
|
|
|
+ self.actual_prob = self.sig_pos_pct
|
|
|
+ self.sample_size = int(sample_pct_df.aclick.sum())
|
|
|
+ elif self.target_type == 'convert_ratio':
|
|
|
+ self.sig_pos_pct = sample_pct_df['activation'].sum() / sample_pct_df['bclick'].sum()
|
|
|
+ self.sig_neg_pct = 1 - self.sig_pos_pct
|
|
|
+ self.actual_prob = self.sig_pos_pct
|
|
|
+ self.sample_size = int(sample_pct_df.bclick.sum())
|
|
|
|
|
|
for ele in product(*dim_combine_lst):
|
|
|
pos_pct_lst = [self.dim_features[key]['pos_pct'] for key in ele]
|
|
@@ -143,7 +149,7 @@ class BayesCombine(object):
|
|
|
neg_pct_lst = [self.dim_features[key]['neg_pct'] for key in ele]
|
|
|
prob_neg = reduce(lambda x, y: x * y, neg_pct_lst)
|
|
|
|
|
|
- prob = (prob_pos * self.dim_features['sig_pos_pct']) / (prob_neg * self.dim_features['sig_neg_pct'])
|
|
|
+ prob = (prob_pos * self.sig_pos_pct) / (prob_neg * self.sig_neg_pct)
|
|
|
out_dict = dict(zip([e.split('_')[0] for e in ele], [e.split('_')[-1] for e in ele]))
|
|
|
|
|
|
# 计算该组合的概率值 与 实际投放的概率值 是否存在显著性差异:显著好、 显著差
|