python工具包:random的注意事项
问题
我们知道在由于random包中生成的是伪随机数,因此通过设置seed可以固定住随机结果。
但有一次使用时发现设置完seed之后结果依然改变:
代码如下:
19 random.seed(args.seed)2021 # read all filenames in list files22 with open(args.file, 'r') as f:23 ¦ filenames = f.readlines()24 filenames = [item.replace('\n', '') for item in filenames]25 filenames.sort()26 random.shuffle(filenames)27 print('total filenames: {}'.format(len(filenames)))28 print('==== filenames[:4]', filenames[:4])2930 # select patient, if None, all patients are selected31 patient_ID = [filename[:4] for filename in filenames]32 set_patient_ID = set(patient_ID)33 print('total {} patients'.format(len(set_patient_ID)))34 selected_patient_ID = list(set_patient_ID)35 #selected_patient_ID.sort()36 selected_patient_ID = selected_patient_ID[:args.patient_num]37 #selected_patient_ID = list(set_patient_ID)[:args.patient_num]38 print('select {} pathents'.format(len(selected_patient_ID)))39 print('selected pathent IDs:\n', selected_patient_ID)40 print('-'*100)41 print('===== selected_patient_ID[:4]', selected_patient_ID[:4])
解决方法
我们发现28行每次输出固定,但是41行每次输出不固定。
查找原因,发现是第32行每次将list改为set之后,会被自动排序,但还是没理解为什么会导致出现了随机。
解决方法是增加第35行代码即可,如下:
19 random.seed(args.seed)2021 # read all filenames in list files22 with open(args.file, 'r') as f:23 ¦ filenames = f.readlines()24 filenames = [item.replace('\n', '') for item in filenames]25 filenames.sort()26 random.shuffle(filenames)27 print('total filenames: {}'.format(len(filenames)))28 print('==== filenames[:4]', filenames[:4])2930 # select patient, if None, all patients are selected31 patient_ID = [filename[:4] for filename in filenames]32 set_patient_ID = set(patient_ID)33 print('total {} patients'.format(len(set_patient_ID)))34 selected_patient_ID = list(set_patient_ID)35 selected_patient_ID.sort()36 selected_patient_ID = selected_patient_ID[:args.patient_num]37 #selected_patient_ID = list(set_patient_ID)[:args.patient_num]38 print('select {} pathents'.format(len(selected_patient_ID)))39 print('selected pathent IDs:\n', selected_patient_ID)40 print('-'*100)41 print('===== selected_patient_ID[:4]', selected_patient_ID[:4])
python工具包:random的注意事项相关推荐
- 机器学习模型可解释性的6种Python工具包,总有一款适合你!
开发一个机器学习模型是任何数据科学家都期待的事情.我遇到过许多数据科学研究,只关注建模方面和评估,而没有解释. 然而,许多人还没有意识到机器学习可解释性在业务过程中的重要性.以我的经验,商业人士希望知 ...
- 优化算法 | 随机漂移粒子群优化算法(附标准PSO算法Python工具包)
目录 ▎标准PSO算法 ▎RDPSO算法 ▎标准PSO算法Python工具包 01 | 工具包适用范围 02 | 工具包安装方法 03 | 工具包使用实例 ▎参考文献 今天为各位讲解一种改进的粒子群优 ...
- 掷骰子python代码_如何使用python的random模拟掷骰子的数据
python是一个很有趣的语言,可以在命令行窗口运行.python支持面向对象的编程方法,这篇经验就告诉你,如何使用python的random模块,模拟掷骰子的数据. 工具/原料 windows系统电 ...
- python中random模块中包含了随机数相关的功能函数_Python中random模块生成随机数详解...
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20 print random.randint(20, 20) #结果永远是20 #pr ...
- 从洗牌算法谈起--Python的random.shuffle函数实现原理
此文首发于我的个人博客:从洗牌算法谈起–random.shuffle实现原理 - zhang0peter的个人博客 昨天看知乎的时候看到了洗牌算法(Knuth shuffle, 最初版本叫Fisher ...
- python中sample是什么意思_基于Python中random.sample()的替代方案
python中random.sample()方法可以随机地从指定列表中提取出N个不同的元素,但在实践中发现,当N的值比较大的时候,该方法执行速度很慢,如: numpy random模块中的choice ...
- python的random模块生成随机数
python的random函数 random.random() 生成0-1之间的随机数 random.uniform(a,b)生成a,b之间的浮点数 random.randint(a,b)生成a,b之 ...
- python的random函数_关于random()的详细介绍
这篇文章主要介绍了Python随机生成数模块random使用实例,本文直接给出示例代码,需要的朋友可以参考下代码如下:#!/usr/bin/env python#coding=utf-8import ...
- python工具包_python 工具包
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! hello,大家好! rose小哥今天主要介绍一下常见用来进行脑成像数据分析的p ...
- python random包含尾部吗_你真的了解Python的random模块吗?
用于生成伪随机数 源码位置: Lib/random.py(看看就好,千万别随便修改) 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结果是不可预测的,是 ...
最新文章
- javaweb实现mysql备份功能_java web 实现mysql 数据库备份、恢复
- [导入]VB实现SQL Server数据库备份/恢复
- 前端学习(2159):vuecli脚手架的配置和安装
- # Schedulerx正式登陆Ali-k8s应用目录
- python新建txt文件,并逐行写入数据
- Ubuntu上安装rvm
- 4-2 数据模型的生成
- mysql安装包配置方式_mysql安装包方式安装及配置(Windows)
- keil+flymcu+CubeMX开发STM32F407
- 黑马程序员视频-微信小程序-原生框架——项目搭建
- sqluldr2 用法简述
- webgl存本地文件_Unity发布WebGL后加载本地文件
- IDEA 更改配色和主题样式
- 未来房价涨or跌?大数据告诉你
- Java直接AXIS调用远程WebService
- SPSS(二)SPSS实现多因素方差分析模型(图文教程+数据集)
- 鲍姆-韦尔奇算法求解HMM参数
- Commvault资源集合(20191009)
- C语言中将数字转换为字符串的方法
- 当所有人都站着吆喝,我选择退一步坐下