r - 如何模拟要在 R 中用于随机森林的分类数据?
问题描述
我想知道如何模拟一些可在随机森林中用于 R 分类的数据?
如果是为了回归,我会做类似的事情:
n <- 1000
p <- 3
e <- rnorm(n)
b <- 10
xVal <- matrix(rnorm(n*p), nrow=n) # Create matrix wt 3 columns
colnames(xVal)<- paste0("x",1:p) # Name columns
df <- data.frame(xVal) # Create dataframe
# Make x1 a useful predictor of y:
y <- df$x1 + e
df$y <- y
看起来像这样:
head(df,3)
x1 x2 x3 y
1 -0.6512695 0.3639012 -0.50231648 -0.296679882
2 -1.1393367 -0.8148882 0.33065078 -2.703743889
3 -0.2674592 -0.2670326 -0.15028117 1.024109832
其中 x1 是 y 的有用预测器,而 x2 和 x3 只是随机噪声。然后我只是将随机森林回归模型拟合到他的数据中。
我将如何实现类似的分类?
解决方案
x1 = c(rnorm(500, 0,1), rnorm(500,3,1))
x2 = rnorm(1000)
x3 = rnorm(1000)
class= factor(rep(1:2, each=500))
plot(x1,x2, pch=20, col=class)
x1
是一个有用的预测器class
。x2
并且x3
只是噪音。
推荐阅读
- python - 如何从python docx中删除粗体字
- flutter - Flutter 容器高度与父高度相同
- javascript - IE 11,Edge - 当按下文件上传按钮时,它会自动提交表单而不是等待
- python - 散景图访问单选按钮在滑块回调中处于活动状态
- laravel - 授权时如何查看其他字段的hash?
- r - 从忽略R中的空白的表中产生所有可能的组合
- javascript - 如何使用量角器获取设置值名字?
- r - 组合两个“ts”对象时的不一致数组
- jquery - jQuery html() 将角度属性转换为小写
- cordova - 在 cordova 插件中将类路径添加到 build.gradle