• numpy数据库


    numpy中的数组

    0、导包

    import numpy as np

    1、创建数组

    1. >>> # 创建数组,得到darray类型
    2. >>> t1 = np.array([1, 2, 3])
    3. >>> t2 = np.array(range(8))
    4. >>> t3 = np.arange(1, 9, 2)

    2、数组为  numpy.ndarray  类型

    1. >>> print(t1)
    2. [1 2 3]
    3. >>> print(type(t1)) # 数组的类名
    4. <class 'numpy.ndarray'>

    3、数组中数据类型

    1. >>> print(t1.dtype) # 数据的类型
    2. int32

    4、指定数组中数据类型

    1. >>># 指定数组的数据类型为float
    2. >>> t4 = np.array(range(1,4),dtype=float) # 指定数组的数据类型为float
    3. >>> print(t4)
    4. [1. 2. 3.]
    5. >>> print(t4.dtype)
    6. float64
    7. >>>
    8. >>>
    9. >>># 指定数组的数据类型为int8
    10. >>> t5 = np.array(range(1,4),dtype="i1") # 指定数组的数据类型为int8
    11. >>> print(t5)
    12. [1 2 3]
    13. >>> print(t5.dtype)
    14. int8
    15. >>>
    16. >>>
    17. >>># 指定数组的数据类型为bool
    18. >>> t6 = np.array([1,1,0,1,0,1,2],dtype=bool) # 指定数组的数据类型为bool
    19. >>> print(t6)
    20. [ True True False True False True True]
    21. >>> print(t6.dtype)
    22. bool

    数据类型合集:

    5、改变数组中数据类型

    1. >>># 调整数据类型
    2. >>> t7 = t6.astype("int8") # 调整数据类型
    3. >>> print(t7)
    4. [1 1 0 1 0 1 1]
    5. >>> print(t7.dtype)
    6. int8

    6、numpy  中的小数

    1. >>> # numpy中的小数
    2. >>> import random
    3. >>> t8 = np.array([random.random() for i in range(10)])
    4. >>> print(t8)
    5. [0.09920204 0.93539751 0.54779053 0.35806529 0.61311635 0.90631822
    6. 0.46175299 0.5640876 0.96294561 0.5474859 ]
    7. >>> print(t8.dtype)
    8. float64

    7、设置小数位数  round()  函数

    1. >>> # 对t8保留一定小数位
    2. >>> t9 = np.round(t8,2)
    3. >>> print(t9)
    4. [0.1 0.94 0.55 0.36 0.61 0.91 0.46 0.56 0.96 0.55]

    8、取小数位数的方法

    1. >>> round(random.random(),3) # round()函数,取小数位
    2. 0.583
    3. >>> "%.3f"%random.random() # 取小数位
    4. '0.988'
    5. >>> "%.3f"%random.random() # 取小数位
    6. '0.626'

    综合代码

    1. >>> import numpy as np
    2. >>>
    3. >>>
    4. >>> # 创建数组,得到darray类型
    5. >>> t1 = np.array([1, 2, 3])
    6. >>> t2 = np.array(range(8))
    7. >>> t3 = np.arange(1, 9, 2)
    8. >>>
    9. >>>
    10. >>> print(t1)
    11. [1 2 3]
    12. >>> print(type(t1)) # 数组的类名
    13. <class 'numpy.ndarray'>
    14. >>> print(t1.dtype) # 数据的类型
    15. int32
    16. >>>
    17. >>>
    18. >>> print(t2)
    19. [0 1 2 3 4 5 6 7]
    20. >>> print(t3)
    21. [1 3 5 7]
    22. >>>
    23. >>># 指定数组的数据类型为float
    24. >>> t4 = np.array(range(1,4),dtype=float) # 指定数组的数据类型为float
    25. >>> print(t4)
    26. [1. 2. 3.]
    27. >>> print(t4.dtype)
    28. float64
    29. >>>
    30. >>># 指定数组的数据类型为int8
    31. >>> t5 = np.array(range(1,4),dtype="i1") # 指定数组的数据类型为int8
    32. >>> print(t5)
    33. [1 2 3]
    34. >>> print(t5.dtype)
    35. int8
    36. >>>
    37. >>># 指定数组的数据类型为bool
    38. >>> t6 = np.array([1,1,0,1,0,1,2],dtype=bool) # 指定数组的数据类型为bool
    39. >>> print(t6)
    40. [ True True False True False True True]
    41. >>> print(t6.dtype)
    42. bool
    43. >>>
    44. >>># 调整数据类型
    45. >>> t7 = t6.astype("int8") # 调整数据类型
    46. >>> print(t7)
    47. [1 1 0 1 0 1 1]
    48. >>> print(t7.dtype)
    49. int8
    50. >>>
    51. >>> # numpy中的小数
    52. >>> import random
    53. >>> t8 = np.array([random.random() for i in range(10)])
    54. >>> print(t8)
    55. [0.09920204 0.93539751 0.54779053 0.35806529 0.61311635 0.90631822
    56. 0.46175299 0.5640876 0.96294561 0.5474859 ]
    57. >>> print(t8.dtype)
    58. float64
    59. >>>
    60. >>> # 对t8保留一定小数位
    61. >>> t9 = np.round(t8,2)
    62. >>> print(t9)
    63. [0.1 0.94 0.55 0.36 0.61 0.91 0.46 0.56 0.96 0.55]
    64. >>> round(random.random(),3) # round()函数,取小数位
    65. 0.583
    66. >>> "%.3f"%random.random() # 取小数位
    67. '0.988'
    68. >>> "%.3f"%random.random() # 取小数位
    69. '0.626'

    numpy中的矩阵(多维数组)

    1、shape()

    1. >>> t1 = np.arange(4)
    2. >>> t2 = np.array([2,4,6,8])
    3. >>> t3 = np.array([[1,2,3],[4,5,6]])
    4. >>> t4 = np.array([[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]])
    5. >>> t1.shape
    6. (4,)
    7. >>> t2.shape
    8. (4,)
    9. >>> t3.shape
    10. (2, 3)
    11. >>> t4.shape
    12. (2, 3, 4)

    2、reshape()

    reshap()有返回值,不会改变原来的数组,返回改变后的数组。

    1. >>> t5 = np.arange(24).reshape(2,3,4)
    2. >>> print(t5)
    3. [[[ 0 1 2 3]
    4. [ 4 5 6 7]
    5. [ 8 9 10 11]]
    6. [[12 13 14 15]
    7. [16 17 18 19]
    8. [20 21 22 23]]]
    9. >>> t6 = t5.reshape((24,)) #将数组变一维
    10. >>> print(t6)
    11. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
    12. >>> t7 = t5.reshape((24,1))
    13. >>> print(t7)
    14. [[ 0]
    15. [ 1]
    16. [ 2]
    17. [ 3]
    18. [ 4]
    19. [ 5]
    20. [ 6]
    21. [ 7]
    22. [ 8]
    23. [ 9]
    24. [10]
    25. [11]
    26. [12]
    27. [13]
    28. [14]
    29. [15]
    30. [16]
    31. [17]
    32. [18]
    33. [19]
    34. [20]
    35. [21]
    36. [22]
    37. [23]]
    38. >>> t7 = t5.reshape((1,24))
    39. >>> print(t7)
    40. [[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]]

    3、flatten()

    将数组变为一维,不需要传入参数

    1. >>> t8 = t5.flatten()
    2. >>> t8
    3. array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    4. 17, 18, 19, 20, 21, 22, 23])

    4、矩阵运算    +    -    *   /

    两个矩阵维度相同时,其加减乘除为对应位置元素运算后的新矩阵;对于两个维数不同的矩阵,其行数或列数有一个相同时,其进行运算时,会发生广播,仍然可以运算;行数和列数都不同时,运算发生错误。

    广播:如果两个数组的后缘维度(trailing dimension,即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失和 (或)长度为1的维度上进行。

  • 相关阅读:
    【windows下导入sql文件到数据库中】
    阿里P8整合深入理解Dubbo实战+Kafka+分布式设计核心原理内部手册
    [feign]远程调用实现token信息的传递
    Java项目:SSM网上零食超市商城
    做技术开发到老 or 晋升管理层,程序员的终极目标是什么?
    2024全国水科技大会:【协办单位】山东文远环保科技股份有限公司
    ArcGIS应用(二十六)按照属性分割矢量图层要素为新的图层
    Go字符串实战操作大全!
    MySQL函数(经典收藏)
    【哲学问题】-《哲学家们都干了些什么?》
  • 原文地址:https://blog.csdn.net/m0_56997192/article/details/134433256