一、业务背景
- 传统广告与在线广告区别?
传统广告: 类似电视广告,报纸媒体、杂志、广播、户外媒体等。
在线广告: 类似百度搜索广告,facebook页面展示广告。
区别:在线广告更多与用户相关,例,在google上搜索“kids shoes”,则会出现与搜索相关的一些带有广告标签(“Ad”)的连接。例如,淘宝中带有hot标签的物品。
注: 国家规定如果是广告的话,必须带有广告标签。 - 传统广告与在线广告区别?
通过上图可以看到,互联网广告回报率近年来不断上升,主要是存在许多商家对曝光的需求度较高。而且这种在线广告是于用户相关的,而不是一些硬性的广告。例如: google公司广告收入占总收入的90%,所以其他 - 在线广告投放类型
Retarget 基于用户行为的广告: 用户在网站中浏览的一些信息,进行广告投放。
Behavior: 用户在京东和淘宝中浏览一些物品,如果你跳转到一些其他网站,则会发现有一些你曾经看过的一些物品
GEO基于位置: 例如美观点评, 例如用户吃饭。 - 广告角色
平台方:提供广告展示的平台,例如腾讯,百度等
用户方: 即浏览广告的用户
商家: 需要曝光商品的商家。 - 广告收费模式
1) 按曝光收费: 按每曝光多少次收费,这类一般是展示广告,例如腾讯多是展示广告
2) 按点击预估: 点击后收费,特别是百度搜索广告(特别是医疗广告,每点击一次100~200元),淘宝直通车(淘宝大概点击一次2元左右)。
3) 按购买预估: 按购买后收取广告费。这种比较少,广告费比较高,但是一旦发生则利润非常高。 - 点击率计算方式(CPC)
- 点击率预估
可展示广告位置与次数是有限,所以需要尽可能的将用户可能点击的广告展示给用户,使得最终广告费最大。因此需要做点击率预估。 - 广告特征提取
商品角度: 是不是热销商品, 评价率,退货率等,即可表现商品的质量
广告内容: 搜的蓝色牛仔裤,搜索结果是否接近我搜索关键词,也会影响用户点击。
用户偏好: 有些一用户喜欢个性,不喜欢大众的
二、LR做CTR预估
背景
https://www.kaggle.com/c/avazu-ctr-prediction/rules
kaggel广告点击率预估案例code
#load数据
import pandas as pd
data = pd.read_csv('train_small.csv', verbose=False)
data.shape
#import graphlab as gl
#data = gl.SFrame.read_csv('train_subset.csv', verbose=False)
#数据包含列及类型了解
data.info()
#数据概览
data.head(10)
#click点击率baseline
data['click'].mean()
#数据探索
#data.groupby('device_type', {'CTR':gl.aggregate.MEAN('click')})
data['device_type'].groupby([data['device_type'],data['click']]).count()
#数据探索
#data.groupby('C1', {'CTR':gl.aggregate.MEAN('click')})
data['C1'].groupby([data['C1'],data['click']]).count()
#数据探索
#data['C15'].sketch_summary().frequent_items()
data['C15'].groupby([data['C15']]).count()
#数据探索
#data['C16'].sketch_summary().frequent_items()
data['C16'].groupby([data['C16']]).count()
#实际为分类变量但存储为int, 此处转换为字符串
data['device_type'] = data['device_type'].astype(str)
data['C1'] = data['C1'].astype(str)
data.info()
#训练集与测试集切割
#train_data, test_data = data.random_split(0.8, seed=1)
from sklearn import cross_validation #交叉验证
train_data, test_data = cross_validation.train_test_split(data, test_size=0.3, random_state=0)
#模型训练
model_feature='click|device_type|C1|C15|C16'
model_index=['click','device_type','C1','C15','C16']
train_df = train_data.filter(regex=model_feature)
index=model_index
train_df=train_df.reindex_axis(index,axis=1)
train_df.info()
from sklearn import linear_model #训练模型
x_train=train_df.as_matrix()[:,1:]
y_train= train_df.as_matrix()[:,0]
clf = linear_model.LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
clf.fit(x_train,y_train)
#保存模型
from sklearn.externals import joblib #保存模型
joblib.dump(clf, "train_model.m")
#模型权重与偏执项
clf.coef_.T,clf.intercept_[0]
#model.predict(test_data, output_type='probability').head(5)
#将模型权重与属性对应
import re
from pandas import Series,DataFrame #数据存储结构
import pandas as pd #数据分析
replace_reg = re.compile(r'\[|\]')
coef_feature=pd.DataFrame({"columns":list(train_df.columns)[1:], "coef":[replace_reg.sub('',str(value)) for value in list(clf.coef_.T)]}) #系数与属性对应
coef_b=Series({ 'columns':'intercept_','coef':str(clf.intercept_[0])})
coef_feature=coef_feature.append(coef_b,ignore_index=True)
coef_feature
#预测数据处理
cv_df = test_data.filter(regex=model_feature)
index=model_index
cv_df=cv_df.reindex_axis(index,axis=1)
x_test=cv_df.as_matrix()[:,1:]
y_test=cv_df.as_matrix()[:,0]
x_test,y_test
#模型预测
proba=clf.predict_proba(x_test)[:,1]
test_data['proba']=proba
predictions=clf.predict(x_test)
test_data['predictions']= predictions
#test_data['proba'] #预测概率查看
#模型评估
from sklearn.metrics import precision_recall_curve, roc_curve, auc #模型效果评估
from sklearn.metrics import classification_report #
precision, recall, thresholds = precision_recall_curve(y_test, test_data['proba'])
precision, recall, thresholds
report = test_data['proba'] > 0.5
#type(y_test[0])
y_test=[int(x) for x in y_test] #lable是字符串型,需要转换为int型
report_result=classification_report(y_test, report, target_names = ['neg', 'pos'])
report_result
3、结果
训练权重
模型评估