• 【Python数据科学快速入门系列 | 02】创建ndarray对象的十多种方法


    这是机器未来的第39篇文章

    原文首发地址:https://blog.csdn.net/RobotFutures/article/details/126092621

    在这里插入图片描述

    1. np.array方法创建

    将列表或元组转换为ndarray数组

    • 创建一维数组
    import numpy as np
    arr = np.array([1,2,3,4,5])  # 创建一维数组
    arr
    
    • 1
    • 2
    • 3
    array([1, 2, 3, 4, 5])
    
    • 1
    • 创建二维数组,并且指定数据类型
    arr2 = np.array([[1,2,3,4],[5,6,7,8]], dtype=float)  # 创建二维数组
    arr2
    
    • 1
    • 2
    array([[1., 2., 3., 4.],
           [5., 6., 7., 8.]])
    
    • 1
    • 2
    • 创建高维数组
    arr3 = np.array([[[1]]])            # 创建三维数组
    arr3
    
    • 1
    • 2
    array([[[1]]])
    
    • 1
    print(f"arr_ndim:{arr.ndim}, arr2_ndim:{arr2.ndim}, arr3_ndim:{arr3.ndim}")
    print(f"arr_shape:{arr.shape}, arr2_shape:{arr2.shape}, arr3_shape:{arr3.shape}")
    
    • 1
    • 2
    arr_ndim:1, arr2_ndim:2, arr3_ndim:3
    arr_shape:(5,), arr2_shape:(2, 4), arr3_shape:(1, 1, 1)
    
    • 1
    • 2

    2 根据规则创建ndarray对象的方法

    2.1 等差数列linespace

    linespace函数常用来生成matplot图标展示的X轴。

    x1 = np.linspace(start=1,stop=10,num=10)
    start:起始值,stop:结束值(包含),num:元素个数

    x1 = np.linspace(start=1,stop=10,num=10)
    x1
    
    • 1
    • 2
    array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
    
    • 1

    2.2 生成等差数列

    x2 = np.arange(start=1,stop=100,step=3)
    可以指定步长step

    x2 = np.arange(start=1,stop=100,step=3)
    x2
    
    • 1
    • 2
    array([ 1,  4,  7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49,
           52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97])
    
    • 1
    • 2

    2.3 生成值全为1的数组

    x = np.ones(shape=(3,3))
    x
    
    • 1
    • 2
    array([[1., 1., 1.],
           [1., 1., 1.],
           [1., 1., 1.]])
    
    • 1
    • 2
    • 3

    2.4 生成值全为0的数组

    np.zeros(shape=(3,3))
    
    • 1
    array([[0., 0., 0.],
           [0., 0., 0.],
           [0., 0., 0.]])
    
    • 1
    • 2
    • 3

    2.5 按照x2的形状,生成值全为1的数组

    
    x2 = np.arange(start=1,stop=100,step=3)
    print(x2)
    x2_1 = np.ones_like(x2)
    print(x2_1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [ 1  4  7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70
     73 76 79 82 85 88 91 94 97]
    [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
    
    • 1
    • 2
    • 3

    2.6 按照x2生成值全为0的数组

    
    np.zeros_like(x2)
    
    • 1
    • 2
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    • 1
    • 2

    2.7 对角矩阵

    生成指定ndarray对象的对角矩阵

    x2 = np.linspace(start=1, stop=64,num=64)
    x2 = x2.reshape(8, 8)
    print(x2)
    np.diag(x2)
    
    • 1
    • 2
    • 3
    • 4
    [[ 1.  2.  3.  4.  5.  6.  7.  8.]
     [ 9. 10. 11. 12. 13. 14. 15. 16.]
     [17. 18. 19. 20. 21. 22. 23. 24.]
     [25. 26. 27. 28. 29. 30. 31. 32.]
     [33. 34. 35. 36. 37. 38. 39. 40.]
     [41. 42. 43. 44. 45. 46. 47. 48.]
     [49. 50. 51. 52. 53. 54. 55. 56.]
     [57. 58. 59. 60. 61. 62. 63. 64.]]
    
    array([ 1., 10., 19., 28., 37., 46., 55., 64.])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    可以看到diag仅提取了8*8矩阵的左上右下对角线的值。

    2.8 单位矩阵

    生成指定形状的单位矩阵。

    np.eye(8)
    
    • 1
    array([[1., 0., 0., 0., 0., 0., 0., 0.],
           [0., 1., 0., 0., 0., 0., 0., 0.],
           [0., 0., 1., 0., 0., 0., 0., 0.],
           [0., 0., 0., 1., 0., 0., 0., 0.],
           [0., 0., 0., 0., 1., 0., 0., 0.],
           [0., 0., 0., 0., 0., 1., 0., 0.],
           [0., 0., 0., 0., 0., 0., 1., 0.],
           [0., 0., 0., 0., 0., 0., 0., 1.]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3. 随机数生成

    3.1 生成正态分布的随机数组

    np.random.normal(loc=0.0, scale=1.0, size=None)

    输入:
    loc - 均值,可以为单个整数,也可以为数组,当为数组时指定每列的均值
    scale - 标准差,可以为单个整数,也可以为数组,当为数组时指定每列的标准差
    size - 数据的shape
    
    返回:
    ndarray对象
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    np.random.normal(3, 2.5, size=(2, 4))
    
    • 1
    array([[ 4.99637559,  4.41112766, -4.46850171,  2.34375326],
           [ 7.80167697,  4.88896638,  4.50739853,  1.91669014]])
    
    • 1
    • 2
    import numpy as np
    from matplotlib import pyplot as plt
    
    # loc-均值,scale-标准差,size-随机数组的数据形状shape
    y = np.random.normal(loc=0.0, scale=1.0, size=1000)
    x = np.linspace(0, 100, num=1000)
    
    plt.hist(x=y, bins=50)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9


    png

    3.2 生成均匀分布的随机数组

    np.random.uniform(low=0.0, high=1.0, size=None)

    输入:
    low - 最小值,可以为单个整数,也可以为数组,当为数组时指定每列的最小值
    high - 最大值,范围是开区间[low,high),可以为单个整数,也可以为数组,当为数组时指定每列的最大值
    size - 数据的shape
    
    数据类型为float
    
    返回:
    ndarray对象
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    import numpy as np
    
    x = np.random.uniform(size=(3,3))
    x
    
    • 1
    • 2
    • 3
    • 4
    array([[0.55604474, 0.23922789, 0.49744522],
           [0.72736682, 0.50886455, 0.89055923],
           [0.86007017, 0.45767947, 0.64721748]])
    
    • 1
    • 2
    • 3
    import numpy as np
    from matplotlib import pyplot as plt
    
    y = np.random.uniform(size=(10000))
    x = np.linspace(0, 100, num=10000)
    
    plt.hist(x=y, bins=36)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8


    png

    3.3 生成随机分布的数组

    np.random.random(size=None)
    等同于np.random.uniform(size=None),即取默认low和high的uniform, 它是random_sample的别名/简称

    输入:
    size - 数据的shape
    数据区间为: [0.0, 1.0)
    数据类型为float
    
    返回:
    ndarray对象
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    x = np.random.random(size=(3,3))
    x
    
    • 1
    • 2
    array([[0.81853323, 0.68854024, 0.93096203],
           [0.73975546, 0.48552889, 0.62078013],
           [0.08407448, 0.16430265, 0.08354929]])
    
    • 1
    • 2
    • 3
    import numpy as np
    from matplotlib import pyplot as plt
    
    y = np.random.random(size=100000)
    x = np.linspace(0, 100, num=100000)
    
    plt.hist(x=y, bins=36)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8


    png

    随机分布达到一定的数据量之后,可以看到也是均匀分布的。

    另外,np.random.rand也可以实现和np.random.random一样的功能,连size=()都不用写,直接填入数据形状即可。

    x = np.random.rand(3,3)
    x
    
    • 1
    • 2
    array([[0.48217616, 0.45697736, 0.51243651],
           [0.42060063, 0.12191877, 0.11430536],
           [0.43719726, 0.88747028, 0.63576976]])
    
    • 1
    • 2
    • 3

    3.4 生成均匀分布的整数数组

    np.random.randint(low, high=None, size=None, dtype=int)

    输入:
    low - 最小值,必须填写,可以为单个整数,也可以为数组,当为数组时指定每列的最小值
    high - 最大值,可以为单个整数,也可以为数组,当为数组时指定每列的最大值
    size - 数据形状
    
    数据区间为: [low, high),如果要实现[low, high]闭区间,则使用random_integers
    数据类型可选多种整型
    
    返回:
    ndarray对象
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    x = np.random.randint(low=0, high=10, size=(3,3), dtype=np.uint8)
    x
    
    • 1
    • 2
    array([[5, 6, 4],
           [6, 9, 8],
           [4, 6, 7]], dtype=uint8)
    
    • 1
    • 2
    • 3

    写在末尾:

    • 博客简介:专注AIoT领域,追逐未来时代的脉搏,记录路途中的技术成长!
    • 专栏简介:从0到1掌握SELinux的使用。
    • 面向人群:嵌入式Linux软件工程师
    • 专栏计划:接下来会逐步发布跨入人工智能的系列博文,敬请期待

  • 相关阅读:
    基于红黑树对map和set容器的封装
    『期末复习』16/32位微处理器(8086)基本寄存器
    计算机网络——CSMA/CD协议
    [Qt]QListView 重绘实例之三:滚动条覆盖的问题处理
    LeetCode //C - 77. Combinations
    Spring Cloud Gateway集成sentinel进行网关限流
    web前端日常更新 8.16
    Google Earth Engine 教程——栅格矢量数据转化和导出
    xml的语法
    PythonNote038---python执行shell命令
  • 原文地址:https://blog.csdn.net/RobotFutures/article/details/126092621