【Numpy学习04】数组的创建

it2023-11-01  78

文章目录:

引言1. 依据现有数据来创建 ndarray【例1-1】通过array()函数进行创建。【例1-2】通过asarray()函数进行创建【例1-3】通过fromfunction()函数进行创建 2. 依据 ones 和 zeros 等填充方式【例2-1】零数组【例2-2】空数组【例2-3】单位数组【例2-4】对角数组【例2-5】常数数组 3. 利用数值范围来创建ndarray【例3】利用数值范围来创建ndarray实例 4. 结构数组的创建【例4-1】利用包含多个元组的列表来定义结构【例4-2】利用字典来定义结构

引言

NumPy 中定义的最重要的对象是称为 ndarray 的 N 维数组类型,它是描述相同类型的元素集合。ndarray 中的每个元素都是数据类型对象(dtype)的对象。ndarray 中的每个元素在内存中使用相同大小的块

ndarray的含义是The N-dimensional array,意思就是N维数组

注:默认import numpy as np已经写在每段代码前,不再重复写入

1. 依据现有数据来创建 ndarray

【例1-1】通过array()函数进行创建。

def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0):

【例1-1】通过array()函数进行创建

>>>datas = [x for x in range(5)] # 创建一维数组 >>>a = np.array(datas) >>>b = np.array(tuple(datas)) >>>print(a, type(a)) >>>print(b, type(b)) >>>print(a.shape,b.shape) [0 1 2 3 4] <class 'numpy.ndarray'> [0 1 2 3 4] <class 'numpy.ndarray'> (5,) (5,) # 创建二维数组 >>>c = np.array([a]*2) >>>print(c, type(c)) [[0 1 2 3 4] [0 1 2 3 4]] <class 'numpy.ndarray'> (2, 5)

我们可以看到使用array时,传入的数据类型可以是list,也可以是tuple。有一个小技巧n维数组的array()内的左括号数等于n,例如:

【例1-2】通过asarray()函数进行创建

array()和asarray()都可以将结构数据转化为 ndarray,但是array()和asarray()主要区别就是当数据源是ndarray 时,array()仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 asarray()不会。

如果改变dtype,例如int转成float,那么不用占用新的内存,即保持一致。

def asarray(a, dtype=None, order=None): return array(a, dtype, copy=False, order=order) #`array()`和`asarray()`都可以将结构数据转化为 ndarray >>>x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] >>>y = np.array(x) >>>z = np.asarray(x) >>>#改变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'> #当数据源是ndarray时 >>>x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) >>>y = np.array(x) >>>z = np.asarray(x) >>>w = np.asarray(x, dtype=np.int) >>>x[1][2] = 2 >>>print(x,type(x),x.dtype) [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32 >>>print(y,type(y),y.dtype) [[1 1 1] [1 1 1] [1 1 1]] <class 'numpy.ndarray'> int32 >>>print(z,type(z),z.dtype) [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32 >>>print(w,type(w),w.dtype) [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32

可见w随着x的变化而变化,而不是在一个新的内存中

#更改为较大的dtype时,其大小必须是array的最后一个axis的总大小(以字节为单位)的除数 >>>x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) >>>print(x, x.dtype) [[1 1 1] [1 1 1] [1 1 1]] int32 >>>x.dtype = np.float # ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.

怎么理解上面的错误,很简单,画个图示意一下:

假设我们之前有4个int8,总共32个单位,那么新的dtype可以是int16,可以是int32,但是不能是int64,因为int64装完32之后还有空余。

【例1-3】通过fromfunction()函数进行创建

给函数绘图的时候可能会用到fromfunction(),该函数可从函数中创建数组。

def fromfunction(function, shape, **kwargs): #通过在每个坐标上执行一个函数来构造数组 >>>def f(x, y): >>> return 10 * x + y >>>x = np.fromfunction(f, (5, 4), dtype=int) >>>print(x) [[ 0 1 2 3] [10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43]]

