天天看点

kaggle案例:广告点击率预估+LR

一、业务背景

  1. 传统广告与在线广告区别?

    传统广告: 类似电视广告,报纸媒体、杂志、广播、户外媒体等。

    在线广告: 类似百度搜索广告,facebook页面展示广告。

    区别:在线广告更多与用户相关,例,在google上搜索“kids shoes”,则会出现与搜索相关的一些带有广告标签(“Ad”)的连接。例如,淘宝中带有hot标签的物品。

    注: 国家规定如果是广告的话,必须带有广告标签。

  2. 传统广告与在线广告区别?
    kaggle案例:广告点击率预估+LR
    通过上图可以看到,互联网广告回报率近年来不断上升,主要是存在许多商家对曝光的需求度较高。而且这种在线广告是于用户相关的,而不是一些硬性的广告。例如: google公司广告收入占总收入的90%,所以其他
  3. 在线广告投放类型

    Retarget 基于用户行为的广告: 用户在网站中浏览的一些信息,进行广告投放。

    Behavior: 用户在京东和淘宝中浏览一些物品,如果你跳转到一些其他网站,则会发现有一些你曾经看过的一些物品

    GEO基于位置: 例如美观点评, 例如用户吃饭。

  4. 广告角色

    平台方:提供广告展示的平台,例如腾讯,百度等

    用户方: 即浏览广告的用户

    商家: 需要曝光商品的商家。

  5. 广告收费模式

    1) 按曝光收费: 按每曝光多少次收费,这类一般是展示广告,例如腾讯多是展示广告

    2) 按点击预估: 点击后收费,特别是百度搜索广告(特别是医疗广告,每点击一次100~200元),淘宝直通车(淘宝大概点击一次2元左右)。

    3) 按购买预估: 按购买后收取广告费。这种比较少,广告费比较高,但是一旦发生则利润非常高。

  6. 点击率计算方式(CPC)
    kaggle案例:广告点击率预估+LR
  7. 点击率预估

    可展示广告位置与次数是有限,所以需要尽可能的将用户可能点击的广告展示给用户,使得最终广告费最大。因此需要做点击率预估。

  8. 广告特征提取

    商品角度: 是不是热销商品, 评价率,退货率等,即可表现商品的质量

    广告内容: 搜的蓝色牛仔裤,搜索结果是否接近我搜索关键词,也会影响用户点击。

    用户偏好: 有些一用户喜欢个性,不喜欢大众的

二、LR做CTR预估

  1. 背景

    https://www.kaggle.com/c/avazu-ctr-prediction/rules

    kaggel广告点击率预估案例

  2. 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()
#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=, random_state=) 

#模型训练
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=)  
train_df.info()

from sklearn import linear_model #训练模型
x_train=train_df.as_matrix()[:,:]
y_train= train_df.as_matrix()[:,]
clf = linear_model.LogisticRegression(C=, penalty='l1', tol=)
clf.fit(x_train,y_train)

#保存模型
from sklearn.externals import joblib #保存模型
joblib.dump(clf, "train_model.m")

#模型权重与偏执项 
clf.coef_.T,clf.intercept_[]

#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)[:], "coef":[replace_reg.sub('',str(value)) for value in list(clf.coef_.T)]}) #系数与属性对应    
coef_b=Series({ 'columns':'intercept_','coef':str(clf.intercept_[])})
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=)
x_test=cv_df.as_matrix()[:,:]
y_test=cv_df.as_matrix()[:,]
x_test,y_test

#模型预测
proba=clf.predict_proba(x_test)[:,]
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'] > 
#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、结果

训练权重

kaggle案例:广告点击率预估+LR

模型评估

kaggle案例:广告点击率预估+LR

继续阅读