Numpy学习 TASK1

it2023-05-27  61

TASK1 数据类型及数组创建

1 数据类型1.1 常见数据类型1.2 创建数据类型1.3 数据类型信息 2 数组创建2.1依据现有数据创建ndarray2.1.1 array() 函数2.1.2 asarray() 函数2.1.3 fromfunction() 函数 2.2 依据ones 和zeros 填充2.3 依据数值范围创建2.4 结构数据的创建2.4.1 利用字典定义结构2.4.2 利用包含多个元组的列表来定义结构 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。其中的科学计算包括:包括:1、一个强大的N维数组对象Array;2、比较成熟的(广播)函数库;3、用于整合C/C++和Fortran代码的工具包;4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。

1 数据类型

numpy支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型。下表列举了常用 NumPy 基本类型

1.1 常见数据类型

1.2 创建数据类型

numpy的数值类型实际上是dtype对象的实例

class dtype(object): def __init__(self, obj, align=False, copy=False): pass a = np.dtype('b1') print(a.type) # <class 'numpy.bool_'> print(a.itemsize) # 1 a = np.dtype('i1') print(a.type) # <class 'numpy.int8'> print(a.itemsize) # 1 a = np.dtype('i2') print(a.type) # <class 'numpy.int16'> print(a.itemsize) # 2 a = np.dtype('i4') print(a.type) # <class 'numpy.int32'> print(a.itemsize) # 4 a = np.dtype('i8') print(a.type) # <class 'numpy.int64'> print(a.itemsize) # 8 a = np.dtype('u1') print(a.type) # <class 'numpy.uint8'> print(a.itemsize) # 1 a = np.dtype('u2') print(a.type) # <class 'numpy.uint16'> print(a.itemsize) # 2 a = np.dtype('u4') print(a.type) # <class 'numpy.uint32'> print(a.itemsize) # 4 a = np.dtype('u8') print(a.type) # <class 'numpy.uint64'> print(a.itemsize) # 8 a = np.dtype('f2') print(a.type) # <class 'numpy.float16'> print(a.itemsize) # 2 a = np.dtype('f4') print(a.type) # <class 'numpy.float32'> print(a.itemsize) # 4 a = np.dtype('f8') print(a.type) # <class 'numpy.float64'> print(a.itemsize) # 8 a = np.dtype('S') print(a.type) # <class 'numpy.bytes_'> print(a.itemsize) # 0 a = np.dtype('S3') print(a.type) # <class 'numpy.bytes_'> print(a.itemsize) # 3 a = np.dtype('U3') print(a.type) # <class 'numpy.str_'> print(a.itemsize) # 12

1.3 数据类型信息

NumPy和Python整数类型的行为在整数溢出方面存在显着差异,与 NumPy 不同,Python 的 int 是灵活的。这意味着Python整数可以扩展以容纳任何整数并且不会溢出。

def __init__(self, int_type): pass def min(self): pass def max(self): pass

2 数组创建

NumPy 数组的维数称为秩(rank),秩就是轴的数量,即数组的维度,一维数组的秩为 1,二维数组的秩为 2,以此类推。axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;axis=1,表示沿着第1轴进行操作,即对每一行进行操作。 下面是ndarray的主要对象属性:

2.1依据现有数据创建ndarray

2.1.1 array() 函数

a = np.array([0, 1, 2, 3, 4]) b = np.array((0, 1, 2, 3, 4)) print(a, type(a)) print(b, type(b)) [0 1 2 3 4] <class 'numpy.ndarray'> [0 1 2 3 4] <class 'numpy.ndarray'>

2.1.2 asarray() 函数

array()和asarray()的区别: A.当数据源是ndarray 时,array()仍然会 copy 出一个副本,占用新的内存; B.当数据源是ndarray并且不改变 dtype 时,asarray()不会占用新内存,但改变dtype时,asarray()会占用新内存。占用新内存意味着新创建的ndarray数据不随原array()数据改变而改变。

