R语言 计算机 sample()是什么意思?随机取样?

Python018

R语言 计算机 sample()是什么意思?随机取样?,第1张

大致相当于排列组合中的组和,

官方的函数解释:sample(x,size[,replace=T/F]):随机在向量x中抽取size个元素,选项replace=T允许放回抽取,replace=F则为一次性抽取。

这里有个函数参考的帖子:

http://www.douban.com/note/294226439/

样本不均衡问题是指在机器学习分类任务中,不同类型的样本占比差距悬殊。

比如训练数据有100个样本,其中只有5个正样本,其余均为负样本,这样正样本:负样本=5%:95%,训练数据中负样本过多,会导致模型无法充分学习到正样本的信息,这时候模型的正确率往往较高,但特异性却很低,即模型识别正样本的能力很差。这样的模型是无法投入实际项目中的,我们需要解决不均衡问题带来的影响。

解决样本不均衡,采用的方法是重采样。根据采样的方法,分为欠采样、过采样和组合采样。

在R语言中, ROSE 包用于处理样本不均衡问题。

安装包

加载示范数据,查看列联表。可以看到训练数据 hacide.train 出现了样本不均衡,正样本1只有20个,负样本0有980个。

欠采样会缩小训练数据。训练数据虽然正负样本均衡了,但由于原始的正样本很少,导致处理后总样本数减少很多。这个方法适用于训练数据很大,且正样本也较大的情况,可以用欠采样来减少训练数据规模,提高训练速度。

过采样会增大训练数据。该方法适用于训练数据中正样本数量较少的情况。

组合采样会同时增加正样本和减少负样本。参数 N 表示处理后样本总数,一般设置为训练数据样本数。

不均衡样本对模型的训练结果会产生较大偏差,以实际分类问题为例,对比一下处理与不处理均衡样本的结果。

从预测的结果来看,均衡处理与不均衡处理的模型准确率都很高,都超过了98%,貌似模型都很好。但均衡处理后模型的特异性达到了73.68%,未采用均衡处理的模型只有36.84%,显然 均衡处理能提高模型的特异性

处理样本不均衡问题是做分类问题不可或缺的的一步,针对训练数据的情况,可以采用不同的均衡处理方法。均衡处理的目的是尽可能多的且高效的利用训练数据里的信息,不至于后续训练出的模型学习的不够充分,出现较大偏差。均衡处理对于既要求准确率高,又要求特异性高的模型来说尤为重要。

R语言基本数据分析

本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。

不多说,直接上代码,代码中有注释。

1. 基本作图(盒图,qq图)

#basic plot

boxplot(x)

qqplot(x,y)

2. 线性拟合

#linear regression

n = 10

x1 = rnorm(n)#variable 1

x2 = rnorm(n)#variable 2

y = rnorm(n)*3

mod = lm(y~x1+x2)

model.matrix(mod) #erect the matrix of mod

plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance

summary(mod) #get the statistic information of the model

hatvalues(mod) #very important, for abnormal sample detection

3. 逻辑回归

#logistic regression

x <- c(0, 1, 2, 3, 4, 5)

y <- c(0, 9, 21, 47, 60, 63) # the number of successes

n <- 70 #the number of trails

z <- n - y #the number of failures

b <- cbind(y, z) # column bind

fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model

print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]

beta1 <- fitx$coef[2]

fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))

par(new=T)

curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve

3. Bootstrap采样

# bootstrap

# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution

dat = matrix(rnorm(100*5),100,5)

no.samples = 200 #sample 200 times

# theta = matrix(rep(0,no.samples*5),no.samples,5)

theta =rep(0,no.samples*5)

for (i in 1:no.samples)

{

j = sample(1:100,100,replace = TRUE)#get 100 samples each time

datrnd = dat[j,]#select one row each time

lambda = princomp(datrnd)$sdev^2#get eigenvalues

# theta[i,] = lambda

theta[i] = lambda[1]/sum(lambda)#plot the ratio of the biggest eigenvalue

}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue

hist(theta)#plot the percentage distribution of the biggest eigenvalue

sd(theta)#standard deviation of theta

#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能

4. ANOVA方差分析

#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)

#

y = rnorm(9)#weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入

#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)

Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group

mod = lm(y~Treatment) #linear regression

print(anova(mod))

#解释:Df(degree of freedom)

#Sum Sq: deviance (within groups, and residuals) 总偏差和

# Mean Sq: variance (within groups, and residuals) 平均方差和

# compare the contribution given by Treatment and Residual

#F value: Mean Sq(Treatment)/Mean Sq(Residuals)

#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)

qqnorm(mod$residual) #plot the residual approximated by mod

#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)

如下面两图分别是

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9)

的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。