|
-
-
- import numpy as np
- # # 使用标量类型
- # dt = np.dtype(np.int32)
- # print(dt)
- #
- # # int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
- # dt = np.dtype('i4')
- # print(dt)
- #
- # # 字节顺序标注
- # dt = np.dtype('<i4')
- # print(dt)
- #
- # dt = np.dtype([('age', np.int8)])
- # print(dt)
- #
- # dt = np.dtype([('age', np.int8)])
- # a = np.array([(10,), (20,), (30,)], dtype=dt)
- # print(a)
- # print(a['age'])
- #
- # student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
- # print(student)
- #
- # a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
- # print(a)
- #
- # a = np.arange(32)
- # b = a.reshape(2, 4, 4)
- # print(b.ndim)
- # print(b.shape)
- #
- # a = np.array([[1, 2, 3], [4, 5, 6]])
- # a.shape = (3, 2)
- # print(a)
- #
- # a = np.array([[1, 2, 3], [4, 5, 6]])
- # b = a.reshape(3, 2)
- # print(b)
- # x = np.array([1, 2, 3, 4, 5], dtype=np.int8)
- # print(x.itemsize)
- # y = np.array([1, 2, 3, 4, 5], dtype=np.float64)
- # print(y.itemsize)
-
- # x = np.empty([3, 2], dtype=int)
- # print(x)
- #
- # # 默认为浮点数
- # x = np.zeros(5)
- # print(x)
- # # 设置类型为整数
- # y = np.zeros((5,), dtype=int)
- # print(y)
- #
- # # 自定义类型
- # z = np.zeros((2, 2), dtype=[('x', 'i4'), ('y', 'i4')])
- # print(z)
- #
- # # 默认为浮点数
- # x = np.ones(5)
- # print(x)
- #
- # x = np.ones([2, 2], dtype=int)
- # print(x)
- #
- # s = b'Hello World'
- # a = np.frombuffer(s, dtype='S1')
- # print(a)
- #
- # arr1 = np.array([1, 2, 3, 4, 5])
- # arr2 = np.frombuffer(arr1, dtype=int)
- # print(arr2) # [1 2 3 4 5]
- #
- #
- # list=range(5)
- # it=iter(list)
- # a = [1,2,3,4,5]
- # # 使用迭代器创建 ndarray
- # x=np.fromiter(arr1, dtype=float)
- # print(x)
- # aa = [
- # [1, 2, 3],
- # [4, 5, 6],
- # [7, 8, 9]
- # ]
- # a = np.array(aa)
- # # b = a[1:3, 1:3]
- # # c = a[1:3, [1, 2]]
- # d = a[...,1:]
- # # print(b)
- # # print(c)
- # print(d)
-
- # a = np.array([[0, 0, 0],
- # [10, 10, 10],
- # [20, 20, 20],
- # [30, 30, 30]])
- # b = np.array([1, 2, 3])
- # bb = np.tile(b, (4, 1)) # 重复 b 的各个维度
- # print(bb)
- # # print(a + bb)
-
- a = np.arange(6).reshape(2, 3)
- print(a)
- print("========================")
- print(a.T)
|