使用Numpy打乱数组或打乱矩阵行
更新时间:2023年05月10日 09:30:45 作者:coasxu
这篇文章主要介绍了使用Numpy打乱数组或打乱矩阵行问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
numpy打乱数组或打乱矩阵行
使用numpy.random.shuffle函数,能够打乱ndarray对象的第一维度,对于数组来说,就是整体被打乱。
对于矩阵来说,第一维度行被打乱。可以在打乱训练数据或测试模型性能的时候使用。
- Parameters: x: array_like
- Returns: None
e.g.
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [9, 1, 2, 7, 5, 3, 0, 8, 4, 6]
多维数组
>>> arr = np.arange(9).reshape((3, 3)) # array([[0, 1, 2], # [3, 4, 5], # [6, 7, 8]]) >>> np.random.shuffle(arr) >>> arr array([[0, 1, 2], [6, 7, 8], [3, 4, 5]])
numpy.random.shuffle打乱数组或者列表的顺序
numpy.random.shuffle
注:打乱数组时,只沿着多维数组的第一个轴移动数组。子数组的顺序改变了,但它们的内容保持不变.
shuffle(x)
Modify a sequence in-place by shuffling its contents. This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same. Parameters ---------- x : array_like The array or list to be shuffled. Returns ------- None Examples -------- >>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] Multi-dimensional arrays are only shuffled along the first axis: >>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], [6, 7, 8], [0, 1, 2]]) """
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Python中使用第三方库xlutils来追加写入Excel文件示例
这篇文章主要介绍了Python中使用第三方库xlutils来追加写入Excel文件示例,本文直接给出追加写入示例和追加效果,需要的朋友可以参考下2015-04-04Python tkinter之ComboBox(下拉框)的使用简介
这篇文章主要介绍了Python tkinter之ComboBox(下拉框)的使用简介,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下2021-02-02研究Python的ORM框架中的SQLAlchemy库的映射关系
这篇文章主要介绍了研究Python的ORM框架中的SQLAlchemy库的映射关系,SQLAlchemy库是一个常见的Python中操作数据库的工具,需要的朋友可以参考下2015-04-04python re正则表达式模块(Regular Expression)
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析、复杂字符串分析和信息提取时是一个非常有用的工具.2014-07-07
最新评论