前沿:

这是天池的一个新人实战塞题目,原址 https://tianchi.aliyun.com/getStart/information.htm?spm=5176.100067.5678.2.e1321db7ydQmSB&raceId=231593 ,下文会分析以下几个过程。

1.数据预处理

2.特征的选取

3.算法的说明

4.结果分析

5.其他

第一部分:数据预处理

原始数据可以从上边链接中下载,拿到.csv文件,可以使用pandas处理。

比如:

dfoff = pd.read_csv('ccf_offline_stage1_train.csv', keep_default_na=False)

参数  keep_default_na默认为True,当为True时,文件中的'null'则读物Nan, 此时不能使用  dfoff['Date'] != 'null' 判断,为了对‘null’可以使用 “==”,“!=”,此处设置 keep_default_na=False 。

我们需要得出优惠券与购买的关联数据,以此得出Label。

有以下4中组合:

  有优惠券,购买商品条数
  无优惠券,购买商品条数
  有优惠券,不购买商品条数
  无优惠券,不购买商品条数 代码如下:
print('有优惠券,购买商品条数', dfoff[(dfoff['Date_received'] != 'null') & (dfoff['Date'] != 'null')].shape[0])
print('无优惠券,购买商品条数', dfoff[(dfoff['Date_received'] == 'null') & (dfoff['Date'] != 'null')].shape[0])
print('有优惠券,不购买商品条数', dfoff[(dfoff['Date_received'] != 'null') & (dfoff['Date'] == 'null')].shape[0])
print('无优惠券,不购买商品条数', dfoff[(dfoff['Date_received'] == 'null') & (dfoff['Date'] == 'null')].shape[0])

  文件中有买多少减多少,需要格式化为折扣率,距离门店格式化为数字等

def convertRate(row):
if row == 'null':
return 1.0
elif ':' in row:
rows = row.split(':')
return 1.0 - float(rows[1])/float(rows[0])
else:
return float(row) def getDiscountMan(row):
if ':' in row:
rows = row.split(':')
return int(rows[0])
else:
return 0 def getDiscountJian(row):
if ':' in row:
rows = row.split(':')
return int(rows[1])
else:
return 0 def getWeekday(row):
if row == 'null':
return row
else:
return date(int(row[0:4]), int(row[4:6]), int(row[6:8])).weekday() + 1 def processData(df):
df['discount_rate'] = df['Discount_rate'].apply(convertRate)
df['discount_man'] = df['Discount_rate'].apply(getDiscountMan)
df['discount_jian'] = df['Discount_rate'].apply(getDiscountJian)
df['discount_type'] = df['Discount_rate'].apply(getDiscountType)
print(df['discount_rate'].unique()) df['distance'] = df['Distance'].replace('null', -1).astype(int)
return df

  调用 dfoff = processData(dfoff) 即可格式化以上信息。

注意代码中apply()函数,apply()函数是pandas里面所有函数中自由度最高的函数。该函数如下:

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)

对收到优惠券日期处理:

date_received = dfoff['Date_received'].unique()  #.unique()删除重复项
date_received = sorted(date_received[date_received != 'null'] #排序
print('优惠券收到日期从',date_received[0],'到', date_received[-1]) #输出最小日期和最大日期

同样对于消费日期处理:

date_buy = dfoff['Date'].unique()
date_buy = sorted(date_buy[date_buy != 'null'])
date_buy = sorted(dfoff[dfoff['Date'] != 'null']['Date'])
print('消费日期从', date_buy[0], '到', date_buy[-1])

 

将发放的优惠券与被使用的优惠券画图:

couponbydate = dfoff[dfoff['Date_received'] != 'null'][['Date_received', 'Date']].groupby(['Date_received'], as_index=False).count()
couponbydate.columns = ['Date_received','count']
buybydate = dfoff[(dfoff['Date'] != 'null') & (dfoff['Date_received'] != 'null')][['Date_received', 'Date']].groupby(['Date_received'], as_index=False).count()
buybydate.columns = ['Date_received','count'] sns.set_style('ticks')
sns.set_context("notebook", font_scale= 1.4)
plt.figure(figsize = (12,8))
date_received_dt = pd.to_datetime(date_received, format='%Y%m%d') plt.subplot(211)
plt.bar(date_received_dt, couponbydate['count'], label = 'number of coupon received' )
plt.bar(date_received_dt, buybydate['count'], label = 'number of coupon used')
plt.yscale('log')
plt.ylabel('Count')
plt.legend() plt.subplot(212)
plt.bar(date_received_dt, buybydate['count']/couponbydate['count'])
plt.ylabel('Ratio(coupon used/coupon received)')
plt.tight_layout()
plt.show()

  得到一幅图:

第二部分:特征的选取

第三部分:算法的说明

第四部分:结果分析

第五部分:其他

o2o优惠券使用预测的更多相关文章

  1. 数据挖掘实战 - 天池新人赛o2o优惠券使用预测

    数据挖掘实战 - o2o优惠券使用预测 一.前言 大家好,家人们.今天是2021/12/14号.上次更新是2021/08/29.上篇文章中说到要开两个专题,果不其然我鸽了,这一鸽就是三个多月.今天,我 ...

  2. 天池新人赛-天池新人实战赛o2o优惠券使用预测(一)

    第一次参加天池新人赛,主要目的还是想考察下自己对机器学习上的成果,以及系统化的实现一下所学的东西.看看自己的掌握度如何,能否顺利的完成一个分析工作.为之后的学习奠定基础. 这次成绩并不好,只是把整个机 ...

  3. 2016天池-O2O优惠券使用预测竞赛总结

    第一次参加数据预测竞赛,发现还是挺有意思的.本文中的部分内容参考第一名“诗人都藏在水底”的解决方案. 从数据划分.特征提取.模型设计.模型融合/优化,整个业务流程得到了训练.作为新手在数据划分和模型训 ...

  4. 《阿里云天池大赛赛题解析》——O2O优惠卷预测

    赛事链接:https://tianchi.aliyun.com/competition/entrance/231593/introduction?spm=5176.12281925.0.0.7e157 ...

  5. 天池历届大赛答辩PPT及视频

    1.阿里移动推荐算法: 答辩视频:https://space.dingtalk.com/c/gQHOEnXdXw 2.资金流入流出预测: 答辩视频:https://space.dingtalk.com ...

  6. 悖论当道,模式成空:汽车O2O真是死得其所?

    O2O热潮的兴起似乎来得颇为蹊跷--或许是线上连接线下的模式太过空泛,具有极大的包容性,让各个行业都忍不住在其中横插一脚.在经历过最初的崛起和后来的火爆之后,最终形成目前的寒冬.究其原因,O2O并不是 ...

  7. GBDT 总结文档

    在做阿里的o2o优惠券预测的时候学习了GBDT.听闻GBDT的威力,自然要学习学习. 接下来从以下几个方面记录下我对于GBDT的理解. GBDT的用途,优势 GBDT的结构和算法流程 GBDT如何训练 ...

  8. 当谈 SQL 优化时谈些什么?

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:孙银行 背景 Mysql数据库作为数据持久化的存储系统,在实际业务中应用广泛.在应用也经常会因为SQL遇 ...

  9. 当我们谈 SQL 优化时在谈些什么?

    作者 |孙银行编辑 | 顾乡 背景 Mysql数据库作为数据持久化的存储系统,在实际业务中应用广泛.在应用也经常会因为SQL遇到各种各样的瓶颈.最常用的Mysql引擎是innodb,索引类型是B-Tr ...

随机推荐

  1. Express app.listen 函数了解

    最近一直在学习如何用原生的 Node.js 来做一个网站.在写的同时也在学习 Express 源码. 一直觉得 Express 开启服务器的方法挺有趣的,就看了一下. 在 Express 运行的时候会 ...

  2. mysql 配置 root 远程访问

    来源: https://www.cnblogs.com/24la/p/mariadb-remoting-access.html 首先配置允许访问的用户,采用授权的方式给用户权限 GRANT ALL P ...

  3. mybatis源码-解析配置文件(一)之XML的DOM解析方式

    目录 简介 Java 中 XML 文件解析 解析方式 DOM 解析 XML 新建 XML 文件 DOM 操作相关类 Java 读取 XML 文件 一起学 mybatis @ 简介 在之前的文章< ...

  4. 如何唯一确定一个 Java 类?

    今天偶然想起之前和朋友讨论过的一个问题:如何唯一确定一个 Java 类?我相信大多数朋友遇到这个问题的回答都是:类的全路径呗.但事实上,唯一确定一个 Java 类,单单靠类路径是不够的,还要多加上一个 ...

  5. 3、Docker容器管理

    一.容器创建 1.创建命令 docker  container [root@localhost harbor]# docker container Usage: docker container CO ...

  6. 《Pro SQL Server Internals, 2nd edition》的CHAPTER 2 Tables and Indexes中的Clustered Indexes一节(翻译)

    <Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...

  7. 详细解析 nginx uri 如何匹配 location 规则

    location 是 nginx 配置中出现最频繁的配置项,一个 uri 是如何与多个 location 进行匹配的? 在有多个 location 都匹配的情况下,如何决定使用哪一个 location ...

  8. HyperLedger Fabric 学习思路分享

    HyperLedger Fabric 学习思路分享 HyperLedger Fabric最初是由Digital Asset和IBM公司贡献的.由Linux基金会主办的一个超级账本项目,它是一个目前非常 ...

  9. ubuntu16.04下Hyperledger之搭建Fabric环境简单操作(五步启动e2e_cli)

    如果你已经安装好go等工具.git及checkout相关代及下载相关镜像,您只需下面5步就能up e2e_cli~/go/src/github.com/hyperledger/fabric$ sudo ...

  10. 阿里云ubuntu16.04安装ruby

    0x0 准备 环境:阿里云轻量服务器ubuntu16.04 目的:安装beef需要的ruby环境 更新软件 sudo apt-get update sudo apt-get upgrade sudo ...