时序数据与Embedding
在最近查看腾讯赛赛题介绍的时候突然发现赛题有点熟悉,进而在看渔佬对今年腾讯赛分享,以及大白对DCIC海洋赛的比赛总结时,思路逐渐清晰:所有的时序序列都可以用Embedding的操作。本文将以几个历史比赛案例(按照参赛的时间排序),讲解时序数据与Embedding的应用场景。文章末尾将介绍与腾讯赛相似的几个历史赛题。
01 蛋白质序列
第一次在非典型NLP领域看到Embedding是在“基于人工智能的药物分子筛选”比赛中,这个比赛任务是根据蛋白质序列来预测蛋白质和小分子之间的亲和力数值。
这个开源还是小伍哥的开源,使用蛋白质序列训练一个词向量,然后使用LightGBM进行训练。小伍哥在2年前都这么帅了,赞!
texts = [[word for word in re.findall(r'.{3}',document)]for document in list(protein_concat['Sequence'])]model = Word2Vec(texts,size=n,window=4,min_count=1,negative=3,sg=1,sample=0.001,hs=1,workers=4)
这是一个两年前的比赛,当然Top获奖方案还是需要使用一些领域知识。所以在没有领域知识的情况下或许无脑Embedding是一个不错的选择。
小伍哥的分享:
源碼
##########################################加载包,估计就这些够了,成绩1.34import gcimport reimport sysimport timeimport jiebaimport os.pathimport osimport datetimeimport numpy as npimport pandas as pdimport lightgbm as lgbimport gensimfrom gensim.models import Word2Vec######################################## data read ######################################工作空间设置data_path = 'C:/Users/wuzhengxiang/Desktop/晶泰科技比赛数据/'os.chdir(data_path)#设置当前工作空间print (os.getcwd())#获得当前工作目录#数据读取df_protein_train = pd.read_csv('df_protein_train.csv')#1653df_protein_test = pd.read_csv('df_protein_test.csv')#414protein_concat = pd.concat([df_protein_train,df_protein_test])df_molecule = pd.read_csv('df_molecule.csv')#111216df_affinity_train = pd.read_csv('df_affinity_train.csv')#165084df_affinity_test = pd.read_csv('df_affinity_test_toBePredicted.csv')#41383df_affinity_test['Ki'] = -11data = pd.concat([df_affinity_train,df_affinity_test])######################################################################################################### feature ############################################################################################################1、Fingerprint分子指纹处理展开feat = []for i in range(0,len(df_molecule)):feat.append(df_molecule['Fingerprint'][i].split(','))feat = pd.DataFrame(feat)feat = feat.astype('int')feat.columns=["Fingerprint_{0}".format(i) for i in range(0,167)]feat["Molecule_ID"] = df_molecule['Molecule_ID']data = data.merge(feat, on='Molecule_ID', how='left')#2、df_molecule其他特征处理feat = df_molecule.drop('Fingerprint',axis=1)data = data.merge(feat, on='Molecule_ID', how='left')#3、protein 蛋白质 词向量训练 看不懂的加公众号:Python_R_wu,稍后会讲解下,来不及写n = 128texts = [[word for word in re.findall(r'.{3}',document)]for document in list(protein_concat['Sequence'])]model = Word2Vec(texts,size=n,window=4,min_count=1,negative=3,sg=1,sample=0.001,hs=1,workers=4)vectors = pd.DataFrame([model[word] for word in (model.wv.vocab)])vectors['Word'] = list(model.wv.vocab)vectors.columns= ["vec_{0}".format(i) for i in range(0,n)]+["Word"]wide_vec = pd.DataFrame()result1=[]aa = list(protein_concat['Protein_ID'])for i in range(len(texts)):result2=[]for w in range(len(texts[i])):result2.append(aa[i])result1.extend(result2)wide_vec['Id'] = result1result1=[]for i in range(len(texts)):result2=[]for w in range(len(texts[i])):result2.append(texts[i][w])result1.extend(result2)wide_vec['Word'] = result1del result1,result2wide_vec = wide_vec.merge(vectors,on='Word', how='left')wide_vec = wide_vec.drop('Word',axis=1)wide_vec.columns = ['Protein_ID']+["vec_{0}".format(i) for i in range(0,n)]del vectorsname = ["vec_{0}".format(i) for i in range(0,n)]feat = pd.DataFrame(wide_vec.groupby(['Protein_ID'])[name].agg('mean')).reset_index()feat.columns=["Protein_ID"]+["mean_ci_{0}".format(i) for i in range(0,n)]data = data.merge(feat, on='Protein_ID', how='left')#################################### lgb ############################train_feat = data[data['Ki']> -11].fillna(0)testt_feat = data[data['Ki']<=-11].fillna(0)label_x = train_feat['Ki']label_y = testt_feat['Ki']submission = testt_feat[['Protein_ID','Molecule_ID']]len(testt_feat)train_feat = train_feat.drop('Ki',axis=1)testt_feat = testt_feat.drop('Ki',axis=1)train_feat = train_feat.drop('Protein_ID',axis=1)testt_feat = testt_feat.drop('Protein_ID',axis=1)train_feat = train_feat.drop('Molecule_ID',axis=1)testt_feat = testt_feat.drop('Molecule_ID',axis=1)#lgb算法train = lgb.Dataset(train_feat, label=label_x)test = lgb.Dataset(testt_feat, label=label_y,reference=train)params = {'boosting_type': 'gbdt','objective': 'regression_l2','metric': 'l2',#'objective': 'multiclass',#'metric': 'multi_error',#'num_class':5,'min_child_weight': 3,'num_leaves': 2 ** 5,'lambda_l2': 10,'subsample': 0.7,'colsample_bytree': 0.7,'colsample_bylevel': 0.7,'learning_rate': 0.05,'tree_method': 'exact','seed': 2017,'nthread': 12,'silent': True}num_round = 3000gbm = lgb.train(params,train,num_round,verbose_eval=50,valid_sets=[train,test])preds_sub = gbm.predict(testt_feat)#结果保存nowTime=datetime.datetime.now().strftime('%m%d%H%M')#现在name='lgb_'+nowTime+'.csv'submission['Ki'] = preds_subsubmission.to_csv(name, index=False)
B榜第三名的分享:
02 病毒序列
第二次在非典型NLP比赛中看到Embedding是在“第三届阿里云安全赛”中,这个比赛任务是需要根据程序的API序列进行分类。
这场比赛我参加过,也因此在线下赛认识了大白。安全赛中也是不同的病毒API是一个单词,执行序列组成一个样本。
但是在这个比赛中,由于API个数比较少,所以Embedding反而没有TF-IDF有效。当然在stacking阶段,Embedding也是有提升的。
03 船舶序列
最近一次是在最近结束的DCIC海洋赛中看到了Embedding,这也是一个非典型的NLP比赛,赛题任务需要根据渔船的运动轨迹进行行为分类。
这场比赛大白也参加了,每个渔船id的速度、经纬度看做是一个序列信息,利用速度、经纬度的分位数统计量,将浮点特征分桶转成一个类型特征。
使用深度学习的word2vec的CBOW算法无监督训练,获取经纬度(x-y)和速度(speed)的类型向量,每个渔船id的经纬度和速度向量取平均作为特征,这个思路和Fasttext比较类似。
大白的分享:
04 APP序列
最近一次是在易观用户性别年龄预测比赛中遇到,这也是一个非典型的NLP比赛,赛题任务需要根据用户手机APP使用序列来对用户的年龄和性别进行分类。
在易观这场比赛中,chizhu获得了冠军,我是亚军。这场比赛的核心也是APP序列建模,使用Embedding构建特征。
chizhu在易观的分享:
看到这里,有没有发现本次腾讯赛的赛题也是这个路子。大赛的题目尝试从另一个方向来验证这个假设,即以用户在广告系统中的交互行为作为输入来预测用户的人口统计学属性。
如果你参加了本次腾讯赛,chizhu 的分享可以参考。Coggle数据科学也会持续关注,大家一起学起来~
