机器学习算法:随机森林|python与r语言代码实现

2018-10-1200:02:49数据结构与算法Comments2,547 views字数 742阅读模式

随机森林

随机森林是决策树集合的商标术语。在随机森林中,我们收集了决策树(被称为“森林”)。为了根据属性对新对象进行分类,每个树都给出分类,并且我们称树为该类“投票”。森林选择选票最多的分类(超过森林中的所有树木)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html

每棵树被种植和生长如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html

  1. 如果训练集中的病例数为N,则随机抽取N例样本,但进行替换。这个样本将是培养树木的训练工具。
  2. 如果存在M个输入变量,则指定一个数字m>M,使得在每个节点处,随机地从M中选择m个变量,并使用这些m上的最佳分割来分割节点。在森林生长过程中,M的值保持不变。
  3. 每棵树的生长尽可能最大,没有修剪。

Python代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html

#Import Library
from sklearn.ensemble import RandomForestClassifier
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
# Create Random Forest object
model= RandomForestClassifier()
# Train the model using the training sets and check score
model.fit(X, y)
#Predict Output
predicted= model.predict(x_test)

R语言代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html

library(randomForest)
x <- cbind(x_train,y_train)
# Fitting model
fit <- randomForest(Species ~ ., x,ntree=500)
summary(fit)
#Predict Output 
predicted= predict(fit,x_test)
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/suanfa/6548.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定