1 数组的创建 Numpy的核心特征N-维数组对象-ndarray, 数据集容器,允许数学计算
1.1. 依据现有数据来创建ndarray
1.1.1. arange() 函数:返回给定间隔内的均匀间隔的值。 1.1.2. linspace() 函数:返回指定间隔内的等间隔数字。 1.1.3. logspace() 函数:返回数以对数刻度均匀分布。 1.1.4. numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。
1.1.1. arange() 函数:返回给定间隔内的均匀间隔的值。 # def arange([start,] stop[, step,], dtype=None): x = np.arange(5) print(x) # [0 1 2 3 4] x = np.arange(1, 5, 2) print(x) # [3 5] 1.1.2. linspace() 函数:返回指定间隔内的等间隔数字。 # def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): x = np.linspace(start=0, stop=2, num=3) print(x) # [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] x= np.linspace(0,2,num=9) print(x) 1.1.3. logspace() 函数:返回数以对数刻度均匀分布。 #np.around 返回四舍五入后的值,可指定精度。 # around(a, decimals=0, out=None) # a 输入数组 # decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置 x = np.logspace(0, 1, 5) print(np.around(x, 2)) # [ 1. 1.78 3.16 5.62 10. ] 1.1.4. numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。 # numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。 # def rand(d0, d1, ..., dn): x = np.random.randn() 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]]1.2. 依据ones和zeros填充方式
1.2.1 (a)零数组 1. zeros() 函数:返回给定形状和类型的零数组。 2. zeros_like() 函数:返回与给定数组形状和类型相同的零数组。 def zeros(shape, dtype=None, order='C'): def zeros_like(a, dtype=None, order='K', subok=True, shape=None): 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.2.2 (b)1数组 1. ones() 函数:返回给定形状和类型的1数组。 2. ones_like() 函数:返回与给定数组形状和类型相同的1数组。 def ones(shape, dtype=None, order='C'): def ones_like(a, dtype=None, order='K', subok=True, shape=None): 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]]1.3. 依据现有数据来创建ndarray
1.array()函数进行创建 def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0) # 创建一维数组 a = np.array([0, 1, 2, 3, 4]) b = np.array((0, 1, 2, 3, 4)) print(a, type(a)) # [0 1 2 3 4] <class 'numpy.ndarray'> print(b, type(b)) # [0 1 2 3 4] <class 'numpy.ndarray'> # 创建二维数组 c = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) print(c, type(c)) # [[11 12 13 14 15] # [16 17 18 19 20] # [21 22 23 24 25] # [26 27 28 29 30] # [31 32 33 34 35]] <class 'numpy.ndarray'> 2. asarray()函数进行创建 将输入转换为ndarray,但如果输入已经是ndarray则不再复制 x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] y = np.array(x) z = np.asarray(x) 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'>1.4. 结构数组的创建
(a)利用字典来定义结构 personType = np.dtype({ 'names':['name','weight','age'], 'formats':['U30','i8','f8']}) a = np.array([('liming',24, 63.9), ('Mike',15,67.),('Jan', 34, 45.8)], dtype = personType) print(a,type(a)) # [('liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)] <class 'numpy.ndarray'> (b)利用包含多个元组的列表来定义结构 personType = np.dtype([('name','U30'),('age','i8'),('weight','f8')]) a= np.array([('liming', 24, 63.9),('Mike', 15, 67.),('Jan', 34, 45.8)], dtype= personType) print(a,type(a)) # 结构数组的取值方式和一般数组差不多,可以通过下标取得元素: print(a[0]) # ('Liming', 24, 63.9) print(a[-2:]) #[('Mike', 15, 67. ) ('Jan', 34, 45.8)] # 我们可以使用字段名作为下标获取对应的值 print(a['name']) #['liming' 'Mike' 'Jan'] print(a['age']) #[24 15 34] print(a['weight']) #[63.9 67. 45.8]