Numpy基础知识(一)
数组属性
NumPy 数组的维数称为秩(rank),秩就是轴的数量,即数组的维度。
在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions)。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是 NumPy 中的轴(axis),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。
很多时候可以声明 axis。axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;axis=1,表示沿着第1轴进行操作,即对每一行进行操作。
属性 | 说明 |
---|---|
ndarray.ndim | 秩,即轴的数量或维度的数量 |
ndarray.shape | 数组的维度,对于矩阵,n 行 m 列 |
ndarray.size | 数组元素的总个数,相当于 .shape 中 n*m 的值 |
ndarray.dtype | ndarray 对象的元素类型 |
ndarray.itemsize | ndarray 对象中每个元素的大小,以字节为单位 |
ndarray.flags | ndarray 对象的内存信息 |
ndarray.real | ndarray元素的实部 |
ndarray.imag | ndarray 元素的虚部 |
ndarray.data | 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。 |
创建数组
#未初始化
numpy.empty(shape, dtype = float, order = 'C')
#初始化为0
numpy.zeros(shape, dtype = float, order = 'C')
#初始化为1
numpy.ones(shape, dtype = None, order = 'C')
参数 | 描述 |
---|---|
shape | 数组形状 |
dtype | 数据类型,可选 |
order | 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。 |
从数值范围创建数组
numpy.arange(start, stop, step, dtype)
#起始值,终止值,步长,返回的数据类型
#生成0~5的数组
x = np.arrange(5)
#numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
#num未生成的样本数量(保证等步长) endpoint为fasle是数列中不包含stop
#retstep为true时,生成的数组中会显示间距
#numpy.logspace 函数用于创建一个于等比数列
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
#base为log的底数
a = np.logspace(0,9,10,base=2)
print (a)
#[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
索引
a = np.arange(10)
s = slice(2, 7, 2) #从索引2开始到索引7前,步长为2
ic(a[s])
ic(a[2:7:2])#可以用冒号间隔三个参数start、stop、step
ic(a[2:])#表示2后所有的元素
x = np.array([[1, 2], [3, 4], [5, 6]])
#获取(0,0) (1,1) (2,0)位置的元素
y = x[[0, 1, 2], [0, 1, 0]]
#得到结果
[1, 4, 5]
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
print("数组为:", x)
print('\n')
# index = np.array([0, 0, 3, 3], [0, 2, 0, 2])不可以这么写
ic(x[[0, 0, 3, 3], [0, 2, 0, 2]]) #产生一个列表
rows = np.array([[0, 0], [3, 3]])
cols = np.array([[0, 2], [0, 2]])
ic(x[rows, cols]) #产生2行2列的矩阵
#结果
x[[0, 0, 3, 3], [0, 2, 0, 2]]: array([ 0, 2, 9, 11])
x[rows, cols]: array([[ 0, 2],
[ 9, 11]])
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
ic(a[1:3, 1:3])
ic(a[1:3, [1, 2]]) #注意这里1:3和[1, 2]功能相同
ic(a[:, 1:])
ic(a[..., 1:]) #...和:作用相似
#得到结果为:
#ic| a[1:3, 1:3]: array([[5, 6],
# [8, 9]])
#ic| a[1:3, [1, 2]]: array([[5, 6],
# [8, 9]])
#ic| a[:, 1:]: array([[2, 3],
# [5, 6],
# [8, 9]])
#ic| a[..., 1:]: array([[2, 3],
# [5, 6],
# [8, 9]])
布尔索引(筛选得到特定的值)
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
ic(x)
ic(x[x > 5])
#ic| x: array([[ 0, 1, 2],
# [ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11]])
#ic| x[x > 5]: array([ 6, 7, 8, 9, 10, 11])
#利用取反运算符~
a = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])
ic(a[~np.isnan(a)])
#array([1., 2., 3., 4., 5.])
花式索引
利用整数数组进行索引,每个数值作为目标数组的某个轴的下表来取值,
x = np.arange(32).reshape((8, 4))
y = np.arange(8)
index = [4, 3, 2, 1]
ic(x)
ic(y)
ic(x[index])#对二维数组进行一维索引 返回的是每一行的内容
ic(y[index])#对一维数组进行一维索引,返回的是对应每个元素
#x[index]: array([[16, 17, 18, 19],
# [12, 13, 14, 15],
# [ 8, 9, 10, 11],
# [ 4, 5, 6, 7]])
#y[index]: array([4, 3, 2, 1])
传入多个索引数组需要使用np.ix_
x=np.arange(32).reshape((8,4))
print (x[np.ix_([1,5,7,2],[0,3,1,2])])
#[[ 4 7 5 6]
#[20 23 21 22]
#[28 31 29 30]
#[ 8 11 9 10]]