python调用R第三方包

9 篇文章 0 订阅

前面写到 《python3调用R》这篇文章。我们成功的搭建了python下调用R的环境。
下来来探讨一下,如何调用R包来处理python里面的数据。

我们把脚本装再r_script 里面并,用引号标注起来,然后就可以使用啦。

In [14]: r_script = '''
    ...: library(randomForest) # 导入随机森林包
    ...: ## use data set iris
    ...: data = iris # 使用鸢尾花数据集
    ...: table(data$Species)
    ...: ## create a randomForest model to classfy the iris species
    ...: # 创建随机森林模型给鸢尾花分类
    ...: iris.rf <- randomForest(Species~., data = data, importance=T, proximity=T)
    ...: print('--------here is the random model-------')
    ...: print(iris.rf)
    ...: print('--------here is the names of model-----')
    ...: print(names(iris.rf))
    ...: confusion = iris.rf$confusion
    ...: print(confusion)
    ...: '''

In [16]: robjects.r(r_script)
Out[16]: 
R object with classes: ('matrix',) mapped to:
<Matrix - Python:0x00000000126E8A88 / R:0x000000000FD016A0>
[50.000000, 0.000000, 0.000000, ..., 0.000000, 0.060000, 0.060000]
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值