上述的f也可以直接替换为lambda函数, 即np.fromfunction(lambda i, j: 10*i+j, (5, 5), dtype=int)

2. 依据 ones 和 zeros 等填充方式

在机器学习任务中经常做的一件事就是初始化参数,需要用常数值或者随机值来创建一个固定大小的矩阵。

【例2-1】零数组

zeros()函数:返回给定形状和类型的零数组。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】空数组

empty()函数:返回一个空数组,数组元素为随机数。empty_like函数:返回与给定数组具有相同形状和类型的新数组。 def empty(shape, dtype=None, order='C'): def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):

【例】

>>>x = np.empty(5) >>>print(x) [0. 0. 0. 0. 0.] >>>x = np.empty((3, 2)) >>>print(x) [[0. 0.] [0. 0.] [0. 0.]]

【例2-3】单位数组

eye()函数:返回一个对角线上为1,其它地方为零的单位数组。identity()函数:返回一个方的单位数组。 def eye(N, M=None, k=0, dtype=float, order='C'): def identity(n, dtype=None): >>>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.]]

【例2-4】对角数组

diag()函数:提取对角线或构造对角数组。 def diag(v, k=0): >>>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]]

注:当参数是ndarray时,表示提取对角线;参数是list时,表示构建对角数组

【例2-5】常数数组

full()函数:返回一个常数数组。full_like()函数:返回与给定数组具有相同形状和类型的常数数组。 def full(shape, fill_value, dtype=None, order='C'): def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None): #利用shape+value >>>x = np.full((2,), 7) >>>print(x) [7 7] #上述的变形 >>>x = np.full(2, 7) >>>print(x) [7 7] #shape是二维 >>>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]]

3. 利用数值范围来创建ndarray

arange()函数:返回给定间隔内的均匀间隔的值。linspace()函数:返回指定间隔内的等间隔数字。logspace()函数:返回数以对数刻度均匀分布。numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。 def arange([start,] stop[, step,], dtype=None): def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): def rand(d0, d1, ..., dn):

【例3】利用数值范围来创建ndarray实例

>>>x = np.arange(5) >>>print(x) [0 1 2 3 4] # 3到7 步长为2 左闭右开 >>>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. ] #其实就是求log以10为底y = x,x是0到1的分散数,求y的过程 >>>x = np.logspace(0, 1, 5) >>>print(np.around(x, 2)) [ 1. 1.78 3.16 5.62 10. ] #其实就是logspace(0,1,5)的过程 >>>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.08560221 0.62786915 0.20752473 0.86764754 0.48029354]

4. 结构数组的创建

结构数组,首先需要定义结构,然后利用np.array()来创建数组,其参数dtype为定义的结构。

【例4-1】利用包含多个元组的列表来定义结构

>>>studentType = np.dtype([('name', 'U20'), ('age', 'i1'), ('class', 'i1')]) >>>print(studentType) [('name', '<U20'), ('age', 'i1'), ('class', 'i1')] >>>a = np.array([('ZhangSan', 25, 5), ('LiSi', 25, 6), ('WangWu', 24, 7)], dtype=studentType) >>>print(a, type(a)) [('ZhangSan', 25, 5) ('LiSi', 25, 6) ('WangWu', 24, 7)] <class 'numpy.ndarray'>

【例4-2】利用字典来定义结构

只需将

{ 'names': ['name', 'age', 'weight'],'formats': ['U30', 'i8', 'f8']}

填入dtype中即可

# 结构数组的取值方式和一般数组差不多,可以通过下标取得元素: >>>print(a[0]) ('ZhangSan', 25, 5) >>>print(a[-1]) ('WangWu', 24, 7) # 我们可以使用字段名作为下标获取对应的值 >>>print(a['name']) ['ZhangSan' 'LiSi' 'WangWu']
最新回复(0)