数据抽样smaple、等比抽样 (根据谢佳标老师课程整理)

来源:互联网 发布:dw制作淘宝首页 编辑:程序博客网 时间:2024/05/25 08:15

1.R 中的sample函数可以实现数据的随机抽样。

sample(x, size, replace = FALSE, prob = NULL)

x  表示向量或多个元素

size  抽样的个数大小。replace=FALSE 非放回抽样。
2.对样本进行抽样的一般做法。

> x1<-read.csv("E:\\Users\\Administrator\\Desktop\\sample1.csv",sep="\t",head=FALSE)
> head(x1)      V1  V2          V3       V4         V5  V61 377563  67      520.16      \\N        \\N   12 377573 253  1422982.89      \\N        \\N  393 377574 336 19928643.68 84134.79 4760172.00  864 377575 147    13918.69      \\N        \\N \\N5 377576  56      288.06      \\N        \\N   16 377579 141        0.00      \\N        \\N \\N
> nrow(x1)[1] 65800
> set.seed(1234)> index<-sample(x,10000,replace=TRUE)> x_sample<-x1[index,]
这样抽样对吗? 从x1里面有放回的抽取10000个。貌似没什么问题,一般根据返回的结果是错误的,因为x 一般为一列数或向量。

变通一下,抽取对应的行号,然后组成新的数据不也可以吗。

> index<-sample(nrow(x1),10000,replace = FALSE)
这样的话index应该为一组数值。

> head(index)[1] 204506 299278  11126  72460 294043 334309
> x2-x1[index,]> head(x2)
              V1  V2   V3  V4  V5  V6204506 190371569   3 0.00 \\N \\N \\N299278 269383932  44 0.00 \\N \\N \\N11126     391013  17 0.07 \\N \\N   172460     470178  81 0.00 \\N \\N \\N294043    730783  32 0.00 \\N \\N \\N334309 269294774 153 0.00 \\N \\N \\N
假若新的x2 有两个因子水平,我如果想看每个因子水平对应的数量,应该使用table(X)

计算列联表的数量,如果想看比例呢? 使用prop.table(table(X)) 计算每个因子所占的比例。

3.等比例抽样createDataPartition(x, p=, times=  ,list= )

install.packages("caret")library("caret")
> head(iris)  Sepal.Length Sepal.Width Petal.Length Petal.Width Species1          5.1         3.5          1.4         0.2  setosa2          4.9         3.0          1.4         0.2  setosa3          4.7         3.2          1.3         0.2  setosa4          4.6         3.1          1.5         0.2  setosa5          5.0         3.6          1.4         0.2  setosa6          5.4         3.9          1.7         0.4  setosa> createDataPartition(iris$Species,times=1,p=0.1)$Resample1 [1]   9  10  11  43  44  60  62  75  80  92 102 115 118 140 141
函数中,times 表示抽几组,p表示抽取的比例。list 表示是否为列表形式。

若写成list=TRUE, 则后面提取时应写$.

> x<-createDataPartition(iris$Species,p=0.1,times=1,list=FALSE)
> head(iris[x,])   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species7           4.6         3.4          1.4         0.3     setosa30          4.7         3.2          1.6         0.2     setosa35          4.9         3.1          1.5         0.2     setosa41          5.0         3.5          1.3         0.3     setosa43          4.4         3.2          1.3         0.2     setosa56          5.7         2.8          4.5         1.3 versicolor
> x1<-createDataPartition(iris$Species,p=0.1,times=1,list=TRUE)> iris[x1,]Error in xj[i] : invalid subscript type 'list'
> head(iris[x1$Resample1,])   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species1           5.1         3.5          1.4         0.2     setosa11          5.4         3.7          1.5         0.2     setosa12          4.8         3.4          1.6         0.2     setosa27          5.0         3.4          1.6         0.4     setosa30          4.7         3.2          1.6         0.2     setosa51          7.0         3.2          4.7         1.4 versicolor














0 0
原创粉丝点击