腾讯算法大赛

数据预处理之——腾讯广告算法大赛

2019-04-11  本文已影响135人  Neural_PDE

```

#首先了解如何快速做一个列表

df4 = pd.DataFrame({'col1':['1',3],'col2':[2,4]},index=['a','b'])

#接下来我们来看如何处理脏数据

import pandas as pd

import numpy as np

df=pd.DataFrame({"id":["1","2","3,4"]})

df

def max_str(t):

    a=[int(i) for i in t]

    return max(a)

df["id_max"]=df["id"].str.split(",").map(max_str)

df

#腾讯广告算法大赛为例子

##################################制作小样本集##############################

###########################################################################

#第一步 读取数据

df=pd.read_table('C:/Users/fafa/Desktop/testA/user_data',sep = '\t',header=None,engine='python')

#增加 列名

df.columns=['用户ID','年龄','性别','地域','婚恋状态','学历','消费能力','设备','工作状态','连接类型','行为性趣']

#切分数据,使用前2000条

df2=df.head(2000)

#导出切分好的数据

df2.to_excel('C:/Users/fafa/Desktop/testa/user_data.xls')

##################同理,制作 其余的小样本集##########

test=pd.read_table('C:/Users/fafa/Desktop/testA/test_sample.dat',sep = '\t',header=None,engine='python')

test.columns=['样本id','广告id','创建时间','素材尺寸','广告行业id','商品类型','商品id','广告账户id','投放时段','人群定向','出价(单位分)']

test.to_excel('C:/Users/fafa/Desktop/testa/测试数据.xls')

test=pd.read_table('C:/Users/fafa/Desktop/testA/ad_operation.dat',sep = '\t',header=None,engine='python')

test.columns=['广告id','创建/修改时间','操作类型','修改字段','操作后的字段']

df3=test.head(2000)

df3.to_excel('C:/Users/fafa/Desktop/testa/广告操作数据.xls')

df4=pd.read_table('C:/Users/fafa/Desktop/testA/ad_static_feature.out',sep = '\t',header=None,engine='python')

df4.columns=['广告id','创建时间','广告账户id','商品id','商品类型','广告行业id','素材尺寸']

df5=df4.head(2000)

df5.to_excel('C:/Users/fafa/Desktop/testa/广告静态数据.xls')

df=pd.read_table('C:/Users/fafa/Desktop/testA/想',sep = '\t',header=None,engine='python')

df.columns=['广告请求id','广告请求时间','广告位id','用户id','曝光广告id','曝光广告素材尺寸','曝光广告出价bid','曝光广告pctr','曝光广告quality_ecpm','曝光广告totalEcpm']

df=df.head(2000)

df.to_excel('C:/Users/fafa/Desktop/广告曝光日志.xls')

###################################################

###################################################

##########统计日志中广告id的出现次数,并关联其信息##########

###################################################

###################################################

#读取曝光日志

df=pd.read_excel('D:/mini数据集/曝光日志.xls',header=0)

#对"姓名" 计数,得到 “姓名”和“计数”两列。

df.姓名.value_counts().to_frame().reset_index().rename({"index":"姓名","姓名":"计数"},axis=1)

#对"曝光广告id" 计数,得到 “广告id”和“曝光次数”两列。

df1=df.曝光广告id.value_counts().to_frame().reset_index().rename({"index":"广告id","曝光广告id":"曝光次数"},axis=1)df1.head()

#法二

# df['count'] = 1

#df.groupby('Name')['count'].agg('sum')

#关联广告其他属性 到 曝光次数 表格

#读取广告其他属性df2

df2=pd.read_excel('D:/mini数据集/广告静态数据.xls',header=0)df2.head()

#关联

df1.merge(df2,on="广告id")

#删除不需要的列

df4=df3.drop({'创建时间',"广告账户id"},1)df4.head()

#修改列的位置

df=df[['广告id', '商品id', '商品类型', '广告行业id', '素材尺寸','曝光次数']]

#################################################

###########以下是直接调取大形数据制作训练集#########

#################################################

#第一步 读取数据df1=pd.read_table('C:/Users/fafa/Desktop/testA/totalExposureLog.out',sep = '\t',header=None,engine='python')df2=pd.read_table('C:/Users/fafa/Desktop/testA/ad_static_feature.out',sep = '\t',header=None,engine='python')

#增加 列名

df1.columns=['广告请求id','广告请求时间','广告位id','用户id','曝光广告id','曝光广告素材尺寸','曝光广告出价bid','曝光广告pctr','曝光广告quality_ecpm','曝光广告totalEcpm']df2.columns=['广告id','创建时间','广告账户id','商品id','商品类型','广告行业id','素材尺寸']#对"曝光广告id" 计数,得到 “广告id”和“曝光次数”两列。

df3=df1.曝光广告id.value_counts().to_frame().reset_index().rename({"index":"广告id","曝光广告id":"曝光次数"},axis=1) df1.head()

#关联广告其他属性 到 曝光次数 表格

df3.merge(df2,on="广告id")

#删除不需要的列

df4=df3.drop({'创建时间',"广告账户id"},1) df4.head()

#修改列的位置

df5=df4[['广告id', '商品id', '商品类型', '广告行业id', '素材尺寸','曝光次数']]

#发现id中存在脏数据 所以清理一下(方法见脏数据的清理)

def max_str(t):

    a=[int(i) for i in t]

    return max(a)

df5["广告id"]=df5["广告id"].str.split(",").map(max_str)

df5["商品id"]=df5["商品id"].str.split(",").map(max_str)

df5["广告行业id"]=df5["广告行业id"].str.split(",").map(max_str)

#令空值NaN为0

df5.fillna(0)

df5.head()

###最终得到的df5就是一个数据集合,最后一列是Y,其余列都是特征X(要注意这里df5是有列名的)。然后套用nn模板(或者Light gbm 代码在此)来训练即可。

####nn模板如下####

# -*- coding: utf-8 -*-

import pandas as pd

import numpy as np

from keras import metrics

from keras.models import Sequential

from keras.layers import Dense

from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import KFold, cross_val_scoredataset=pd.read_csv('housing.csv',header=None)

X=dataset.iloc[:,0:13]

Y=dataset.iloc[:,13]

# print(Y)

seed=7

np.random.seed(seed)

# 建立模型

optimizer='adam'

init='normal'

model=Sequential()

model.add(Dense(units=13,activation='relu',input_dim=13,kernel_initializer=init))

#构建更多的隐藏层

model.add(Dense(units=10,activation='relu',kernel_initializer=init))

model.add(Dense(units=1,kernel_initializer=init))

#输出层不需要进行激活函数,预测回归的话unit=1# 编译模型

model.compile(loss='mse',optimizer=optimizer,metrics=['acc'])

model.fit(X.values,Y.values,epochs=30,batch_size=64)

```

数据分析基本过程

XGBOOST模型训练数据集


If you are interested in this topic.
You can get in touch with me.
18234056952(Tel  wechat  qq)

上一篇 下一篇

猜你喜欢

热点阅读