使用Numpy打乱数组或打乱矩阵行

作者:coasxu 时间:2022-09-18 09:40:10 

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]])
"""

来源:https://blog.csdn.net/weixin_44633882/article/details/105307076

标签:Numpy,打乱,数组,矩阵行
0
投稿

猜你喜欢

  • 主页移动背景代码

    2009-11-16 17:54:00
  • python解析xml文件方式(解析、更新、写入)

    2022-03-07 05:19:01
  • 神经网络python源码分享

    2021-10-07 10:41:00
  • Flask缓存静态文件的具体方法

    2023-04-12 18:27:43
  • Python微信公众号开发平台

    2021-11-25 20:50:12
  • python实现爱奇艺登陆密码RSA加密的方法示例详解

    2022-02-10 00:41:53
  • python merge、concat合并数据集的实例讲解

    2023-01-23 16:30:38
  • Python新手实现2048小游戏

    2021-02-19 14:12:57
  • 利用Python将多张图片合成视频的实现

    2022-09-16 07:10:26
  • OpenCV半小时掌握基本操作之图像梯度

    2022-05-29 00:48:03
  • Python远程视频监控程序的实例代码

    2021-02-08 14:26:22
  • python交换两个变量的值方法

    2022-07-28 10:10:48
  • python orm 框架中sqlalchemy用法实例详解

    2021-04-22 18:03:28
  • Python运算符之Inplace运算符的使用教程

    2021-09-24 11:32:10
  • python+django快速实现文件上传

    2021-03-12 19:07:38
  • C#中的委托和事件

    2007-09-26 20:50:00
  • python自动化运维之Telnetlib的具体使用

    2022-09-24 22:11:16
  • 解决缩小图标变样问题

    2007-10-08 19:13:00
  • python计算日期之间的放假日期

    2021-08-14 00:02:42
  • Python-接口开发入门解析

    2022-05-24 09:19:43
  • asp之家 网络编程 m.aspxhome.com