python实现的分层随机抽样案例
作者:三大爷的键 时间:2022-06-07 16:10:42
昨天写了一段用来做分层随机抽样的代码,很粗糙,不过用公司的2万名导购名单试了一下,结果感人,我觉得此刻的我已经要上天了,哈哈哈哈哈哈
代码如下:
#分层随机抽样 stratified sampling
import xlrd, xlwt, time, random
xl = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\分层抽样.xlsx')
xl_sht1 = xl.sheets()[0]
xl_sht1_nrows = xl_sht1.nrows
#表头
title = xl_sht1.row_values(0)
#把样本写进列表 sample
sample = []
for i in range(xl_sht1_nrows):
sample.append(xl_sht1.row_values(i))
#打乱样本
random.shuffle(sample)
#把层的内容写进列表 col
col = xl_sht1.col_values(0)
#对col中的内容进行计数,获得每一类的名称对应个数的字典
col_dict = {}
for i in col:
col_dict[i] = col_dict.get(i, 0) + 1
p = eval(input('每层抽取的比例(小数):'))
#获得每一类的名称对应抽取个数的字典
col_p = {}
k = 0
for i in col_dict.keys():
col_p[i] = int(round(col_dict[i] * p)) #round用来四舍五入,不加int结果会变成无数个p
#开始抽样,把抽取结果写进result_l列表
result_l = []
for i in sample:
if col_p.get(i[0], 0) > 0:
result_l.append(i)
col_p[i[0]] -= 1
#新建文档,写入结果
f = xlwt.Workbook()
f_sht1 = f.add_sheet('result')
n = 0
for i in title:
f_sht1.write(0, n, i)
n += 1
r = 1
for i in result_l:
c = 0
for k in i:
f_sht1.write(r, c, k)
c += 1
r += 1
nowtime = time.strftime("%Y%m%d_%H%M%S", time.localtime())
f.save(r'C:\Users\Administrator\Desktop\Result_{0}.xls'.format(nowtime))
print('\n抽样成功!')
input()
原理思路如下:
1、首先数据的形式如下
数据自带层标签,我只需要从每层抽取一定比例的数据就可以了
2、第一步:先将源数据按行存入一个列表,然后打乱行的顺序
3、第二步:对层标签进行计数,获取每一个层标签的总数,并将结果存进字典
4、第三步:根据抽样比例p,计算出每层实际应抽取的个数,并存入一个新的字典
5、第四步:从源数据列表中抽取出目标数据,每层抽取的个数由第三步的字典进行指定
小白刚刚起步,实现的时候发现自己对数据结构和算法一点也不懂。。。。结果就写了一堆的for循环。。。。
大神们如果看到这段代码,还请不吝赐教,看看代码可以怎样优化,或者有更好的设计思路
补充拓展:pandas实现对dataframe抽样的实现
随机抽样
import pandas as pd
#对dataframe随机抽取2000个样本
pd.sample(df, n=2000)
分层抽样
利用sklean中的函数灵活进行抽样
from sklearn.model_selection import train_test_split
#y是在X中的某一个属性列
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, stratify=y)
来源:https://blog.csdn.net/highmiao_19/article/details/71791354
标签:python,随机,抽样
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Keras 使用 Lambda层详解
2021-08-11 20:12:40
![](https://img.aspxhome.com/file/2023/0/105710_0s.jpg)
解决Pandas生成Excel时的sheet问题的方法总结
2021-01-13 14:24:44
![](https://img.aspxhome.com/file/2023/6/87486_0s.png)
python实现逻辑回归的示例
2022-04-05 05:16:59
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
2021-10-28 22:36:17
利用python/R语言绘制圣诞树实例代码
2021-02-10 02:32:23
![](https://img.aspxhome.com/file/2023/5/93715_0s.png)
Python切片操作实例分析
2022-05-02 17:49:25
简化的CSS Reset — 常用CSS重设实例
2009-03-11 16:46:00
python flask实现分页的示例代码
2021-04-01 09:55:23
python中format的用法实例详解
2023-08-03 12:27:28
根据Dreamweaver里的ToolTip代码改进的提示框
2008-11-27 12:19:00
使用python切片实现二维数组复制示例
2021-11-02 22:24:41
教你如何升级SQL Server数据库系统
2009-01-19 14:42:00
Python面向对象实现一个对象调用另一个对象操作示例
2023-08-30 22:54:35
Python3 venv搭建轻量级虚拟环境的步骤(图文)
2022-11-26 08:47:16
![](https://img.aspxhome.com/file/2023/5/68385_0s.jpg)
利用python在excel里面直接使用sql函数的方法
2023-10-15 00:34:57
![](https://img.aspxhome.com/file/2023/1/81781_0s.jpg)
基于Python实现定时自动给微信好友发送天气预报
2023-09-13 01:24:05
![](https://img.aspxhome.com/file/2023/5/61765_0s.jpg)
Python+OpenCV实现阈值分割的方法详解
2023-08-13 02:24:00
![](https://img.aspxhome.com/file/2023/0/77910_0s.jpg)
Facebook开源一站式服务python时序利器Kats详解
2023-11-13 18:29:13
![](https://img.aspxhome.com/file/2023/7/62877_0s.png)
Google的产品设计指导思想
2008-03-23 14:15:00
![](https://img.aspxhome.com/file/UploadPic/20083/23/2008323141844207s.jpg)
Python GUI编程 文本弹窗的实例
2022-08-24 02:07:48