R训练RandomForest并转pmml

star2017 1年前 ⋅ 8668 阅读

from https://blog.csdn.net/c1481118216/article/details/74202786

PMML

关于pmml 请查看博客:

http://blog.csdn.net/c1481118216/article/details/78411200

源码

github: https://github.com/liaotuo/R_RandomForest_pmml.git

R训练Random Forest

本次实例中,使用R 语言自带的数据集 iris 训练一个Random Forest模型,数据集将被分为两个样本(70%训练集,30%测试集)
R 语言脚本如下:

需要安装randomForest包: install.packages(“randomForest”)

# load library and data
library(randomForest)
data(iris)

# load data and divide(划分) into training set and sampling(训练集和测试集)
# 将数据分为两部分 70%训练集 30%测试集
ind <- sample(2,nrow(iris),replace=TRUE,prob=c(0.7,0.3))
trainData <- iris[ind==1,]
testData <- iris[ind==2,]

# train model
iris_rf <- randomForest(Species~.,data=trainData,ntree=100,proximity=TRUE)
table(predict(iris_rf),trainData$Species)

# visualize the model
print(iris_rf)
attributes(iris_rf)
plot(iris_rf)

模型转pmml保存

上面的代码我们生成了一个randomForest模型,现在需要把模型转换成PMML,需要用到的包有:XML PMML 需要提前安装,命令如下:

xml:install.packages(“XML”)
pmml: install.packages(“pmml”,dependencies=TRUE)
接下来执行脚本:

# convert model to pmml
iris_rf.pmml <- pmml(iris_rf,name="Iris Random Forest",data=iris_rf)

# save to file "iris_rf.pmml" in disk 路径自定义
saveXML(iris_rf.pmml,"D://iris_rf.pmml")

然后生成了一个pmml文件。
R训练Random Forest并转pmml

这样我们就成功转换成PMMl文件了

原创文章,作者:xsmile,如若转载,请注明出处:http://www.17bigdata.com/r%e8%ae%ad%e7%bb%83random-forest%e5%b9%b6%e8%bd%acpmml/

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: