You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.1KB

  1. import numpy as np
  2. # # 使用标量类型
  3. # dt = np.dtype(np.int32)
  4. # print(dt)
  5. #
  6. # # int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
  7. # dt = np.dtype('i4')
  8. # print(dt)
  9. #
  10. # # 字节顺序标注
  11. # dt = np.dtype('<i4')
  12. # print(dt)
  13. #
  14. # dt = np.dtype([('age', np.int8)])
  15. # print(dt)
  16. #
  17. # dt = np.dtype([('age', np.int8)])
  18. # a = np.array([(10,), (20,), (30,)], dtype=dt)
  19. # print(a)
  20. # print(a['age'])
  21. #
  22. # student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
  23. # print(student)
  24. #
  25. # a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
  26. # print(a)
  27. #
  28. # a = np.arange(32)
  29. # b = a.reshape(2, 4, 4)
  30. # print(b.ndim)
  31. # print(b.shape)
  32. #
  33. # a = np.array([[1, 2, 3], [4, 5, 6]])
  34. # a.shape = (3, 2)
  35. # print(a)
  36. #
  37. # a = np.array([[1, 2, 3], [4, 5, 6]])
  38. # b = a.reshape(3, 2)
  39. # print(b)
  40. # x = np.array([1, 2, 3, 4, 5], dtype=np.int8)
  41. # print(x.itemsize)
  42. # y = np.array([1, 2, 3, 4, 5], dtype=np.float64)
  43. # print(y.itemsize)
  44. # x = np.empty([3, 2], dtype=int)
  45. # print(x)
  46. #
  47. # # 默认为浮点数
  48. # x = np.zeros(5)
  49. # print(x)
  50. # # 设置类型为整数
  51. # y = np.zeros((5,), dtype=int)
  52. # print(y)
  53. #
  54. # # 自定义类型
  55. # z = np.zeros((2, 2), dtype=[('x', 'i4'), ('y', 'i4')])
  56. # print(z)
  57. #
  58. # # 默认为浮点数
  59. # x = np.ones(5)
  60. # print(x)
  61. #
  62. # x = np.ones([2, 2], dtype=int)
  63. # print(x)
  64. #
  65. # s = b'Hello World'
  66. # a = np.frombuffer(s, dtype='S1')
  67. # print(a)
  68. #
  69. # arr1 = np.array([1, 2, 3, 4, 5])
  70. # arr2 = np.frombuffer(arr1, dtype=int)
  71. # print(arr2) # [1 2 3 4 5]
  72. #
  73. #
  74. # list=range(5)
  75. # it=iter(list)
  76. # a = [1,2,3,4,5]
  77. # # 使用迭代器创建 ndarray
  78. # x=np.fromiter(arr1, dtype=float)
  79. # print(x)
  80. # aa = [
  81. # [1, 2, 3],
  82. # [4, 5, 6],
  83. # [7, 8, 9]
  84. # ]
  85. # a = np.array(aa)
  86. # # b = a[1:3, 1:3]
  87. # # c = a[1:3, [1, 2]]
  88. # d = a[...,1:]
  89. # # print(b)
  90. # # print(c)
  91. # print(d)
  92. # a = np.array([[0, 0, 0],
  93. # [10, 10, 10],
  94. # [20, 20, 20],
  95. # [30, 30, 30]])
  96. # b = np.array([1, 2, 3])
  97. # bb = np.tile(b, (4, 1)) # 重复 b 的各个维度
  98. # print(bb)
  99. # # print(a + bb)
  100. a = np.arange(6).reshape(2, 3)
  101. print(a)
  102. print("========================")
  103. print(a.T)