R 随机森林分类和训练/测试分割
我在机器学习方面迈出了第一步,尤其是R。我以前使用过python的sklearn,但我对R完全不熟悉。为了一个大学项目,我尝试在一个基因表达式数据集上创建一个随机林,用于教育目的。我试图通过各种脑细胞的基因表达来预测精神障碍(双相情感障碍、抑郁症或精神分裂症)。我的脚本当前如下所示:R 随机森林分类和训练/测试分割,r,machine-learning,random-forest,R,Machine Learning,Random Forest,我在机器学习方面迈出了第一步,尤其是R。我以前使用过python的sklearn,但我对R完全不熟悉。为了一个大学项目,我尝试在一个基因表达式数据集上创建一个随机林,用于教育目的。我试图通过各种脑细胞的基因表达来预测精神障碍(双相情感障碍、抑郁症或精神分裂症)。我的脚本当前如下所示: library(randomForest) train_ind <- sample.int(n = nrow(GSMdata), size = floor(
library(randomForest)
train_ind <- sample.int(n = nrow(GSMdata),
size = floor(0.75*nrow(GSMdata)),
replace = F)
RFtrainSet <- data[,train_ind]
RFtestSet <- data[,-train_ind]
RFtrainLabel <- GSMdata$Disease_State[train_ind]
RFtestLabel <- GSMdata$Disease_State[-train_ind]
RFmodel <- randomForest(x = t(RFtrainSet),
y = RFtrainLabel,
ntree = 100)
table(RFtestLabel, predict(object = RFmodel,
newdata = t(RFtestSet)))
RFtestLabel bipolar disorder control major depressive disorder schizophrenia
bipolar disorder 0 7 6 7
control 0 7 6 0
major depressive disorder 0 5 2 2
schizophrenia 0 1 7 2
通常,当我对数据进行采样时,一个类不会出现在测试数据集中,正如您在上面的示例中所看到的那样。这是个问题吗?如果是,是否有一个函数可以帮助我获得均匀的测试样本
数据示例
数据
矩阵:
GSM1304852 GSM1304853 GSM1304854 GSM1304855 GSM1304856
1007_s_at 2.3945368 2.27518369 2.16116298 1.9641833 2.1322526
1053_at 0.1051084 0.06160802 0.34217618 0.3593916 0.2235696
117_at -0.4597124 -0.52310349 -0.44360591 -0.6370277 -0.3511470
121_at 0.9333566 1.13180904 0.99756999 1.0079778 0.9720455
1255_g_at -0.2399138 0.10112324 -0.04087979 -0.2185137 -0.2991786
GSMdata
示例:
title geo_accession Age Disease_State Gender pH PMI Race RIN tissue
GSM1304852 bipolar_hip_10 GSM1304852 52 bipolar disorder M 6.7 23.5 W 6.3 hippocampus
GSM1304853 bipolar_hip_11 GSM1304853 50 bipolar disorder F 6.4 11.7 W 6.8 hippocampus
GSM1304854 bipolar_hip_12 GSM1304854 28 bipolar disorder F 6.3 22.3 W 7.7 hippocampus
GSM1304855 bipolar_hip_13 GSM1304855 55 bipolar disorder F 6.4 17.5 W 7.6 hippocampus
GSM1304856 bipolar_hip_14 GSM1304856 58 bipolar disorder M 6.8 27.7 W 7.0 hippocampus
这里有一个快速的dplyr解决方案,可以在类内进行采样,不需要特殊的函数。我以iris数据集为例,但您可以快速地将其适应于您的数据
library(dplyr)
data(iris)
labels <- iris %>% dplyr::select(Species) %>%
sample_frac(1) %>%
group_by(Species) %>%
mutate(set = rep(c(rep("train",3),"test"), length.out=n()))
table(labels$Species, labels$set)
test train
setosa 12 38
versicolor 12 38
virginica 12 38
库(dplyr)
数据(iris)
标签%dplyr::选择(物种)%>%
样本分数(1)%>%
组别(种类)%>%
变异(set=rep(c(rep(“train”,3),“test”),length.out=n())
表(标签$Species,标签$set)
试验列车
刚毛12 38
花色1238
弗吉尼亚州1238
另外,我建议使用
ranger
random-forest包,因为它速度更快 一种方法是使用分层
(来自splitstackshape
包)和sqldf
(进行SQL查询),如下所示:
set.seed(1231)
data(iris)
data <- iris
data$ID <- seq.int(nrow(data)) #Why? remove it and run this again without this bit and you will see the difference.
# making stratified train samples
m_trn <- data.frame((splitstackshape::stratified(data, "Species", 0.5))) #0.5 is percent of training data in each class
m_tst <- (sqldf::sqldf('SELECT * FROM data EXCEPT SELECT * FROM m_trn'))
set.seed(1231)
数据(iris)
数据我本以为更大的问题是测试集的类不在训练集中。您可以尝试在类内采样。