import numpy as np x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.int) y = np.array(x) z = np.asarray(x) w = np.asarray(x, dtype=np.float) # 改变dtype类型 # 改变x中某个值 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 1] # [1 1 1]] <class 'numpy.ndarray'> float64

2.1.3 fromfunction() 函数

通过在每个坐标上执行一个函数来构造数组

import numpy as np def f(x, y): return x + y x = np.fromfunction(f, (5, 4), dtype=int) print(x) # [[0 1 2 3] # [1 2 3 4] # [2 3 4 5] # [3 4 5 6] # [4 5 6 7]]

2.2 依据ones 和zeros 填充

零数组: zeros()函数:返回给定形状和类型的零数组。 zeros_like()函数:返回与给定数组形状和类型相同的零数组。

import numpy as np x = np.zeros(3) print(x) # [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数组: ones()函数:返回给定形状和类型的1数组。 ones_like()函数:返回与给定数组形状和类型相同的1数组。 把0数组中数字“0”变成数字“1,其余与0数组完全一样。 空数组: empty()函数:返回一个空数组,数组元素为随机数。 empty_like函数:返回与给定数组具有相同形状和类型的新数组。 把0数组中数字“0”变成随机数,其余与0数组完全一样 单位数组: eye()函数:返回一个对角线上为1,其它地方为零的单位数组。identity()函数:返回一个方的单位数组。

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.]]

对角数组: diag()函数:有两个功能,一是提取数组的对角线,二是构造对角数组。

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]]

常数数组: full()函数:返回一个常数数组。 full_like()函数:返回与给定数组具有相同形状和类型的常数数组。

import numpy as np x = np.full(2, 5) print(x) # [5 5] x = np.full((2, 3), 7) print(x) # [[7 7 7] # [7 7 7]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.full_like(x, 6) print(y) # [[6 6 6] # [6 6 6]]

2.3 依据数值范围创建

arange()函数:返回给定间隔内的均匀间隔的值linspace()函数:返回指定间隔内的等间隔数字。logspace()函数:返回数以对数刻度均匀分布。numpy.random.random() 返回一个由[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.logspace(0, 1, 5) print(np.around(x, 2)) # [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] x = np.linspace(start=0, stop=1, num=5) # 解释了np.logspace()函数 x = [10 ** i for i in x] print(np.around(x, 2)) # np.around 返回四舍五入后的值,可指定精度。 # [ 1. 1.78 3.16 5.62 10. ] x = np.random.random(5) print(x) # [ 1. 1.78 3.16 5.62 10. ] x = np.random.random([2, 3]) print(x) # [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291] # [[0.41151858 0.93785153 0.57031309] # [0.13482333 0.20583516 0.45429181]]

2.4 结构数据的创建

2.4.1 利用字典定义结构

import numpy as np personType = np.dtype({ 'names': ['name', 'age', 'weight'], 'formats': ['U30', 'i8', 'f8']}) a = np.array([('yiyi', 18, 50), ('dawang', 20, 57.)], dtype=personType) print(a, type(a)) # [('yiyi', 18, 50.) ('dawang', 20, 57.)] # <class 'numpy.ndarray'>

2.4.2 利用包含多个元组的列表来定义结构

import numpy as np personType = np.dtype([('name', 'U30'), ('age', 'i8'), ('weight', 'f8'), ('height', 'f8')]) a = np.array([('yiyi', 18, 50, 161), ('dawang', 20, 57, 170)], dtype=personType) print(a, type(a)) # [('yiyi', 18, 50., 161.) ('dawang', 20, 57., 170.)] # <class 'numpy.ndarray'> # 结构数组也可以通过下标取得元素 print(a[0]) # ('yiyi', 18, 50., 161.) # 我们可以使用字段名作为下标获取对应的值 print(a['name']) # ['yiyi' 'dawang'] print(a['height']) # [161. 170.] print(a['age']) # [18 20] print(a['weight']) # [[50. 57.]
最新回复(0)