随机森林
随机森林是决策树集合的商标术语。在随机森林中,我们收集了决策树(被称为“森林”)。为了根据属性对新对象进行分类,每个树都给出分类,并且我们称树为该类“投票”。森林选择选票最多的分类(超过森林中的所有树木)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html
每棵树被种植和生长如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6548.html
- 如果训练集中的病例数为N,则随机抽取N例样本,但进行替换。这个样本将是培养树木的训练工具。
- 如果存在M个输入变量,则指定一个数字m>M,使得在每个节点处,随机地从M中选择m个变量,并使用这些m上的最佳分割来分割节点。在森林生长过程中,M的值保持不变。
- 每棵树的生长尽可能最大,没有修剪。
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