大数据处理示例(上)
利用Cygwin使用Linux下的数据处理工具,对余额宝的申购赎回数据进行预处理,R语言的时间序列分析。
Cygwin
处理大数据,经常使用Linux系统下的命令来处理,数据库、Excel什么的速度简直太捉急。虽然有GnuWin32提供了一些可以在Windows系统下使用的Linux开源工具,但依然数量有限(比如没有sort),诸多不便。
Cygwin是一个在windows平台上运行的类UNIX模拟环境,是cygnus solutions公司开发的自由软件。在http://www.cygwin.com/上可以下载到32位和64位的软件安装包。国内也有很多镜像提供安装。
Cygwin提供了Linux下很多的package(https://cygwin.com/packages/package_list.html)。并默认安装其中一部分,比如sed、grep、sort、awk等,用cygcheck命令可查看已安装的package。
注意到官方的提示。
Q: Is there a command-line installer?
A: Yes and no. The setup*.exe program understands command-line arguments which allow you to control its behavior and choose individual packages to install. While this provides some functionality similar to such tools as apt-get or yum it is not as full-featured as those packages.
The basic reason for not having a more full-featured package manager is that such a program would need full access to all of Cygwin’s POSIX functionality. That is, however, difficult to provide in a Cygwin-free environment, such as exists on first installation. Additionally, Windows does not easily allow overwriting of in-use executables so installing a new version of the Cygwin DLL while a package manager is using the DLL is problematic.
也就是说如果要安装diff
,你就还得用刚才的软件安装包去下载,详细过程可参看FAQ 2.12 How do I just get everything。
数据预处理
以余额宝的申购赎回预测数据为例。
由
df
命令,可看到Cygwin已经挂载了所有磁盘,直接用cd
切换目录即可。1
$ cd F:/【阿里天池大数据】/资金流入流出预测
用户申购赎回数据为在data/user_balance_table.csv(148M),用
grep
命令从中抽取出用户id为1234着色查看(着色功能特别赞)。
1 | $ grep '^\<1234\>' data/user_balance_table.csv --color=auto |
- 输出用户id为1234并按日期排序后的数据结果。
可以用grep
命令抽取出能够匹配行首为完整的1234('^\<1234\>'
)的行,再根据由逗号分隔(-t,
)的第二列进行排序。1
$ grep '^\<1234\>' data/user_balance_table.csv | sort -k 2 -t, > user1234
也可以用awk
命令,以逗号分隔(-F,
)数据的各列后,输出第一列内容为1234的行,再根据由逗号分隔(-t,
)的第二列进行排序。1
$ awk -F, $1==1234{print $0}' data/user_balance_table.csv | sort -k 2 -t, > user1234
- 通过
more
命令查看user1234(你试试用文本编辑器开user_balance_table.csv,卡得飞起)。
- 由数据的FAQ,某些用户的某些天没有数据,还需另外处理。
18.balance那张表不就是记录的一个用户一天的资金流动总量的情况吗?
每个用户每天汇总的数据。 一个用户,一天一行,包括申购和赎回。 当余额是0,并且没有申购的时候,不记录。
时间序列分析
用R语言对今日总购买量进行时间序列分析。1
2
3
4> setwd('F:/【阿里天池大数据】/资金流入流出预测')
> u<-read.table('user1234',sep=',')
> uts<-ts(u[,5])
> plot.ts(uts)
得到用户id为1234的时间-购买量图。
附赠一张余额宝的时间-收益率图,寡人心痛啊……