如何用R提取div识别图片中的文字app

用R爬虫抓取房价数据_小蚊子数据分析_传送门
用R爬虫抓取房价数据
部分城市的房价迅速上涨,多地出台楼市限制政策。未来房价走势如何?由于影响因素的多样性,目前还没有发现任何一种研究分析能够确切地预测,但我们可以自己动手从网络上抓取房价的最新数据,密切关注最新的房价信息,结合实际情况做出自己的判断。下面数据化分析以链家网为例,利用R爬虫抓取如下图所示的房价数据:(点击查看大图)具体操作步骤如下:首先,用R软件安装并载入RCurl和XML包,RCurl是用R爬虫抓取网络数据的关键程序包,其中的getURL函数能够获取网站的原始数据。其次,查看网站的源代码(浏览网页时点击右键查看源代码),寻找目标数据所在的位置,分析源代码的特征,用XPath的语法把数据元素取出来。例如:http://sh.lianjia.com/ershoufang/d1s7网页中二手房源的相关信息都在...用XPath的语法表示出来就是:path = "//div[@class='list-wrap']"但我们需要更具体的信息,例如抓取下面的title内容:可以用以下代码:library(RCurl)library(XML)i = 1urli = paste("http://sh.lianjia.com/ershoufang/d", i, "s7", sep = "")webi = getURL(urli, .encoding = "utf-8")htmi = htmlParse(webi, encoding = "utf-8")nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='info-panel']//a[@name='selectDetail']")
biaoti = sapply(nodi, function(X) xmlGetAttr(X, "title"))
Encoding(biaoti) = "UTF-8"其他信息都可以类似地抓取出来,适当学习一些XPath和正则表达式的知识。由于链家网的页码在网址中的规律比较简单,所以直接采用for循环来抓取每页的数据,每页20套房源数据,目前总共7.9万套房源,如果想抓取全部数据,需要循环3950次,即循环直到网址变成:http://sh.lianjia.com/ershoufang/d3950s7这样需要的时间会比较长,但突破了网页本身上下翻页链接最多100页的限制。本文只抓取10页数据作为示例。最后,数据化分析把所有抓取到的数据结果存入一个数据框中,方便后续的查询和数据分析。完整的R语言代码如下:library(RCurl)library(XML)res = data.frame()# i是页数,可根据实际情况进行调整for (i in 1:10) {
urli = paste("http://sh.lianjia.com/ershoufang/d", i, "s7", sep = "")
webi = getURL(urli, .encoding = "utf-8")
htmi = htmlParse(webi, encoding = "utf-8")
# 标题、链接
nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='info-panel']//a[@name='selectDetail']")
biaoti = sapply(nodi, function(X) xmlGetAttr(X, "title"))
Encoding(biaoti) = "UTF-8"
lianjie = paste("http://sh.lianjia.com", sapply(nodi, function(X) xmlGetAttr(X, "href")), sep = "")
# 小区、户型、面积
nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='where']//span")
xhmi = sapply(nodi, xmlValue)
xiaoqu = xhmi[(1:length(biaoti))*3 - 2]
huxingi = gsub("\\s+", "", xhmi[(1:length(biaoti))*3 - 1])
mianji = as.numeric(gsub("[^0-9]*$", "", xhmi[(1:length(biaoti))*3 - 0]))
# 区域、地段
nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='other']//a")
qdi = sapply(nodi, xmlValue)
quyu = qdi[(1:length(biaoti))*2 - 1]
diduan = qdi[(1:length(biaoti))*2 - 0]
nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='price']/span[@class='num']")
zongjia = as.numeric(gsub("[^0-9]", "", sapply(nodi, xmlValue)))
nodi = getNodeSet(htmi, path = "//div[@class='list-wrap']//div[@class='price-pre']")
danjia = as.numeric(gsub("[^0-9]", "", sapply(nodi, xmlValue)))
res = rbind(res, data.frame(biaoti, lianjie, xiaoqu, huxingi, mianji, quyu, diduan, zongjia, danjia, stringsAsFactors = FALSE))
Sys.sleep(1)}via:数据化分析体系、简单、实用的数据分析视频课程:(1)数据分析入门必备课程(9折限时优惠)http://study.163.com/series/47001.htm(2)R数据分析与数据挖掘实战(9折限时优惠)http://study.163.com/series/.htm(3)Python数据分析与数据挖掘实战(9折限时优惠)http://study.163.com/series/.htm注:由于WX限制,以上链接需要手动复制至浏览器粘贴打开,其他课程可点击“阅读原文”查看。108分享收藏文章被以下专栏收录聊聊数据分析,大数据,机器学习,深度学习,人工智能&figure&&img src=&https://pic3.zhimg.com/v2-7f269445aee41a97ec034be9d76477a3_b.jpg& data-rawwidth=&1113& data-rawheight=&435& class=&origin_image zh-lightbox-thumb& width=&1113& data-original=&https://pic3.zhimg.com/v2-7f269445aee41a97ec034be9d76477a3_r.jpg&&&/figure&&p&今天跟大家介绍一款任坤大神写的新包——formattable。&/p&&p&这个包的功能很简单,但是却很具创意性,它颠覆了R语言data.frame数据表的呈现方式,允许在表格内自定义视觉化元素,比如对某一列数据进行字号、颜色、背景、以及图形化处理,整体的版式仍然保留表格的样式,但是已经具有了表和图结合的意味。&/p&&p&关于数据框的呈现方式,R语言内目前较好的自定义呈现方式是谢益辉大神的DT包,可以 将静态表格动态化,进行切片、索引、排序操作。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&devtools::install_github(&renkun-ken/formattable&)
install.packages(&formattable&)
library(&formattable&)
library(&DT&)
&/code&&/pre&&/div&&p&自定义百分比显示格式。&/p&&p&接触过R语言的都知道R中没有数值形式的百分比,只有浮点型,如果要在数据框中自定义某一列为百分比,则需要使用文本拼接函数将其格式化,但是这样格式化之后,该列便会失去数值格式,转换为字符型变量,无法参数数学运算。&/p&&p&但是formattable包通过扩展内部运算逻辑,不仅可以非常方便的将某一浮点型列自定义为百分比,而且保留其数学运算属性。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&p &- percent(c(0.1, 0.02, 0.03, 0.12))
[1] 10.00% 2.00%
p + 0.01[1] 11.00% 3.00%
[1] 12.00%
[1] &formattable& &numeric&
&/code&&/pre&&/div&&p&通过查看其S3类发现,formattable格式继承了numeric属性,因而保留了数学运算能力。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&balance &- accounting(c(, 200, -150, 0, 1200))
[1] 1,000.00 500.00
(150.00) 0.00
balance + 1000
[1] 2,000.00 1,500.00 1,200.00 850.00
1,000.00 2,200.00
&/code&&/pre&&/div&&p&以上accounting函数可以将输出向量自定义为会计上使用的数字合适,三维间隔,同时负值加括号。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&class(balance)
[1] &formattable& &numeric&
&/code&&/pre&&/div&&p&其核心实现方式仍然是通过将其底层S3类继承numeric来实现保留数学运算功能。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&p &- data.frame(
id = c(1, 2, 3, 4, 5),
name = c(&A1&, &A2&, &B1&, &B2&, &C1&),
balance = accounting(c(5, 2, 7600), format = &d&),
growth = percent(c(0.3, 0.3, 0.1, 0.15, 0.15), format = &d&),
ready = formattable(c(TRUE, TRUE, FALSE, FALSE, TRUE), &yes&, &no&))
id name balance growth ready
&/code&&/pre&&/div&&p&在数据框中这些特性也依然能够保留(这是自然地,因为数据框就是由若干个等长的向量组成的)&/p&&p&但是这仅仅是formattable很基础的功能部分,下面给大家展示它的杀手锏——针对数据框表格的超强自定义可视化能力。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&df &- data.frame(
id = 1:10,
name = c(&Bob&, &Ashley&, &James&, &David&, &Jenny&,
&Hans&, &Leo&, &John&, &Emily&, &Lee&),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c(&C&, &A&, &A&, &C&, &B&, &B&, &B&, &A&, &C&, &C&),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-7a0c1def419ab56c4f432bee_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&928& data-rawheight=&458& class=&origin_image zh-lightbox-thumb& width=&928& data-original=&https://pic3.zhimg.com/v2-7a0c1def419ab56c4f432bee_r.jpg&&&/figure&&p&&br&&/p&&p&以上是使用普通的输出打印出的数据框呈现方式,那么通过formattable函数格式化之后的数据表格又长什么样呢,人让我们拭目以待。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&formattable(df,
age = color_tile(&white&, &orange&),
grade = formatter(
style = x ~ ifelse(x == &A&, style(color = &green&, font.weight = &bold&), NA)
area(col = c(test1_score, test2_score)) ~ normalize_bar(&pink&, 0.2),
final_score = formatter(
style = x ~ style(color = ifelse(rank(-x) &= 3, &green&, &gray&)),
x ~ sprintf(&%.2f (rank: %02d)&, x, rank(-x))
registered = formatter(
style = x ~ style(color = ifelse(x, &green&, &red&)),
x ~ icontext(ifelse(x, &ok&, &remove&), ifelse(x, &Yes&, &No&))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-07b4d411a62fbf898dc9e5b6_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1080& data-rawheight=&422& class=&origin_image zh-lightbox-thumb& width=&1080& data-original=&https://pic3.zhimg.com/v2-07b4d411a62fbf898dc9e5b6_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&是不是很神奇呀,仔细观察以上表格中,一共使用了三种自定义可视化类型,分别是字体大小和颜色自定义、字体背景自定义、以及文本自定义。&/p&&p&color_tile函数用于输出按照数值量级进行颜色背景填充的列。&/p&&p&formatter函数提供字体显示格式的自定义,grade列自定义了值为A的记录显示绿色,并将字体加粗,否则忽略。&br&test1_score, test2_score两列通过area函数在对应字体背景位置使用条形图来代表指标量级大小,颜色填充粉色。&br&final_score列对指标按照top3显示绿色,其余显示灰色,同时将内容显示格式自定义为浮点型+(rank:名次)进行显示。&br&registered列则在对填充颜色按照对应布尔值进行显示(TRUE显示绿色、FALSE显示红色)之外,在左侧添加了对用的icon文本(TRUE显示绿色对号,FALSE显示红色叉号)。&/p&&p&是不是很神奇呀,一个小小的包竟然可以做这么有趣的事情,这种表格在财务数据、营销数据或者绩效数据表中是经常会用到的可视化形式,简单明了,非常醒目。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&DT::datatable(df)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-ebbc504a146e9_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1080& data-rawheight=&796& class=&origin_image zh-lightbox-thumb& width=&1080& data-original=&https://pic4.zhimg.com/v2-ebbc504a146e9_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&而DT的datatable函数,则仅仅是通过动态交互的形式提供了一个可交互的表格UI环境。&/p&&p&本项目主页地址:&/p&&a href=&https://link.zhihu.com/?target=https%3A//github.com/renkun-ken/formattable& data-draft-node=&block& data-draft-type=&link-card& data-image=&https://pic1.zhimg.com/v2-867f72eb6ad_ipico.jpg& data-image-width=&337& data-image-height=&337& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&renkun-ken/formattable&/a&&p&&/p&&p&&/p&
今天跟大家介绍一款任坤大神写的新包——formattable。这个包的功能很简单,但是却很具创意性,它颠覆了R语言data.frame数据表的呈现方式,允许在表格内自定义视觉化元素,比如对某一列数据进行字号、颜色、背景、以及图形化处理,整体的版式仍然保留表格的…
&figure&&img src=&https://pic4.zhimg.com/v2-faf90070eab7fe672ba97a98eba159bc_b.jpg& data-rawwidth=&1052& data-rawheight=&584& class=&origin_image zh-lightbox-thumb& width=&1052& data-original=&https://pic4.zhimg.com/v2-faf90070eab7fe672ba97a98eba159bc_r.jpg&&&/figure&&p&12月的第一天,祝所有小伙伴儿的12月都能够被温柔以待。&br&&/p&&p&能在学校悠哉写推送的日子所剩不多了,为了珍惜剩下所剩不多的推送机会,打算12月写一些实践性强一些的内容,比如数据库(包括关系型的和noSQL)。&/p&&p&前段时间一直在探索数据抓取的内容,那么现在问题来了,抓完数据如何存储呢?&/p&&p&保存成本地文件是一种方案,但是借助关系型数据库或者noSQL数据库,我们可以给自己获取的数据提供一个更为理想的安身之所。&/p&&p&今天这一篇粗浅的聊一聊非结构化数据存储,以及R语言和Python与mongoDB之间的通讯。&/p&&p&写这一篇是因为之前在写web数据抓取的时候,涉及大量的json数据,当然我们可以直接将json转换为R语言(dataframe/list)或者Python(dict/DataFrame)中的内置数据对象,但是保存原始数据往往也很重要,即便是list或者dict,如果不能转化为关系型表格,通常也需要在本地保存成json格式的数据源。&/p&&p&那么通过mongoDB这种专业的noSQL数据库来保存非结构化数据,可以完成批量保存、批量读取、条件查询和更新,这样可以集中维护,显得更具有安全性、便利性、专业性。&/p&&p&mongo数据库的数据对象是bson,这种数据结构相当于json标准的扩展,R语言中的list可以与json互转,Python中的dict本身就与json高度兼容。&/p&&p&R语言&/p&&p&在R语言中,通常通过rmongodb包来进行非结构化数据存储。(当然有替代的包,只是这个包资料相对较多一些!)&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&###下载:
devtools::install_github(&mongosoup/rmongodb&)
library(&rmongodb&)
&/code&&/pre&&/div&&p&创建/断开连接&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mongo &- mongo.create(host = &localhost&)
mongo.is.connected(mongo)
#检查是否连接成功
mongo.destroy(mongo)
&/code&&/pre&&/div&&p&关于如何在系统中启动mongodb服务,网络上有很多此类教程,照葫芦画瓢就好,如果你想使用一个类似MySQL的navicat那样的可视化操作界面,可以考虑安装Robo可视化界面,这样基本就可以手动操作mongodb中的数据对象了。&/p&&p&mongodb中的数据对象,与MySQL中的数据对象略有不同,不过从层级上来看,仍然是分成数据库 》集合(表) 》key-value. &/p&&p&一个数据库中可以有很多个集合(相当于表),每一个集合中又包含很多的documents结构。每一个documents作为一条记录,相当于SQL中的一行,而documents内是键值对结构,且允许包含嵌套结构。一个documents对象内嵌套的同一层级key-value对象,被称为fileds,可以近似理解为SQL中的column。&/p&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-21deafaadf701_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&390& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-21deafaadf701_r.jpg&&&/figure&&p&&br&&/p&&p&mongodb的数据对象叫做bson,是Binary JSON Serialization的缩写简称,关于详细的json和bson的概念及其内含关系,可以查阅相关资料,或者通过W3C网站了解。&/p&&p&接下来进入R语言与mongodb链接的操作讲解。&/p&&p&以上已经建立了一个名为mongo的链接(mongo.is.connected结果可以用于测试连接是否成功!)。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&###查看本地数据库文件
mongo.get.databases(mongo) #查看本地数据库名称
mongo.get.database.collections(mongo, db = &pymongo_test&)
#查看pymongo_test数据库内的各个集合名称
mongo.count(mongo, ns = &pymongo_test&)
#查看pymongo_test数据库内的集合数量
mongo.rename(mongo, &pymongo_test.posts&, &pymongo_test.post&)
#修改pymongo_test数据库内posts表名称
&/code&&/pre&&/div&&p&删除操作&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mongo.drop.database(mongo, db = &database&)
#移除数据库及其内部所有集合
mongo.drop(mongo,
ns = &database.collection&)
#仅删除数据库内全部集合(collection)
mongo.drop(mongo,
ns = &rmongo_test.mydata1&)
#移除数据集合内的某一特定表
mongo.remove(mongo, ns, criteria = mongo.bson.empty())
#移除集合内选定条件的记录
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-5e846a9f48a5abdd7beacda1_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&390& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic1.zhimg.com/v2-5e846a9f48a5abdd7beacda1_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&其中ns是命名空间参数,格式为“数据库名称.集合名称”。&/p&&p&rmongodb内没有专门创建数据库或者在数据库中创建集合的函数,想要创建的话仅需在插入数据时指定一个不存在的ns参数即可。&/p&&p&R语言中的非结构化数据对象是list,因为list结构与json或者bson差别比较大,在插入mongo之前需要使用特定函数进行list/json与bson之间的相互转化。&/p&&p&涉及转化的函数有两个:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mongo.bson.from.JSON
#将json对象转换为mongodb中的bson对象。
mongo.bson.from.list
#将list对象转换为mongodb中的bson对象。
&/code&&/pre&&/div&&p&使用json格式数据插入mongo&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#新建一个json对象
json &- '{&A&:1,&B&:2,&C&:{&D&:3,&E&:4}}'
[1] &{\&A\&:1,\&B\&:2,\&C\&:{\&D\&:3,\&E\&:4}}&
#如果你不想手写json,也可以使用jsonlite包中的toJSON函数(一定记得anto_unbox设置为RUE)
json &- jsonlite::toJSON(list(&A&=1,&B&=2,&C&=list(&D&=3,&E&=4)),auto_unbox = TRUE)
{&A&:1,&B&:2,&C&:{&D&:3,&E&:4}}
#注:使用jsonlite::toJSON函数将一个list转为一个json字符串,这个字符串拥有一个名为json的类,
但是并未改变其内容,仅仅是添加了一个类,同时输出的外观优化了下。所以以上两种list转json的方法等价。
#将json对象转换为mongodb可识别的bson对象:
bson &- mongo.bson.from.JSON(json)
#转化为basn后的数据结构内容未变,但是出现了树状层级结构。
&/code&&/pre&&/div&&p&插入mongo(注意这里的rmongo_test.mydata是数据库名+“.”+表名,而且数据库名和表明都是不存在的,这样会自动创建新数据库及表)&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mongo.get.databases(mongo)
[1] &pymongo_test&
mongo.insert(mongo,ns=&rmongo_test.mydata&,bson)
mongo.get.databases(mongo)
[1] &pymongo_test& &rmongo_test&
&/code&&/pre&&/div&&p&使用list结构插入mongodb与使用json格式步骤差不多,不同的是要使用list转bson的转化函数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list &- list(a=2, b=3, c=list(d=4, e=5))
bson &- mongo.bson.from.list(list)
mongo.insert(mongo,&rmongo_test.mydata&,bson)
#使用之前的数据库+表名会将本次插入的记录添加到mydata已经存在的记录后面
mongo.insert(mongo,&rmongo_test.mydata1&,bson)
#换一个表名则会在rmongo_test数据库中新建一个表
mongo.drop(mongo,
ns = &rmongo_test.mydata1&)
#移除数据集合内的某一特定表(删掉刚才新插的mydata1)
&/code&&/pre&&/div&&b&&figure&&img src=&https://pic4.zhimg.com/v2-b10f48d6c9a6b31f7ef38_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&389& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-b10f48d6c9a6b31f7ef38_r.jpg&&&/figure&&/b&&p&&br&&/p&&h2&&b&数据查询&/b&&/h2&&p&查询其中一条记录(第一条),使用mongo.find.one函数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&tmp &- mongo.find.one(mongo, ns = &rmongo_test.mydata&)
5a1b6eb611cb7
&/code&&/pre&&/div&&p&tmp对象是一个bson结构,需要转化为list才能得到内置数据结构。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&tmp &- mongo.bson.to.list(tmp)
{ $oid : &5a1b6eb611cb7& }
&/code&&/pre&&/div&&p&查询表中所有记录要使用mongo.find.all函数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&find_all &- mongo.find.all(mongo, ns = &pymongo_test.post&)
#find_all直接是将post内的bson对象转化为一个list,很奇怪,
#为啥mongo.find.one输出的是一个bson,需要使用函数转为list,不是很理解设计的原因。
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-90d052fc4e0caa30e051c3_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&2471& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic1.zhimg.com/v2-90d052fc4e0caa30e051c3_r.jpg&&&/figure&&p&&br&&/p&&p&mongo.find函数可以支持条件查询:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#创建索引条件:
buf &- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, name=&gender&, value=&male&)
query &- mongo.bson.from.buffer(buf)
构造查询:
cursor &- mongo.find(mongo,&pymongo_test.post&,query)
[1] &mongo.cursor&
cursor对象类似SQL中的一个游标对象,不能直接查看内部结构,需要借助迭代函数进行输出
while (mongo.cursor.next(cursor))
#判断是否还有剩余迭代次数
print(mongo.cursor.value(cursor))
#打印当前迭代记录
mongo.cursor.destroy(cursor)
#关闭查询游标
5a10a8c4896a
gender : 2
5a10a8c4896b
gender : 2
5a10a8c4896c
gender : 2
&/code&&/pre&&/div&&p&所以也可以把cursor当成是一个迭代器,想要提取里面的查询数据,需要构造循环与迭代函数,自行提取,而mongo.find.one函数和mongo.find.all函数相当于两个快捷函数,直接提取符合条件的记录或者所有记录。&/p&&p&rmongosb的mongo.find函数可以支持mongodb原生的复杂查询,支持很多高级符号函数,这一点儿我暂未深入了解,留待以后再做探讨。如果你想要详细的了解mongodb的用法, 最好参考关于mongodb的专业操作书,rmongodb内的函数与mongodb的原生函数相比,还有很多地方不完善,无法支持,不过对于平时的数据存储而言最够了,用的最频繁的就是插入、读取操作了。&/p&&p&&b&Python:&/b&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&from pymongo import MongoClient,ASCENDING, DESCENDING
import pymongo,json
&/code&&/pre&&/div&&p&之前说到过,因为Python中的dict与json高度兼容(并不代表一模一样),而bson结构又是基于json的扩展,所以在Python中可以直接将dict插入mongodb数据库,而基本无需做类型转换,这一点儿Python完胜R语言。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#连接MongDB:
client = MongoClient()
client = MongoClient(host='localhost',port= 27017)
client = MongoClient('mongodb://localhost:27017')
&/code&&/pre&&/div&&p&以上三种连接方法等价。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#连接数据库:
db = client.pymongo_test
db = client['pymongo_test']
&/code&&/pre&&/div&&p&以上两句等价,用于连接数据库,与Python中访问属性的操作相同。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#指定集合(相当于SQL中的table)
collection = db.post
collection = db['post']
&/code&&/pre&&/div&&p&以上两句等价,db的基础上连接mongodb中的集合(相当于表)。&/p&&p&使用本地的json数据,创建一个带插入的临时dict结构:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mydata = json.load(open(&D:/R/File/indy.json&))
mydata = mydata['indy movies']
mydata1 = mydata[1];mydata1
mydata2 = mydata[-2:];mydata2
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-f355bd7f13fedf49a4bea_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&498& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic1.zhimg.com/v2-f355bd7f13fedf49a4bea_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&为了防止数据混乱,现将之前在R语言中添加的表记录删除:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&collection.remove({})
collection.insert_one(mydata1)
results = collection.find_one()
{'_id': ObjectId('5a10a8c4896f'), 'academy_award_ve': True, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'budget': , 'name': 'Indiana Jones and the Temple of Doom', 'producers': ['Robert Watts'], 'year': 1984}
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&p&当然也可以一次插入多条记录,不过将记录构造成一个列表即可。 &/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&type(mydata2)
collection.remove({})
collection.insert_many(mydata2)
for item in collection.find():
print(item)
{'_id': ObjectId('5a10a8c4896f'), 'name': 'Indiana Jones and the Temple of Doom', 'year': 1984, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'producers': ['Robert Watts'], 'budget': , 'academy_award_ve': True}
{'_id': ObjectId('5a10a8c48970'), 'name': 'Indiana Jones and the Last Crusade', 'year': 1989, 'actors': {'Indiana Jones': 'Harrison Ford', 'Walter Donovan': 'Julian Glover'}, 'producers': ['Robert Watts', 'George Lucas'], 'budget': , 'academy_award_ve': False}
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-4a2ac0b10bbf4da04f42a1d73b90640e_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&396& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-4a2ac0b10bbf4da04f42a1d73b90640e_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&查询函数可以直接提供给for循环进行记录的遍历。&/p&&p&mangodb不允许插入重复记录,还有一些保留字符要注意。(比如英文句点“.”)&/p&&p&查询则提供了更为丰富的函数及可选参数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#查询一条记录:
results = collection.find_one({'budget': })
{'_id': ObjectId('5a10a8c4896f'), 'academy_award_ve': True, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'budget': , 'name': 'Indiana Jones and the Temple of Doom', 'producers': ['Robert Watts'], 'year': 1984}
&/code&&/pre&&/div&&p&条件查询:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&results = collection.find({'year': 1984})
for result in results:
print(result)
{'_id': ObjectId('5a10a8c4896f'), 'name': 'Indiana Jones and the Temple of Doom', 'year': 1984, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'producers': ['Robert Watts'], 'budget': , 'academy_award_ve': True}
&/code&&/pre&&/div&&p&查询条件支持符号函数以及正则表达式:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&results = collection.find({'budget': {'$gt':}})
#budget大于的记录
for result in results:
print(result)
{'_id': ObjectId('5a10a8c48970'), 'name': 'Indiana Jones and the Last Crusade', 'year': 1989, 'actors': {'Indiana Jones': 'Harrison Ford', 'Walter Donovan': 'Julian Glover'}, 'producers': ['Robert Watts', 'George Lucas'], 'budget': , 'academy_award_ve': False}
&/code&&/pre&&/div&&p&布尔条件查询:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&results = collection.find({'academy_award_ve': True})
for result in results:
print(result)
{'_id': ObjectId('5a10a8c4896f'), 'name': 'Indiana Jones and the Temple of Doom', 'year': 1984, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'producers': ['Robert Watts'], 'budget': , 'academy_award_ve': True}
&/code&&/pre&&/div&&p&正则表达式查询:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&results = collection.find({'name': {'$regex': 'Doom$'}})
for result in results:
print(result)
{'_id': ObjectId('5a10a8c4896f'), 'name': 'Indiana Jones and the Temple of Doom', 'year': 1984, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'producers': ['Robert Watts'], 'budget': , 'academy_award_ve': True}
&/code&&/pre&&/div&&p&更新操作:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&student = collection.find_one({'year':1984})
{'_id': ObjectId('5a10a8c4896f'), 'academy_award_ve': True, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'budget': , 'name': 'Indiana Jones and the Temple of Doom', 'producers': ['Robert Watts'], 'year': 1984}
collection.update_one({&year&: 1984}, {&$set&: {&name&: &Indiana Jones and Doom&}})
{'_id': ObjectId('5a10a8c4896f'), 'academy_award_ve': True, 'actors': {'Indiana Jones': 'Harrison Ford', 'Mola Ram': 'Amish Puri'}, 'budget': , 'name': 'Indiana Jones and Doom', 'producers': ['Robert Watts'], 'year': 1984}
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-b119e347baffcf43dde016_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&391& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-b119e347baffcf43dde016_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&删除操作:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&result = collection.delete_one({'name': 'Indiana Jones and Doom'})
data1 = collection.find()
for result in data1:
print(result)
{'_id': ObjectId('5a10a8c48970'), 'name': 'Indiana Jones and the Last Crusade', 'year': 1989, 'actors': {'Indiana Jones': 'Harrison Ford', 'Walter Donovan': 'Julian Glover'}, 'producers': ['Robert Watts', 'George Lucas'], 'budget': , 'academy_award_ve': False}
&/code&&/pre&&/div&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-1b9ebe49bbf7aaa07bea3c_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&391& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-1b9ebe49bbf7aaa07bea3c_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-b183a93b2fcf6_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&720& data-rawheight=&356& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-b183a93b2fcf6_r.jpg&&&/figure&&p&&br&&/p&&p&&br&&/p&&p&删除之后只剩一个记录了。&/p&&p&Python支持的符号运算符还有很多!&/p&&p&符号含义示例&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&{'age': {'$lt': 20}}
{'age': {'$gt': 20}}
{'age': {'$lte': 20}}
#$lte小于等于
{'age': {'$gte': 20}}
#$gte大于等于
{'age': {'$ne': 20}}
#$ne不等于
{'age': {'$in': [20, 23]}}
#$in在范围内
{'age': {'$nin': [20, 23]}}
#$nin不在范围内
&/code&&/pre&&/div&&p&正则表达式含义:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&{'name': {'$regex': '^M.*'}}
#$regex,name以M开头
{'name': {'$exists': True}}
#$exists,name属性存在
{'age': {'$type': 'int'}}
#$type,age的类型为int
{'age': {'$mod': [5,0]}}
#$mod数字模操作,年龄模5余0
{'$text': {'$search': 'Mike'}}
#$text文本查询,text类型的属性中包含Mike字符串
{'$where': 'obj.fans_count == obj.follows_count'}#$where高级条件查询,自身粉丝数等于关注数
&/code&&/pre&&/div&&p&这些运算符号以及正则表达式可以用在查询、更新、删除等所有操作上。&/p&&p&最后吐槽一句,R语言的rmongodb包的查询函数实在是太麻烦了,很难用,Pymongo的函数设计就很友好。&/p&&p&以上便是R语言、Python与mongodb数据库通讯的基础操作,如果想要了解更为详细的高阶查询操作,可以参考关于mongodb的专业技术书籍及资料。&/p&&p&&b&参考资料:&/b& &/p&&p&&a href=&https://link.zhihu.com/?target=https%3A//docs.mongodb.com/manual/reference/operator/query/& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&docs.mongodb.com/manual&/span&&span class=&invisible&&/reference/operator/query/&/span&&span class=&ellipsis&&&/span&&/a&&br&&a href=&https://link.zhihu.com/?target=http%3A//api.mongodb.com/python/current/api/pymongo/collection.html& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&http://&/span&&span class=&visible&&api.mongodb.com/python/&/span&&span class=&invisible&&current/api/pymongo/collection.html&/span&&span class=&ellipsis&&&/span&&/a&&br&&a href=&https://link.zhihu.com/?target=http%3A//api.mongodb.com/python/current/api/pymongo/& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&http://&/span&&span class=&visible&&api.mongodb.com/python/&/span&&span class=&invisible&&current/api/pymongo/&/span&&span class=&ellipsis&&&/span&&/a& &/p&&p&&br&&/p&&p&在线课程请点击文末原文链接:&/p&&p&&a href=&https://link.zhihu.com/?target=https%3A//edu.hellobi.com/course/195/lessons& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Hellobi Live | R语言可视化在商务场景中的应用&/a&&br&往期案例数据请移步本人GitHub:&br&&a href=&https://link.zhihu.com/?target=https%3A//github.com/ljtyduyu/DataWarehouse/tree/master/File& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&github.com/ljtyduyu/Dat&/span&&span class=&invisible&&aWarehouse/tree/master/File&/span&&span class=&ellipsis&&&/span&&/a&&/p&
12月的第一天,祝所有小伙伴儿的12月都能够被温柔以待。 能在学校悠哉写推送的日子所剩不多了,为了珍惜剩下所剩不多的推送机会,打算12月写一些实践性强一些的内容,比如数据库(包括关系型的和noSQL)。前段时间一直在探索数据抓取的内容,那么现在问题来…
&figure&&img src=&https://pic1.zhimg.com/v2-b95f5006bd2c_b.jpg& data-rawwidth=&1053& data-rawheight=&586& class=&origin_image zh-lightbox-thumb& width=&1053& data-original=&https://pic1.zhimg.com/v2-b95f5006bd2c_r.jpg&&&/figure&&p&list是R语言中包容性最强的数据对象,几乎可以容乃所有的其他数据类型。&/p&&p&但是包容性最强也也意味着他对于内部子对象的类型限制最少,甚至内部可以存在递归结构,这样给我们提取数据带来了很大的困难。&/p&&p&如果你对R语言的list结构非常熟悉,又熟练控制流等函数的操作,自然可以通过构建循环来完成目标数据的提取。但是在数据量大、结构及其复杂的情形下,自建循环无论是性能还是代码量上都很不经济。&/p&&p&好在确实有开发者在针对list数据结构进行操作上的优化,任坤老师的大作——rlist就是一个强大的list解析神器,它可以让我们像在dplyr、data.table操作data.frame一样,使用rlist轻松的实现对list数据类型的map(映射)、filter(筛选)、update(更新)、group(分组)、sort(排序)。&/p&&p&任坤老师的主页提供了很好地rlist实践方案,同时该包配套有非常详细的document,是你数据清洗工具箱中不可多得的list操纵神奇,配合tidyverse工具箱,你的数据warpping技能一定会得到大大扩展与提升。&/p&&p&在R语言环境中,我们最常遇到的list操作场景大概有以下三类(当然不含全部):&/p&&p&&b&1、统计模型的输出结果:&/b&&/p&&p&因为统计模型在跑完之后,通过会输出一系列各种指标,比如及置信区间、判定指标和拟合值等,这些对象因为大小和长度不等,类型不一但是又必须输出,所以只能交给list这个容器来盛。&/p&&p&&b&2、地理信息数据源:&/b&&/p&&p&无论是基于s3标准的sp空间数据结构,还是基于s4标准的sf空间数据结构,都容纳着大量的list对象。不过这些数据结构因为用途比较特殊,都有对应包来进行结构化处理(我们无须担心),rgdal可以很好的识别sp对象,sf包可以高效处理sf对象。&/p&&p&&b&3、基于web的api访问返回的json数据包:&/b&&/p&&p&这种情形,尝试过网络数据抓取的小伙伴可能会频繁遇到,虽然这样省去了解析html/xml的麻烦,但是倘若原始的json内部结构比较复杂,解析起来非常麻烦。当然已经有好几个成熟的json结构包来进行json与R内置数据类型的转化,但是除非结构非常规整,否则仍然严重依赖lsit处理。&/p&&p&这一篇就以网络上获取到的json数据结构为例进行演示,当然rlist包内置函数数量非常庞大,一篇根本不足以涵盖所有的,仅以几个高频应用函数为例。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&library(&rlist&)
library(&pipeR&)
&/code&&/pre&&/div&&p&&b&第一类是I/O函数,也就是读写list结构。&/b&&/p&&p&如果本地有一个非关系型的json数据文件,可以用list.load无障碍加载(保存)。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&###加载json文件(相当于jsonlite包中的fromJSON函数)
mydata&-list.load(&E:/git/DataWarehouse/File/indy.json&)
###保存list数据为json文件(相当于jsonlite包中的toJSON函数)
list.save(mydata,&E:/git/DataWarehouse/File/indy.json&)
&/code&&/pre&&/div&&p&&b&第二类是映射与筛选函数:&/b&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&mydata&-mydata
%&&% `[[`(1)
###%&&%是 一个与magrittr包中的%&&%函数功能一样的管道操作函数,
###任坤大大推荐在使用rlist的时候搭配pipR中的%&&%一起使用。
###(当然使用%&%也是可以的)。
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-ea93d7bc3a_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&498& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-ea93d7bc3a_r.jpg&&&/figure&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&###查看mydata的数据结构
class(mydata)
[1] &list&
str(mydata)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-1aeb6c8fe61bdd0651c69d_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&397& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-1aeb6c8fe61bdd0651c69d_r.jpg&&&/figure&&p&&br&&/p&&p&可以看到mydata的含有3个子list,每一个子list中有包含6个子对象。&/p&&p&mydara&/p&&ul&&li&name&br&year&br&actors&br&producers&br&budget&br&academy_award_ve&/li&&/ul&&p&&br&&/p&&p&&b&映射(mapping)&/b&&br&list.map函数提供了list中元素的映射功能。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&###将mydata的子list映射到name变量:
list.map(mydata,name)
[1] &Raiders of the Lost Ark&
[1] &Indiana Jones and the Temple of Doom&
[1] &Indiana Jones and the Last Crusade&
###将每个元素映射到制片人的数量:
list.map(mydata,length(producers))
&/code&&/pre&&/div&&p&&b&索引函数:list.select&/b&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list.select(mydata,name,year)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-c66bddf4fbb343efb4d96_b.jpg& data-caption=&& data-rawwidth=&655& data-rawheight=&494& class=&origin_image zh-lightbox-thumb& width=&655& data-original=&https://pic1.zhimg.com/v2-c66bddf4fbb343efb4d96_r.jpg&&&/figure&&p&&br&&/p&&p&虽然索引函数能够达到的目的与list.map类似,但是两者设计的初衷是不一样的,list.select可以完全类比dplyr中的select函数。&/p&&p&&b&筛选(filtering)&/b&&br&筛选出上映年份在82年以后的影片:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.filter(mydata, year&=1982))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-f1ec5e4dec6ac_b.jpg& data-caption=&& data-rawwidth=&640& data-rawheight=&375& class=&origin_image zh-lightbox-thumb& width=&640& data-original=&https://pic3.zhimg.com/v2-f1ec5e4dec6ac_r.jpg&&&/figure&&p&&br&&/p&&p&筛选出了两部上映年份为84年和89年的影片信息。&/p&&p&筛选出制片人中含有Robert Watts的电影记录:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.filter(mydata, &Robert Watts& %in% producers))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-51eeedd77f2c36b85bcd794b4d9cb421_b.jpg& data-caption=&& data-rawwidth=&703& data-rawheight=&403& class=&origin_image zh-lightbox-thumb& width=&703& data-original=&https://pic1.zhimg.com/v2-51eeedd77f2c36b85bcd794b4d9cb421_r.jpg&&&/figure&&p&&br&&/p&&p&&b&分组(grouping)&/b&&br&按照年份做互斥分组:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.group(mydata, year))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-cf1378dfc522d3bc813c468b061be078_b.jpg& data-caption=&& data-rawwidth=&640& data-rawheight=&424& class=&origin_image zh-lightbox-thumb& width=&640& data-original=&https://pic4.zhimg.com/v2-cf1378dfc522d3bc813c468b061be078_r.jpg&&&/figure&&p&&br&&/p&&p&可以看到,当使用年份进行分组时,相当于又给mydata做了一次父级标签分类(类别即为我们选定的分组变量——年份)。&/p&&p&按照获奖与否做互斥分组:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.group(mydata, academy_award_ve))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-82dd66f88cbac50f2af7_b.jpg& data-caption=&& data-rawwidth=&640& data-rawheight=&407& class=&origin_image zh-lightbox-thumb& width=&640& data-original=&https://pic4.zhimg.com/v2-82dd66f88cbac50f2af7_r.jpg&&&/figure&&p&&br&&/p&&p&因为有两部电影获奖,一部没有获奖,最终输出结果即为按照获奖与否将mydata从新分成两组,组别标签即为是否获奖。&/p&&p&&b&排序(sorting)&/b&&/p&&p&按照年份升序排列:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.sort(mydata,year))
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic2.zhimg.com/v2-3c4fe51b0df92702badd6ad8c033a08c_b.jpg& data-caption=&& data-rawwidth=&640& data-rawheight=&405& class=&origin_image zh-lightbox-thumb& width=&640& data-original=&https://pic2.zhimg.com/v2-3c4fe51b0df92702badd6ad8c033a08c_r.jpg&&&/figure&&p&&br&&/p&&p&按照预算金额排序:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&str(list.sort(mydata,(budget)))
#默认升序,加圆括号代表降序(多么清新脱俗到的设定呀)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-faa348d794be077fefbb8_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&453& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-faa348d794be077fefbb8_r.jpg&&&/figure&&p&&br&&/p&&p&&b&第三类是合并与重塑函数:&/b&&/p&&p&就是如何将list在vector与data.frame之间进行转化。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list.stack
#按行进行堆栈
list.rbind
#这个与list.stack函数类似,也可以达到相同的效果
list.cbind
list.flatten
#将多层嵌套的递归结构转换为单层结构
&/code&&/pre&&/div&&p&&b&list.stack&/b&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list.update(mydata,actors=NULL, producers=NULL) %&&% list.stack
#list.stack函数虽然也类似堆栈操作,但是它眼球要求更为严格,
#必须保持内部机构一致和list长度一致,为了达到这个目的,
#我们使用list.update函数现将存在递归和长度不等两个变量删除,
#然后使用list.rbind函数进行操作。
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-18dfa275ea2fbed8eacecd9f57b89732_b.jpg& data-caption=&& data-rawwidth=&640& data-rawheight=&135& class=&origin_image zh-lightbox-thumb& width=&640& data-original=&https://pic3.zhimg.com/v2-18dfa275ea2fbed8eacecd9f57b89732_r.jpg&&&/figure&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list.rbind(mydata)
#list.stack完成的效果就是将每一个子list按照names进行纵向堆积。
#(允许内部存在递归结构)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-1fce03eb74d5dbab95bb21_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&117& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic4.zhimg.com/v2-1fce03eb74d5dbab95bb21_r.jpg&&&/figure&&p&&br&&/p&&p&&b&list.cbind&/b&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&a&-1:10
b&-sample(LETTERS,10)
c&-rep(c(TRUE,FALSE),each=5)
mylist&-list(ID=a,NAME=b,ACCET=c)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-7d41aad8c_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&569& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-7d41aad8c_r.jpg&&&/figure&&p&&br&&/p&&p&mylist对象有三个子list,每一个长度都为10,按照其实际意义,可以按列合并为data.frame。 &/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&do.call(cbind,mylist) %&&% data.frame()
list.cbind(mylist) %&&% data.frame()
#list.cbind就更好理解了,它可以直接将子list按照列进行合并,
#使之成为规整的矩阵或者数据框(其意义与作用于cbind函数并无不同)。
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-7d41aad8c_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&569& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-7d41aad8c_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-101d3ceaabd8_b.jpg& data-caption=&& data-rawwidth=&672& data-rawheight=&489& class=&origin_image zh-lightbox-thumb& width=&672& data-original=&https://pic4.zhimg.com/v2-101d3ceaabd8_r.jpg&&&/figure&&p&&br&&/p&&p&list.flatten函数可以清除掉递归结构,有点儿类似于unlist函数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&list.flatten(mydata)
&/code&&/pre&&/div&&p&&br&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-5006a8afd4b6ae0155d7abf_b.jpg& data-caption=&& data-rawwidth=&720& data-rawheight=&738& class=&origin_image zh-lightbox-thumb& width=&720& data-original=&https://pic3.zhimg.com/v2-5006a8afd4b6ae0155d7abf_r.jpg&&&/figure&&p&&br&&/p&&p&但是麻烦的是,list.flatten是毁灭式的清除,就是清除之后,相当于遍历了所有子节点,每一个子节点都会被识别为一个单独的字符串,这样我们下一步的数据清洗工作几乎没法进行了,所以慎用。(除非是很规整的递归结构,铺平之后你也许还有希望使用matrix结构进行合适的行列调整,还原这个数据表,但是那样也很费事)。&/p&&p&除此之外,rlist还有大量的list.find、list.merge、list.serch、list.extrct等让人眼花缭乱的高效list操纵函数(据说还支持lamda表达式),甚至可以直接解析XML文档,简直是list操纵的绝世神器呀!绝对值得一学!&/p&&p&如果你打算入手noSQL,那么R语言中的list就是很好地对标工具(Python中也许是dict吧)。&/p&&p&至于更为详细的rlist操纵技巧,请参考起官方文档或者任坤老师的主页!!!&/p&&p&&b&参考资料:&/b&&/p&&p&&a href=&https://link.zhihu.com/?target=https%3A//cosx.org/2014/07/rlist-package& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&cosx.org/2014/07/rlist-&/span&&span class=&invisible&&package&/span&&span class=&ellipsis&&&/span&&/a&&/p&&p&&a href=&https://link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/rlist/rlist.pdf& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&cran.r-project.org/web/&/span&&span class=&invisible&&packages/rlist/rlist.pdf&/span&&span class=&ellipsis&&&/span&&/a&&/p&&p&&br&&/p&&p&在线课程请点击文末原文链接:&/p&&p&&a href=&https://link.zhihu.com/?target=https%3A//edu.hellobi.com/course/195/lessons& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Hellobi Live | 9月12日 R语言可视化在商务场景中的应用&/a&&br&往期案例数据请移步本人GitHub:&br&&a href=&https://link.zhihu.com/?target=https%3A//github.com/ljtyduyu/DataWarehouse/tree/master/File& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&github.com/ljtyduyu/Dat&/span&&span class=&invisible&&aWarehouse/tree/master/File&/span&&span class=&ellipsis&&&/span&&/a&&/p&&p&&/p&
list是R语言中包容性最强的数据对象,几乎可以容乃所有的其他数据类型。但是包容性最强也也意味着他对于内部子对象的类型限制最少,甚至内部可以存在递归结构,这样给我们提取数据带来了很大的困难。如果你对R语言的list结构非常熟悉,又熟练控制流等函数的…
&figure&&img src=&https://pic3.zhimg.com/v2-d306e3ad80c99af7eba41ce_b.jpg& data-rawwidth=&800& data-rawheight=&350& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic3.zhimg.com/v2-d306e3ad80c99af7eba41ce_r.jpg&&&/figure&&blockquote&&p&本使用说明教程主要翻译整理自 &a href=&https://link.zhihu.com/?target=https%3A//facebookincubator.github.io/prophet/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Prophet 的官方说明文档&/a&&/p&&p&教程中使用的数据集可在 &a href=&https://link.zhihu.com/?target=https%3A//github.com/facebookincubator/prophet& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Prophet 的 github 主页&/a& 中的 &a href=&https://link.zhihu.com/?target=https%3A//github.com/facebookincubator/prophet/tree/master/examples& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&examples 文件夹&/a& 内下载得到。&/p&&/blockquote&&p&Prophet 是 Facebook 推出的开源大规模预测工具,可以在 R 和 Python 中使用以预测时间序列数据。 &/p&&p&下面将简单介绍 Prophet 在 R 中的使用。&/p&&h2&一、基础介绍&/h2&&p&下面实例中使用的是 &a href=&https://link.zhihu.com/?target=https%3A//en.wikipedia.org/wiki/Peyton_Manning& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&佩顿 · 曼宁的维基百科主页&/a& 每日访问量的时间序列数据( - )。我们使用 R 中的 &a href=&https://link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/wikipediatrend/vignettes/using-wikipediatrend.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Wikipediatrend&/a& 包获取该数据集。这个数据集具有多季节周期性、不断变化的增长率和可以拟合特定日期(例如佩顿 · 曼宁的决赛和超级碗)的情况等 Prophet 适用的性质,因此可以作为一个不错的例子。(&em&注:&/em&佩顿 · 曼宁为前美式橄榄球四分卫)&/p&&p&在 R 中,Prophet 提供了一个 prophet 函数去拟合模型并且返回一个模型对象,可以对这个模型对象执行“预测”( predict )和“绘图”( plot )操作。&/p&&figure&&img src=&https://pic4.zhimg.com/v2-32b263b3c4caae11cb87628_b.jpg& data-rawwidth=&1138& data-rawheight=&551& class=&origin_image zh-lightbox-thumb& width=&1138& data-original=&https://pic4.zhimg.com/v2-32b263b3c4caae11cb87628_r.jpg&&&/figure&&figure&&img src=&https://pic3.zhimg.com/v2-d86da7c35f8d6a23bd9a5_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic3.zhimg.com/v2-d86da7c35f8d6a23bd9a5_r.jpg&&&/figure&&p&使用 prophet_plot_components 函数去展示预测中的趋势、周效应和年度效应。&figure&&img src=&https://pic4.zhimg.com/v2-fffaf98dd2cf56dea6764_b.jpg& data-rawwidth=&1132& data-rawheight=&113& class=&origin_image zh-lightbox-thumb& width=&1132& data-original=&https://pic4.zhimg.com/v2-fffaf98dd2cf56dea6764_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-1b11c518a6d28554a9acafcf4bafaacd_b.jpg& data-rawwidth=&648& data-rawheight=&648& class=&origin_image zh-lightbox-thumb& width=&648& data-original=&https://pic4.zhimg.com/v2-1b11c518a6d28554a9acafcf4bafaacd_r.jpg&&&/figure&&/p&&p&&em&注:&/em& Windows 系统语言设置为中文的话,会导致 R 输出成分分析图时,周效应无法正常显示,可在 R 中使用 Sys.setlocale(&LC_ALL&,&English&) 代码将环境改为英文。&/p&&h2&二、预测增长&/h2&&p&默认情况下, Prophet 使用线性模型进行预测。当预测增长情况时,通常会存在可到达的最大极限值,例如:总市场规模、总人口数等等。这被称做承载能力,那么预测时就应当在接近该值时趋于饱和。&/p&&p&Prophet 可使用 &a href=&https://link.zhihu.com/?target=https%3A//en.wikipedia.org/wiki/Logistic_function& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&logistic 增长&/a& 趋势模型进行预测,同时指定承载能力。下面使用 &a href=&https://link.zhihu.com/?target=https%3A//en.wikipedia.org/wiki/R_%2528programming_language%2529& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&R 语言的维基百科主页&/a&访问量(取对数)的实例来进行说明。&figure&&img src=&https://pic1.zhimg.com/v2-bcdbcac14c92eb270bb2_b.jpg& data-rawwidth=&1126& data-rawheight=&480& class=&origin_image zh-lightbox-thumb& width=&1126& data-original=&https://pic1.zhimg.com/v2-bcdbcac14c92eb270bb2_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-de9143a2fac0f5_b.jpg& data-rawwidth=&800& data-rawheight=&500& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic4.zhimg.com/v2-de9143a2fac0f5_r.jpg&&&/figure&&/p&&br&&h2&三、趋势突变点&/h2&&p&默认情况下, Prophet 将自动监测到突变点,并对趋势做适当地调整。&/p&&p&下面将会介绍几种使用的方法可以对趋势的调整过程做更好地控制。&/p&&h3&1. 调整趋势的灵活性&/h3&&p&如果趋势的变化被过度拟合(即过于灵活)或者拟合不足(即灵活性不够),可以利用输入参数 changepoint.prior.scale 来调整稀疏先验的程度。默认下,这个参数被指定为 0.05 。&/p&&p&增加这个值,会导致趋势拟合得更加灵活。如下代码和图所示:&figure&&img src=&https://pic4.zhimg.com/v2-572c1bc9ed5b99fbde3719_b.jpg& data-rawwidth=&1132& data-rawheight=&315& class=&origin_image zh-lightbox-thumb& width=&1132& data-original=&https://pic4.zhimg.com/v2-572c1bc9ed5b99fbde3719_r.jpg&&&/figure&&figure&&img src=&https://pic2.zhimg.com/v2-db77eaab27_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic2.zhimg.com/v2-db77eaab27_r.jpg&&&/figure&&/p&&p&减少这个值,会导致趋势拟合得灵活性降低。如下代码和图所示:&figure&&img src=&https://pic2.zhimg.com/v2-73bdefaafdf_b.jpg& data-rawwidth=&1134& data-rawheight=&176& class=&origin_image zh-lightbox-thumb& width=&1134& data-original=&https://pic2.zhimg.com/v2-73bdefaafdf_r.jpg&&&/figure&&figure&&img src=&https://pic2.zhimg.com/v2-55e1bc9ecb5892a72ebe4b7_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic2.zhimg.com/v2-55e1bc9ecb5892a72ebe4b7_r.jpg&&&/figure&&/p&&br&&h3&2. 指定突变点的位置&/h3&&p&如果你希望手动指定潜在突变点的位置而不是利用自动的突变点监测,可以使用 changepoints 参数。&/p&&p&代码和图如下所示:&figure&&img src=&https://pic3.zhimg.com/v2-a912bbced4d77757cbe0582a_b.jpg& data-rawwidth=&1144& data-rawheight=&183& class=&origin_image zh-lightbox-thumb& width=&1144& data-original=&https://pic3.zhimg.com/v2-a912bbced4d77757cbe0582a_r.jpg&&&/figure&&figure&&img src=&https://pic1.zhimg.com/v2-a15c8068dbbd5ef851bad_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic1.zhimg.com/v2-a15c8068dbbd5ef851bad_r.jpg&&&/figure&&/p&&br&&h3&四、节假日效应&/h3&&h3&1. 对节假日建模&/h3&&p&如果需要专门对节假日进行建模,你就必须得为此创建一个新的数据框,其中包含两列(节假日 holiday 和日期戳 ds ),每行分别记录了每个出现的节假日。&/p&&p&你可以在这个数据框基础上再新建两列 lower_window 和 upper_window ,从而将节假日的时间扩展成一个区间 [ lower_window , upper_window ] 。举例来说,如果想将平安夜也加入到 “圣诞节” 里,就设置 lower_window = -1 , upper_window = 0 ;如果想将黑色星期五加入到 “感恩节” 里,就设置 lower_window = 0 , upper_window = 1 。&/p&&p&下面我们创建一个数据框,其中包含了所有佩顿 · 曼宁参加过的决赛日期:&figure&&img src=&https://pic4.zhimg.com/v2-d1fa7d9bd451b41f70d8d_b.jpg& data-rawwidth=&1128& data-rawheight=&650& class=&origin_image zh-lightbox-thumb& width=&1128& data-original=&https://pic4.zhimg.com/v2-d1fa7d9bd451b41f70d8d_r.jpg&&&/figure&&/p&&p&上述代码中,我们将超级碗的日期既记录在了决赛的日期数据框中,也记录在了超级碗的日期数据框中。这就会造成超级碗日期的效应会在决赛日期的作用下叠加两次。&/p&&p&一旦这个数据框创建好了,就可以通过传入 holidays 参数使得在预测时考虑上节假日效应。&figure&&img src=&https://pic4.zhimg.com/v2-beaab22bba81880c2aedd76cf3c500e6_b.jpg& data-rawwidth=&1131& data-rawheight=&142& class=&origin_image zh-lightbox-thumb& width=&1131& data-original=&https://pic4.zhimg.com/v2-beaab22bba81880c2aedd76cf3c500e6_r.jpg&&&/figure&&/p&&p&可通过 forecast 数据框,来展示节假日效应:&figure&&img src=&https://pic3.zhimg.com/v2-0be225cda9a6_b.jpg& data-rawwidth=&1131& data-rawheight=&177& class=&origin_image zh-lightbox-thumb& width=&1131& data-original=&https://pic3.zhimg.com/v2-0be225cda9a6_r.jpg&&&/figure&&figure&&img src=&https://pic2.zhimg.com/v2-da6ac56ddf7_b.jpg& data-rawwidth=&367& data-rawheight=&210& class=&content_image& width=&367&&&/figure&&/p&&br&&p&在成分分析的图中,如下所示,也可以看到节假日效应。我们可以发现,在决赛日期附近有一个穿透,而在超级碗日期时穿透则更为明显。&figure&&img src=&https://pic1.zhimg.com/v2-a8bdd7487_b.jpg& data-rawwidth=&1128& data-rawheight=&76& class=&origin_image zh-lightbox-thumb& width=&1128& data-original=&https://pic1.zhimg.com/v2-a8bdd7487_r.jpg&&&/figure&&figure&&img src=&https://pic2.zhimg.com/v2-8b0f51ae829d_b.jpg& data-rawwidth=&800& data-rawheight=&1200& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic2.zhimg.com/v2-8b0f51ae829d_r.jpg&&&/figure&&/p&&br&&h3&2. 对节假日和季节性设定先验规模&/h3&&p&如果发现节假日效应被过度拟合了,通过设置参数 holidays.prior.scale 可以调整它们的先验规模来使之平滑,默认下该值取 10 。&figure&&img src=&https://pic1.zhimg.com/v2-cfdecfbf96_b.jpg& data-rawwidth=&1132& data-rawheight=&281& class=&origin_image zh-lightbox-thumb& width=&1132& data-original=&https://pic1.zhimg.com/v2-cfdecfbf96_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-d35d8b3af2ff8a7ef8b66378_b.jpg& data-rawwidth=&368& data-rawheight=&209& class=&content_image& width=&368&&&/figure&&/p&&br&&p&和之前相比,节假日效应的规模被减弱了,特别是对观测值较少的超级碗而言。类似的,还有一个 seasonality.prior.scale 参数可以用来调整模型对于季节性的拟合程度。&/p&&h2&五、预测区间&/h2&&p&在预测时,不确定性主要来源于三个部分:趋势中的不确定性、季节效应估计中的不确定性和观测值的噪声影响。&/p&&h3&1. 趋势中的不确定性&/h3&&p&预测中,不确定性最大的来源就在于未来趋势改变的不确定性。Prophet 假定 “未来将会和历史具有相似的趋势” 。尤其重要的是,我们假定未来趋势的平均变动频率和幅度和我们观测到的历史值是一样的,从而预测趋势的变化并通过计算,最终得到预测区间。&/p&&p&这种衡量不确定性的方法具有以下性质:变化速率灵活性更大时(通过增大参数 changepoint.prior.scale 的值),预测的不确定性也会随之增大。原因在于如果将历史数据中更多的变化速率加入了模型,也就代表我们认为未来也会变化得更多,就会使得预测区间成为反映过拟合的标志。&/p&&p&预测区间的宽度(默认下,是 80% )可以通过设置 interval.width 参数来控制:&figure&&img src=&https://pic2.zhimg.com/v2-f05ff65c3f96d_b.jpg& data-rawwidth=&1129& data-rawheight=&141& class=&origin_image zh-lightbox-thumb& width=&1129& data-original=&https://pic2.zhimg.com/v2-f05ff65c3f96d_r.jpg&&&/figure&&/p&&h3&2. 季节效应中的不确定性&/h3&&p&默认情况下, Prophet 只会返回趋势中的不确定性和观测值噪声的影响。你必须使用贝叶斯取样的方法来得到季节效应的不确定性,可通过设置 mcmc.samples 参数(默认下取 0 )来实现。&figure&&img src=&https://pic2.zhimg.com/v2-ded9e1596fe_b.jpg& data-rawwidth=&1130& data-rawheight=&142& class=&origin_image zh-lightbox-thumb& width=&1130& data-original=&https://pic2.zhimg.com/v2-ded9e1596fe_r.jpg&&&/figure&&/p&&p&上述代码将最大后验估计( MAP )取代为马尔科夫蒙特卡洛取样 ( MCMC ),并且将计算时间从 10 秒延长到 10 分钟。如果做了全取样,就能通过绘图看到季节效应的不确定性了:&figure&&img src=&https://pic1.zhimg.com/v2-d73bcfd88f4cab787e75a2ed34ce05c0_b.jpg& data-rawwidth=&1133& data-rawheight=&75& class=&origin_image zh-lightbox-thumb& width=&1133& data-original=&https://pic1.zhimg.com/v2-d73bcfd88f4cab787e75a2ed34ce05c0_r.jpg&&&/figure&&figure&&img src=&https://pic3.zhimg.com/v2-8a2e608eed9a05e1cf6dac3ee1264ce9_b.jpg& data-rawwidth=&800& data-rawheight=&900& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic3.zhimg.com/v2-8a2e608eed9a05e1cf6dac3ee1264ce9_r.jpg&&&/figure&&/p&&br&&h2&六、异常值&/h2&&p&下面我们使用之前使用过的 R 语言维基百科主页对数访问量的数据来建模预测,只不过使用存在时间间隔并不完整的数据:&figure&&img src=&https://pic3.zhimg.com/v2-16dac79fc76a04ddd00e1f_b.jpg& data-rawwidth=&1131& data-rawheight=&246& class=&origin_image zh-lightbox-thumb& width=&1131& data-original=&https://pic3.zhimg.com/v2-16dac79fc76a04ddd00e1f_r.jpg&&&/figure&&/p&&p&&figure&&img src=&https://pic1.zhimg.com/v2-61caf5adcbed80c89ed129_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic1.zhimg.com/v2-61caf5adcbed80c89ed129_r.jpg&&&/figure&如上 R 输出图所示,趋势预测看似合理,预测区间的估计却过于广泛。&/p&&p&处理异常值最好的方法是移除它们,而 Prophet 使能够处理缺失数据的。如果在历史数据中某行的值为空( NA ),但是在待预测日期数据框 future 中仍保留这个日期,那么 Prophet 依旧可以给出该行的预测值。&figure&&img src=&https://pic4.zhimg.com/v2-73e9cc6654_b.jpg& data-rawwidth=&1131& data-rawheight=&246& class=&origin_image zh-lightbox-thumb& width=&1131& data-original=&https://pic4.zhimg.com/v2-73e9cc6654_r.jpg&&&/figure&&figure&&img src=&https://pic1.zhimg.com/v2-e5d0ec82abf8bb0ff785ed14a4c07126_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic1.zhimg.com/v2-e5d0ec82abf8bb0ff785ed14a4c07126_r.jpg&&&/figure&&br&&/p&&br&&p&上述这个实例虽然影响了不确定性的估计,却没有影响到主要的预测值 yhat 。但是,现实往往并非如此,接下来,在上述数据集基础上加入新的异常值后再建模预测:&figure&&img src=&https://pic3.zhimg.com/v2-d092df88ef73d00e27471a_b.jpg& data-rawwidth=&1133& data-rawheight=&257& class=&origin_image zh-lightbox-thumb& width=&1133& data-original=&https://pic3.zhimg.com/v2-d092df88ef73d00e27471a_r.jpg&&&/figure&&figure&&img src=&https://pic1.zhimg.com/v2-68dd1092eee4bc861376_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic1.zhimg.com/v2-68dd1092eee4bc861376_r.jpg&&&/figure&&/p&&br&&p&这里 2015年 6 月存在一些异常值破坏了季节效应的估计,因此未来的预测也会永久地受到这个影响。最好的解决方法就是移除这些异常值:&br&&figure&&img src=&https://pic1.zhimg.com/v2-f6bc29c43da10_b.jpg& data-rawwidth=&1134& data-rawheight=&250& class=&origin_image zh-lightbox-thumb& width=&1134& data-original=&https://pic1.zhimg.com/v2-f6bc29c43da10_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-bff91cebdfaa_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic4.zhimg.com/v2-bff91cebdfaa_r.jpg&&&/figure&&/p&&h2&七、非日数据&/h2&&p&Prophet 处理的对象并非必须是日数据,不过要是试图通过非日数据来预测每日的情况或拟合季节性效应,往往会得到奇奇怪怪的结果。下面使用美国零售业销售量数据来预测未来 10 年的情况: &figure&&img src=&https://pic3.zhimg.com/v2-fea37739c26_b.jpg& data-rawwidth=&1131& data-rawheight=&214& class=&origin_image zh-lightbox-thumb& width=&1131& data-original=&https://pic3.zhimg.com/v2-fea37739c26_r.jpg&&&/figure&&figure&&img src=&https://pic1.zhimg.com/v2-53c43a4df569b2be50a8e1ccb1ea999b_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic1.zhimg.com/v2-53c43a4df569b2be50a8e1ccb1ea999b_r.jpg&&&/figure&预测结果看起来非常杂乱,原因正是在于这个特殊的数据集使用的是月数据。当我们拟合年度效应时,只有每个月第一天的数据,而且对于其他天的周期效应是不可测且过拟合的。当你使用 Prophet 拟合月度数据时,可以通过在 make_future_dataframe 中传入频率参数只做月度的预测。&figure&&img src=&https://pic4.zhimg.com/v2-6fe3c950b87a35fc53df_b.jpg& data-rawwidth=&1132& data-rawheight=&181& class=&origin_image zh-lightbox-thumb& width=&1132& data-original=&https://pic4.zhimg.com/v2-6fe3c950b87a35fc53df_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/v2-8fec2fabcd97e5d6959e4_b.jpg& data-rawwidth=&800& data-rawheight=&400& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic4.zhimg.com/v2-8fec2fabcd97e5d6959e4_r.jpg&&&/figure&&/p&&blockquote&&p&本文译作者:&a href=&https://link.zhihu.com/?target=https%3A//vectorf.github.io/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Vector&/a&&/p&&/blockquote&
本使用说明教程主要翻译整理自 教程中使用的数据集可在
内下载得到。Prophet 是 Facebook 推出的开源大规模预测工具,可以在 R 和 Python 中使用以预测时间序列数据。 下面将简单介绍 P…
&a href=&//link.zhihu.com/?target=http%3A//blog.fens.me/r-quant-packages/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&R语言量化投资常用包总结&/a&&br&&br&&ul&&li&数据管理:包括数据集抓取、存储、读取、时间序列、数据处理等,涉及R包有 &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/zoo/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&zoo&/a&(时间序列对象),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/xts/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&xts&/a&(时间序列处理), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/timeSeries/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&timeSeries&/a&(Rmetrics系时间序列对象) &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/timeDate/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&timeDate&/a&(Rmetrics系时间序列处理),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/data.table/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&data.table&/a&(数据处理), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/quantmod/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&quantmod&/a&(数据下载和图形可视化), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/RQuantLib/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&RQuantLib&/a&(QuantLib数据接口), &a href=&//link.zhihu.com/?target=http%3A//180.96.8.19/windnet/Bulletin/help/R.pdf& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&WindR&/a&(Wind数据接口), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/RJDBC/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&RJDBC&/a&(数据库访问接口), &a href=&//link.zhihu.com/?target=https%3A//github.com/RevolutionAnalytics/RHadoop/wiki& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&rhadoop&/a&(Hadoop访问接口), &a href=&//link.zhihu.com/?target=https%3A//github.com/nexr/RHive& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&rhive&/a&(Hive访问接口), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/rredis/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&rredis&/a&(Redis访问接口), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/rmongodb/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&rmongodb&/a&(MongoDB访问接口), &a href=&//link.zhihu.com/?target=https%3A//github.com/apache/spark/tree/master/R& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&SparkR&/a&(Spark访问接口),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fImport/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fImport&/a&(Rmetrics系数据访问接口)等。&/li&&li&指标计算:包括金融市场的技术指标的各种计算方法,涉及R包有 &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/TTR/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&TTR&/a&(技术指标), TSA(时间序列计算),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/urca/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&urca&/a&(单位根检验), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fArma/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fArma&/a&(Rmetrics系ARMA计算), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fAsianOptions/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fAsianOptions&/a&(Rmetrics系亚洲期权定价),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fBasics/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fBasics&/a&(Rmetrics系计算工具), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fCopulae/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fCopulae&/a&(Rmetrics系财务分析), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fExoticOptions/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fExoticOptions&/a&(Rmetrics系期权计算),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fGarch/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fGarch&/a&(Rmetrics系Garch模型), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fNonlinear/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fNonlinear&/a&(Rmetrics系非线模型), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fOptions/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fOptions&/a&(Rmetrics系期权定价),&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fRegression/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fRegression&/a&(Rmetrics系回归分析), &a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/fUnitRoots/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&fUnitRoots&/a&(Rmetrics系单位根检验) 等。&/li&&li&回测交易:包括金融数据建模,并验证用历史数据验证模型的可靠性,涉及R包有&a href=&//link.zhihu.com/?target=https%3A//cran.r-project.org/web/packages/FinancialInstrument/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&FinancialInstrument&/a&(金融产品), &a href=&//link.zhihu.com/?target=https%3A//r-forge.r-project.org/R/%3Fgroup_id%3D316& class=& wrap external& target=&_blank& rel=&nofollow noref}

我要回帖

更多关于 文字E-R图关系描述 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信