R语言中常用的抽样函数
简单随机抽样
简单随机抽样分为有放回抽样和无放回抽样,这两种形式都可以通过base包中的sample()函数实现。
sample(x, size, replace = FALSE, prob =NULL)
x: 带抽取对象,若为整数则表示从1-n的整数中抽取,特别注意如果x为数据库抽取的是列而非行
size: 想要抽取的样本数量
replace: 是否为有放回,默认为FALSE,即无放回
prob: 设置个抽取样本的抽样概率,默认为无取值,即等概率抽样
实例:
sample(x=6,size=3,replace = F, prob=c(0.1,0.2,0.3,0.2,0.1,0.1))
# [1] 2 3 6
分层抽样
分层抽样可以通过sampling包中的strata()函数实现。
strata(data, stratanames=NULL, size, method=c("srswor","srswr","poisson","systematic"),pik,description=FALSE)
data: 带抽样数据
stratanames: 进行分层所依据的变量名称
size: 各层中要抽出的观测样本数
method: 选择4中抽样方法,分别为无放回、有放回、泊松、系统抽样,默认为srswor
pik: 设置各层中样本的抽样概率
description: 选择是否输出含有各层基本信息的结果
注意每一层都是无放回抽样。
library(sampling)
df=data.frame(x=c(1,2,2,3,3,4),api=c('index','index','logout','show','show','index'))sub2=strata(df, stratanames = 'x',size=c(1,2,1,1), method='srswor',description=T)
# Stratum 1
#
# Population total and number of selected units: 1 1
# Stratum 2
#
# Population total and number of selected units: 2 2
# Stratum 3
#
# Population total and number of selected units: 2 1
# Stratum 4
#
# Population total and number of selected units: 1 1
# Number of strata 4
# Total number of selected units 5
sub2
# x ID_unit Prob Stratum
# 1 1 1 1.0 1
# 2 2 2 1.0 2
# 3 2 3 1.0 2
# 4 3 4 0.5 3
# 6 4 6 1.0 4
整群抽样
cluster(data, clustername, size,method=c("srswor","srswr","poisson","systematic"),pik,description=FALSE)
data: 带抽样数据
clustername: 用来划分群的变量名称
size:需要抽取的群数
method: 选择4中抽样方法,分别为无放回、有放回、泊松、系统抽样,默认为srswor
pik: 设置各层中样本的抽样概率
description: 选择是否输出含有各层基本信息的结果
实例:
library(sampling)
df=data.frame(x=c(1,2,2,3,3,4),api=c('index','index','logout','show','show','index'))sub3=cluster(df, clustername = 'x',size=2, method='srswor',description=T)
# Number of selected clusters: 2
# Number of units in the population and number of selected units: 6 3
sub3
# x ID_unit Prob
# 1 1 1 0.5
# 2 3 4 0.5
# 3 3 5 0.5
.
训练集与测试集的分割
训练集和测试集的分割在模型训练中经常使用,因此怎样用高效的代码实现很重要。
R语言实现:
df=data.frame(x=1:10,y=paste0('n',1:10))
train_ind=sample(x=nrow(df),size=7,replace = F)
train_set=df[train_ind,]
train_set
# x y
# 9 9 n9
# 5 5 n5
# 3 3 n3
# 7 7 n7
# 10 10 n10
# 8 8 n8
# 4 4 n4
test_set=df[-train_ind,]
test_set
# x y
# 1 1 n1
# 2 2 n2
# 6 6 n6
python语言实现:
python语言中没有像R这么方便的索引方式,但是提供了sklearn包中的分割函数。
基础代码的实现
import pandas as pd
import numpy as np
y=["".join(('n',str(_))) for _ in range(1,11)]
df=pd.DataFrame({'x':range(1,11),'y':y})
train_ind=np.random.choice(range(10),size=7,replace=False)
test_ind=np.array(list(set(range(10))-set(train_ind)))
train_set=df.iloc[train_ind,]
# x y
# 3 4 n4
# 4 5 n5
# 0 1 n1
# 8 9 n9
# 9 10 n10
# 5 6 n6
# 6 7 n7
test_set=df.iloc[test_ind,]
# x y
# 1 2 n2
# 2 3 n3
# 7 8 n8
sklearn包的实现
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size, random_state)
test_size 为测试集占比,如果为整数就代表样本的数量
random_state 为随机数种子
x=range(1,11)
y=["".join(('n',str(_))) for _ in range(1,11)]
X_train, X_test, y_train, y_test = train_test_split(x,y, test_size=0.3, random_state=10)
X_train
# [10, 2, 7, 8, 4, 1, 6]
X_test
# [3, 9, 5]
y_train
# ['n10', 'n2', 'n7', 'n8', 'n4', 'n1', 'n6']
y_test
['n3', 'n9', 'n5']
参考来源:
1.sklearn官网https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html#sklearn.model_selection.train_test_split
2.https://blog.csdn.net/u014460433/article/details/52756752
R语言中常用的抽样函数相关推荐
- c语言中常用的字符函数以及字符串函数
文章目录 前言 一.常用字符串函数 1.strlen() 2.strcpy() 3.strcat() 4.strcmp() 5.strstr() 6.memcpy() 6.memmove() 二.qs ...
- c/c++语言中常用的math函数
在实用math库函数之前,需要引用头文件 include<math.h> 常用库函数如下: 1.fabs(double X) 该函数用于对double型取绝对值 如果用于float会损失精 ...
- mysql sql总计函数_请问select SQL 语言中常用的合计函数有哪些?
1.sql中sum和count的区别 ----sql聚合函数 (1)首先,sum是对一个字段求和,hive中字段的类型一般是string或者是int,如果是int当然没有问题,如果是string类型但 ...
- C语言中常用字符串处理函数(总结大全)
目录 字符串处理函数 1.char *gets(char *s); 2. char *fgets(char *s, intsize, FILE *stream); 3. int puts(const ...
- C语言中常用的数学函数
① double exp(double x); (求e的x次幂) ② double pow(double x, double y); (求x的y次幂) ③ double sqrt(double x); ...
- bind merge r 和join_R语言中的数据合并函数(merge,cbind和rbind)的使用
R语言中的数据合并函数(merge,cbind和rbind)的使用-R语言中用cbind() 和rbind() 构建分块矩阵 1.merge函数 两个数据框拥有相同的时间或观测值,但这些列却不尽相同. ...
- r语言中正定矩阵由于误差不正定_R语言之数据处理(一)
在上一篇小文中,提到了关于R语言导入数据的一些方法,之后的重点就转向了数据的处理上.数据处理其实在整个数据分析项目中所占用的时间是比较多的,所以根据处理的目的不同,也有不同的处理方法.在R语言中,我通 ...
- 技巧 | 如何使用R语言的常用工具包绘制双变量填充地图
之前本号转载了DataCharm公众号的一篇推送: 转载 | 双变量映射地图可视化绘制方法 这篇推送使用了biscale工具包绘制了双变量填充地图.近来,小编发现使用常用的绘图工具包也能很便捷的绘制这 ...
- c语言中有裁剪字符串的函数吗,C语言中的字符串截取函数
/*======================================================== 子数整数 源程序名 num.??? (pas,c,cpp) 可执行文件名 num. ...
- R语言字符串拼接(paste()函数)
R语言中常使用paste()函数进行字符串拼接,paste()函数可以将任意数量的参数组合在一起. 1. 语法 代码 参数 paste(-, sep = " ", collapse ...
最新文章
- WebGame 客户端 美术资源处理之PNG批量导出SWF
- [PHP] - 性能加速 - 开启opcache
- JSP——JSTL语法总结
- C语言结构体与联合体
- B - 一只小蜜蜂...
- REVERSE-PRACTICE-BUUCTF-9
- 奔跑吧兄弟变成机器人是哪一期_奔跑吧预告,郑恺郭麒麟回归,而我却被女嘉宾的颜值吸引了...
- 三维数据平滑处理_关于CAD三维对象建模
- linux系统终端more,一篇文章让你学透Linux系统中的more命令
- 梅森旋转产生随机数c语言实现,梅森旋转法产生随机数
- 光电隔离RS485典型电路
- 智能手机基于众包的室内定位
- web前端-纯前端音频剪辑,vue音频编辑组件
- unraid应用_unraid 篇三:unraid docker之网页文件管理,强迫症的福音
- 向下取整符号_22. 为什么 Python 中的整除是向下取整?
- 百度的文心一言是否可以打败ChatGPT?
- 运行中的线程如何停止?
- HTTP HTTPS
- 海思3559A平台4GB LPDDR配置方案
- 数据平台初试(产品篇)——短视频直播监测案例分享
热门文章
- linux可以用tab键,linux下tab键在命令行情况下的强大
- git clone 码云仓库项目报错fatal: Authentication failed for ‘https://gitee.com/...‘
- 整车电子电气架构EEA
- java cmyk转rgb_图片 CMYK转RGB 代码
- 如何设计出一款好的软件
- php微信发送客服消息,微信公众号利用客服消息和模板消息实现微信群发
- 软件工程中如何设计测试用例
- 吹塑模具和注塑模具的区别
- web前端网页设计期末课程大作业:旅游网页主题网站设计——紫色的旅游开发景点网站静态模板(4页)HTML+CSS+JavaScript
- win10卸载office2010卸载途中就自动重启重复出现