表示空值。
表示正无穷大。
表示圆周率
表示自然常数
numpy 的数值类型实际上是 dtype 对象的实例。
class dtype(object): def __init__(self, obj, align=False, copy=False): pass带单位的日期时间类型。 默认情况下,numpy 会根据字符串自动选择对应的单位。
import numpy as np a = np.datetime64('2020-03-01') print(a, a.dtype) # 2020-03-01 datetime64[D] a = np.datetime64('2020-03') print(a, a.dtype) # 2020-03 datetime64[M] a = np.datetime64('2020-03-08 20:00:05') print(a, a.dtype) # 2020-03-08T20:00:05 datetime64[s] a = np.datetime64('2020-03-08 20:00') print(a, a.dtype) # 2020-03-08T20:00 datetime64[m] a = np.datetime64('2020-03-08 20') print(a, a.dtype) # 2020-03-08T20 datetime64[h]可以强制指定使用的单位。
import numpy as np a = np.datetime64('2020-03', 'D') print(a, a.dtype) # 2020-03-01 datetime64[D] a = np.datetime64('2020-03', 'Y') print(a, a.dtype) # 2020 datetime64[Y] print(np.datetime64('2020-03') == np.datetime64('2020-03-01')) # True print(np.datetime64('2020-03') == np.datetime64('2020-03-02')) #False使用arange()创建 datetime64 数组,用于生成日期范围。
import numpy as np a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64) print(a) # ['2020-08-01' '2020-08-02' '2020-08-03' '2020-08-04' '2020-08-05' # '2020-08-06' '2020-08-07' '2020-08-08' '2020-08-09'] print(a.dtype) # datetime64[D] a = np.arange('2020-08-01 20:00', '2020-08-10', dtype=np.datetime64) print(a) # ['2020-08-01T20:00' '2020-08-01T20:01' '2020-08-01T20:02' ... # '2020-08-09T23:57' '2020-08-09T23:58' '2020-08-09T23:59'] print(a.dtype) # datetime64[m] a = np.arange('2020-05', '2020-12', dtype=np.datetime64) print(a) # ['2020-05' '2020-06' '2020-07' '2020-08' '2020-09' '2020-10' '2020-11'] print(a.dtype) # datetime64[M]numpy.datetime64 与 datetime.datetime 相互转换
import numpy as np import datetime dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30) dt64 = np.datetime64(dt, 's') print(dt64, dt64.dtype) # 2020-06-01T20:05:30 datetime64[s] dt2 = dt64.astype(datetime.datetime) print(dt2, type(dt2)) # 2020-06-01 20:05:30 <class 'datetime.datetime'>表示两个 datetime64 之间的差。
import numpy as np a = np.timedelta64(1, 'Y') b = np.timedelta64(a, 'M') print(a) # 1 years print(b) # 12 months c = np.timedelta64(1, 'h') d = np.timedelta64(c, 'm') print(c) # 1 hours print(d) # 60 minutestimedelta64 的运算。
import numpy as np a = np.timedelta64(1, 'Y') b = np.timedelta64(6, 'M') c = np.timedelta64(1, 'W') d = np.timedelta64(1, 'D') e = np.timedelta64(10, 'D') print(a) # 1 years print(b) # 6 months print(a + b) # 18 months print(a - b) # 6 months print(2 * a) # 2 years print(a / b) # 2.0 print(c / d) # 7.0 print(c % e) # 7 daysarray()和asarray()主要区别就是当数据源是ndarray 时,array()仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 asarray()不会。
import numpy as np x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] y = np.array(x) z = np.asarray(x) x[1][2] = 2 print(x,type(x)) # [[1, 1, 1], [1, 1, 2], [1, 1, 1]] <class 'list'> print(y,type(y)) # [[1 1 1] # [1 1 1] # [1 1 1]] <class 'numpy.ndarray'> print(z,type(z)) # [[1 1 1] # [1 1 1] # [1 1 1]] <class 'numpy.ndarray'>返回给定形状和类型的零数组。
返回与给定数组形状和类型相同的零数组。
import numpy as np x = np.zeros(5) print(x) # [0. 0. 0. 0. 0.] x = np.zeros([2, 3]) print(x) # [[0. 0. 0.] # [0. 0. 0.]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.zeros_like(x) print(y) # [[0 0 0] # [0 0 0]]返回给定形状和类型的1数组。
返回与给定数组形状和类型相同的1数组。
import numpy as np x = np.ones(5) print(x) # [1. 1. 1. 1. 1.] x = np.ones([2, 3]) print(x) # [[1. 1. 1.] # [1. 1. 1.]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.ones_like(x) print(y) # [[1 1 1] # [1 1 1]]返回一个空数组,数组元素为随机数。
返回与给定数组具有相同形状和类型的新数组。
import numpy as np x = np.empty(5) print(x) # [1.95821574e-306 1.60219035e-306 1.37961506e-306 # 9.34609790e-307 1.24610383e-306] x = np.empty((3, 2)) print(x) # [[1.60220393e-306 9.34587382e-307] # [8.45599367e-307 7.56598449e-307] # [1.33509389e-306 3.59412896e-317]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.empty_like(x) print(y) # [[ 7209029 6422625 6619244] # [ 100 707539280 504]]返回一个对角线上为1,其它地方为零的单位数组。
返回一个方的单位数组。
import numpy as np x = np.eye(4) print(x) # [[1. 0. 0. 0.] # [0. 1. 0. 0.] # [0. 0. 1. 0.] # [0. 0. 0. 1.]] x = np.eye(2, 3) print(x) # [[1. 0. 0.] # [0. 1. 0.]] x = np.identity(4) print(x) # [[1. 0. 0. 0.] # [0. 1. 0. 0.] # [0. 0. 1. 0.] # [0. 0. 0. 1.]]提取对角线或构造对角数组。
import numpy as np x = np.arange(9).reshape((3, 3)) print(x) # [[0 1 2] # [3 4 5] # [6 7 8]] print(np.diag(x)) # [0 4 8] print(np.diag(x, k=1)) # [1 5] print(np.diag(x, k=-1)) # [3 7] v = [1, 3, 5, 7] x = np.diag(v) print(x) # [[1 0 0 0] # [0 3 0 0] # [0 0 5 0] # [0 0 0 7]]返回一个常数数组。
返回与给定数组具有相同形状和类型的常数数组。
import numpy as np x = np.full((2,), 7) print(x) # [7 7] x = np.full(2, 7) print(x) # [7 7] x = np.full((2, 7), 7) print(x) # [[7 7 7 7 7 7 7] # [7 7 7 7 7 7 7]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.full_like(x, 7) print(y) # [[7 7 7] # [7 7 7]]返回给定间隔内的均匀间隔的值。
返回指定间隔内的等间隔数字。
返回数以对数刻度均匀分布。
返回一个由[0,1)内的随机数组成的数组。
import numpy as np x = np.arange(5) print(x) # [0 1 2 3 4] x = np.arange(3, 7, 2) print(x) # [3 5] x = np.linspace(start=0, stop=2, num=9) print(x) # [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] x = np.logspace(0, 1, 5) print(np.around(x, 2)) # [ 1. 1.78 3.16 5.62 10. ] #np.around 返回四舍五入后的值,可指定精度。 # around(a, decimals=0, out=None) # a 输入数组 # decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置 x = np.linspace(start=0, stop=1, num=5) x = [10 ** i for i in x] print(np.around(x, 2)) # [ 1. 1.78 3.16 5.62 10. ] x = np.random.random(5) print(x) # [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291] x = np.random.random([2, 3]) print(x) # [[0.41151858 0.93785153 0.57031309] # [0.13482333 0.20583516 0.45429181]]首先需要定义结构,然后利用np.array()来创建数组,其参数dtype为定义的结构。
在使用 numpy 时,你会想知道数组的某些信息。很幸运,在这个包里边包含了很多便捷的方法,可以给你想要的信息。
用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1,二维数组的秩为 2,以此类推。
表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
数组中所有元素的总量,相当于数组的shape中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。
对象的元素类型。
以字节的形式返回数组中每一个元素的大小
import numpy as np a = np.array([1, 2, 3, 4, 5]) print(a.shape) # (5,) print(a.dtype) # int32 print(a.size) # 5 print(a.ndim) # 1 print(a.itemsize) # 4 b = np.array([[1, 2, 3], [4, 5, 6.0]]) print(b.shape) # (2, 3) print(b.dtype) # float64 print(b.size) # 6 print(b.ndim) # 2 print(b.itemsize) # 8