1.numpy
Numpy基本介绍
在Numpy中,方向称作轴,轴的数目称作维。
N维数组在numpy中是一个ndarray类,而python中的array只处理一维数组。以下是几个比较重要的属性。
-
ndarray.ndim
指数组的维度
-
ndarray.shape
数组的形状,n行m列的矩阵,shape为(n,m)是元组。
-
ndarray.size
数组的大小,即n*m
-
ndarray.dtype
数组内元素的数据类型
-
ndarray.itemsize
占计算机内存的大小
-
ndarry.data
数据在内存中的地址
生成数组
import numpy as np
a = np.array([1,2,3,4])
还可以显示地声明数组的数据类型。
b = np.array([[1,2,3],[4,5,6]],dtype=complex)
numpy还提供了一些其他生成数组的方法。e.g.: zeros()
numpy.zeros(shape, dtype=float, order=’C’)
shape是数组的形状.dtype是类型,order:{‘C’,‘F’},可选 是否在存储器中以C或Fortran连续(按行或列方式)存储多维数据。
>>> np.zeros((3,3,3,3))
array([[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0.