import numpy as np
- >>> # 创建数组,得到darray类型
- >>> t1 = np.array([1, 2, 3])
- >>> t2 = np.array(range(8))
- >>> t3 = np.arange(1, 9, 2)
- >>> print(t1)
- [1 2 3]
- >>> print(type(t1)) # 数组的类名
- <class 'numpy.ndarray'>
- >>> print(t1.dtype) # 数据的类型
- int32
- >>># 指定数组的数据类型为float
- >>> t4 = np.array(range(1,4),dtype=float) # 指定数组的数据类型为float
- >>> print(t4)
- [1. 2. 3.]
- >>> print(t4.dtype)
- float64
- >>>
- >>>
- >>># 指定数组的数据类型为int8
- >>> t5 = np.array(range(1,4),dtype="i1") # 指定数组的数据类型为int8
- >>> print(t5)
- [1 2 3]
- >>> print(t5.dtype)
- int8
- >>>
- >>>
- >>># 指定数组的数据类型为bool
- >>> t6 = np.array([1,1,0,1,0,1,2],dtype=bool) # 指定数组的数据类型为bool
- >>> print(t6)
- [ True True False True False True True]
- >>> print(t6.dtype)
- bool
数据类型合集:

- >>># 调整数据类型
- >>> t7 = t6.astype("int8") # 调整数据类型
- >>> print(t7)
- [1 1 0 1 0 1 1]
- >>> print(t7.dtype)
- int8
- >>> # numpy中的小数
- >>> import random
- >>> t8 = np.array([random.random() for i in range(10)])
- >>> print(t8)
- [0.09920204 0.93539751 0.54779053 0.35806529 0.61311635 0.90631822
- 0.46175299 0.5640876 0.96294561 0.5474859 ]
- >>> print(t8.dtype)
- float64
- >>> # 对t8保留一定小数位
- >>> t9 = np.round(t8,2)
- >>> print(t9)
- [0.1 0.94 0.55 0.36 0.61 0.91 0.46 0.56 0.96 0.55]
- >>> round(random.random(),3) # round()函数,取小数位
- 0.583
- >>> "%.3f"%random.random() # 取小数位
- '0.988'
- >>> "%.3f"%random.random() # 取小数位
- '0.626'
- >>> import numpy as np
- >>>
- >>>
- >>> # 创建数组,得到darray类型
- >>> t1 = np.array([1, 2, 3])
- >>> t2 = np.array(range(8))
- >>> t3 = np.arange(1, 9, 2)
- >>>
- >>>
- >>> print(t1)
- [1 2 3]
- >>> print(type(t1)) # 数组的类名
- <class 'numpy.ndarray'>
- >>> print(t1.dtype) # 数据的类型
- int32
- >>>
- >>>
- >>> print(t2)
- [0 1 2 3 4 5 6 7]
- >>> print(t3)
- [1 3 5 7]
- >>>
- >>># 指定数组的数据类型为float
- >>> t4 = np.array(range(1,4),dtype=float) # 指定数组的数据类型为float
- >>> print(t4)
- [1. 2. 3.]
- >>> print(t4.dtype)
- float64
- >>>
- >>># 指定数组的数据类型为int8
- >>> t5 = np.array(range(1,4),dtype="i1") # 指定数组的数据类型为int8
- >>> print(t5)
- [1 2 3]
- >>> print(t5.dtype)
- int8
- >>>
- >>># 指定数组的数据类型为bool
- >>> t6 = np.array([1,1,0,1,0,1,2],dtype=bool) # 指定数组的数据类型为bool
- >>> print(t6)
- [ True True False True False True True]
- >>> print(t6.dtype)
- bool
- >>>
- >>># 调整数据类型
- >>> t7 = t6.astype("int8") # 调整数据类型
- >>> print(t7)
- [1 1 0 1 0 1 1]
- >>> print(t7.dtype)
- int8
- >>>
- >>> # numpy中的小数
- >>> import random
- >>> t8 = np.array([random.random() for i in range(10)])
- >>> print(t8)
- [0.09920204 0.93539751 0.54779053 0.35806529 0.61311635 0.90631822
- 0.46175299 0.5640876 0.96294561 0.5474859 ]
- >>> print(t8.dtype)
- float64
- >>>
- >>> # 对t8保留一定小数位
- >>> t9 = np.round(t8,2)
- >>> print(t9)
- [0.1 0.94 0.55 0.36 0.61 0.91 0.46 0.56 0.96 0.55]
- >>> round(random.random(),3) # round()函数,取小数位
- 0.583
- >>> "%.3f"%random.random() # 取小数位
- '0.988'
- >>> "%.3f"%random.random() # 取小数位
- '0.626'
1、shape()
- >>> t1 = np.arange(4)
- >>> t2 = np.array([2,4,6,8])
- >>> t3 = np.array([[1,2,3],[4,5,6]])
- >>> 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]]])
- >>> t1.shape
- (4,)
- >>> t2.shape
- (4,)
- >>> t3.shape
- (2, 3)
- >>> t4.shape
- (2, 3, 4)
2、reshape()
reshap()有返回值,不会改变原来的数组,返回改变后的数组。
- >>> t5 = np.arange(24).reshape(2,3,4)
- >>> print(t5)
- [[[ 0 1 2 3]
- [ 4 5 6 7]
- [ 8 9 10 11]]
-
- [[12 13 14 15]
- [16 17 18 19]
- [20 21 22 23]]]
- >>> t6 = t5.reshape((24,)) #将数组变一维
- >>> print(t6)
- [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
- >>> t7 = t5.reshape((24,1))
- >>> print(t7)
- [[ 0]
- [ 1]
- [ 2]
- [ 3]
- [ 4]
- [ 5]
- [ 6]
- [ 7]
- [ 8]
- [ 9]
- [10]
- [11]
- [12]
- [13]
- [14]
- [15]
- [16]
- [17]
- [18]
- [19]
- [20]
- [21]
- [22]
- [23]]
- >>> t7 = t5.reshape((1,24))
- >>> print(t7)
- [[ 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()
将数组变为一维,不需要传入参数
- >>> t8 = t5.flatten()
- >>> t8
- array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
- 17, 18, 19, 20, 21, 22, 23])
4、矩阵运算 + - * /
两个矩阵维度相同时,其加减乘除为对应位置元素运算后的新矩阵;对于两个维数不同的矩阵,其行数或列数有一个相同时,其进行运算时,会发生广播,仍然可以运算;行数和列数都不同时,运算发生错误。
广播:如果两个数组的后缘维度(trailing dimension,即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失和 (或)长度为1的维度上进行。