前沿:

这是天池的一个新人实战塞题目,原址 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. What's new in XAML of .NET 4.0( .NET 4.0中XAML的新功能 )

    原文 What's new in XAML of .NET 4.0 What's new in XAML of .NET 4.0 Easy Object References with {x:Refe ...

  2. ubuntu 12.04 桌面版关闭图形界面

    对于12.04的ubuntu桌面系统,如果想在开机的时候直接进入字符界面,那可以: 编辑文件 /etc/init/lightdm.conf,在第12行附近,原句“ and runlevel [!06] ...

  3. 理解 NgModelController 中相关方法和属性

    1. 理解$formatters和$parsers方法 angular的双向绑定可以实现view和model中的值自动同步,但有时候我们不想让用户输入的(view值)和发送给后台的(model值)并不 ...

  4. python3获取指定目录内容的详细信息

    不同平台获取指定目录内容的详细信息命令各不相同: Linux中可以通过ls -al获取获取 windows中可以通过dir命令获取 下面是我写的一个通用获取目录内容详细信息的python3脚本: #! ...

  5. Android 实现 WheelView

    wheel view 目录(?)[-] Android WheelView效果图 网上的开源代码 实现思路 扩展Gallery 如何使用 我们都知道,在iOS里面有一种控件------滚筒控件(Whe ...

  6. pthon自动化之路-编写登录接口

    # Author:Lixiang Zoulock = "F:/Users/admin/PycharmProjects/day1/account.txt"account = &quo ...

  7. Frida----安装

    介绍 它是本机应用程序的 Greasemonkey,或者更多技术术语,它是一个动态代码检测工具包.它允许您将JavaScript或您自己的库的片段注入Windows,macOS,GNU / Linux ...

  8. unity ray和line射线检测

    RaycastHit 光线投射碰撞 Struct Structure used to get information back from a raycast. 用来获取从raycast函数中得到的信息 ...

  9. thinkphp在wamp 配置去掉url中index.php方法

    http://blog.csdn.net/youmypig/article/details/45008971

  10. linux gcc编译多个源文件的方法

    http://blog.csdn.net/yinjiabin/article/details/7731817