ndarray是一个包含了相同元素类型和大小的多维数组。
创建数组:
1、使用系统方法
(shape[, dtype, order]) # 根据给定的参数创建一个ndarray数组,值用随机数填充
例:
>>> np.empty([2, 2])array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]])
(a[, dtype, order, subok]) #和empty不同的是,需要给出一个array的模板,就是a参数,新生成的ndarray继承了a的shape和dtype
例:
>>> a = ([1,2,3], [4,5,6]) >>> np.empty_like(a)array([[-1073741821, -1073741821, 3], #random [ 0, 0, -1073741821]])
(N[, M, k, dtype]) #生成一个N行M列的数组,K指定一条斜线,这条斜线上的值都是1,数组的其他元素维0
例:生成一个5行4列的数组,索引为1的斜线上全部是1,其他元素为0
>>> np.eye(5,4,1)array([[ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
(n[, dtype]) #生成一个正方形的数组即N×N类型的数组,且索引万恶哦0的斜线上维1,其他元素维0
例:
>>> np.identity(3)array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
(shape[, dtype, order]) #生成一个指定shape和dtype的数组,用1填充
例:
>>> np.ones(5)array([ 1., 1., 1., 1., 1.])>>> np.ones((2, 1))array([[ 1.], [ 1.]])
(a[, dtype, order, subok]) #和ones的区别就是需要给定一个dnarray模板,新生成的array继承了a的shape和dtype
例:
>>> x = np.arange(6)>>> x = x.reshape((2, 3))>>> xarray([[0, 1, 2], [3, 4, 5]])>>> np.ones_like(x)array([[1, 1, 1], [1, 1, 1]])
(shape[, dtype, order]) #根据给定的shape,和dtype生成一个由0填充的数组
例:
>>> np.zeros(5)array([ 0., 0., 0., 0., 0.])
(a[, dtype, order, subok]) #根据a模板生成一个新的用0 填充的ndarray数组
例:
>>> x = np.arange(6)>>> x = x.reshape((2, 3))>>> xarray([[0, 1, 2], [3, 4, 5]])>>> np.zeros_like(x)array([[0, 0, 0], [0, 0, 0]])
(shape, fill_value[, dtype, order]) #用指定的值填充数组
例:
>>> np.full((2, 2), 10)array([[10, 10], [10, 10]])
(a, fill_value[, dtype, order, subok]) #根据a模板的shape和dtype生成一个数组,如果指定的填充数不是a的dtype类型,会向下取整,这时候也可以指定新数组的dtype类型。
例:
>>> x = np.arange(6, dtype=np.int)>>> np.full_like(x, 1)array([1, 1, 1, 1, 1, 1])>>> np.full_like(x, 0.1) #如果full_value设置为1.2则就是用1填充array([0, 0, 0, 0, 0, 0])>>> np.full_like(x, 0.1, dtype=np.double)array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])>>> np.full_like(x, np.nan, dtype=np.double)array([ nan, nan, nan, nan, nan, nan])
2、用指定的数据填充
(object[, dtype, copy, order, subok, ndmin]) #用对象直接填充数组
例:
>>> np.array([1, 2, 3]) #一维数组array([1, 2, 3])>>> np.array([[1, 2], [3, 4]]) #二维数组array([[1, 2], [3, 4]])>>> np.array([1, 2, 3], ndmin=2) #只有一个元素的二维数组array([[1, 2, 3]])>>> np.array(np.mat('1 2; 3 4')) #从子类创建array([[1, 2], [3, 4]])
(a[, dtype, order]) #把lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays转化为array
例:
>>> a = [1, 2]>>> np.asarray(a)array([1, 2])
(a[, dtype, order]) #通过ndarray的子类创建array
>>> a = [1, 2]>>> np.asanyarray(a)array([1, 2])
(a[, dtype]) #返回一个地址连续的数组(C order)
>>> x = np.arange(6).reshape(2,3)>>> np.ascontiguousarray(x, dtype=np.float32)array([[ 0., 1., 2.], [ 3., 4., 5.]], dtype=float32)>>> x.flags['C_CONTIGUOUS']True
(data[, dtype]) # 把数组转化为矩阵,新的变量没有copy数据,只是指向原有的数据
>>> x = np.array([[1, 2], [3, 4]])>>> m = np.asmatrix(x)>>> x[0,0] = 5>>> mmatrix([[5, 2], [3, 4]])
(a[, order]) #顾名思义就是复制的意思
>>> x = np.array([1, 2, 3])>>> y = x>>> z = np.copy(x)>>> x[0] = 10>>> x[0] == y[0]True>>> x[0] == z[0]False
(buffer[, dtype, count, offset]) #把buffer数据转化为1维数组ps:如果数据不是机器字节顺序,需要指定他的dtype类型
>>> s = 'hello world'>>> np.frombuffer(s, dtype='S1', count=5, offset=6)array(['w', 'o', 'r', 'l', 'd'], dtype='|S1')
(buffer[, dtype, count, offset]) #从文件读取数据 ps:该方法不长用用save替代
(function, shape, **kwargs) #用方法计算出来的数据填充数组
>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)array([[0, 1, 2], [1, 2, 3], [2, 3, 4]])
(iterable, dtype[, count]) #通过迭代器生成一个一维数组
>>> iterable = (x*x for x in range(5))>>> np.fromiter(iterable, np.float)array([ 0., 1., 4., 9., 16.])
(string[, dtype, count, sep]) #把二进制流或者字符串转化维数组
>>> np.fromstring('\x01\x02', dtype=np.uint8)array([1, 2], dtype=uint8)>>> np.fromstring('1 2', dtype=int, sep=' ')array([1, 2])>>> np.fromstring('1, 2', dtype=int, sep=',')array([1, 2])>>> np.fromstring('\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)array([1, 2, 3], dtype=uint8)
([start,] stop[, step,][, dtype]) |
#根据给定的区间创建连续的值
>>> np.arange(3)array([0, 1, 2])>>> np.arange(3.0)array([ 0., 1., 2.])>>> np.arange(3,7)array([3, 4, 5, 6])>>> np.arange(3,7,2)array([3, 5])
numpu.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None) #arrange一样,主要是生成浮点型
>>> np.linspace(2.0, 3.0, num=5)array([ 2. , 2.25, 2.5 , 2.75, 3. ])>>> np.linspace(2.0, 3.0, num=5, endpoint=False)array([ 2. , 2.2, 2.4, 2.6, 2.8])>>> np.linspace(2.0, 3.0, num=5, retstep=True)(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
numpy.logspace(start,stop,num=50,endpoint=True,base=10.0,dtype=None) #log函数.base默认值为10
>>> np.logspace(2.0, 3.0, num=4)array([ 100. , 215.443469 , 464.15888336, 1000. ])>>> np.logspace(2.0, 3.0, num=4, endpoint=False)array([ 100. , 177.827941 , 316.22776602, 562.34132519])>>> np.logspace(2.0, 3.0, num=4, base=2.0)array([ 4. , 5.0396842 , 6.34960421, 8. ])
numpy.geomspace(start,stop,num=50,endpoint=True,dtype=None) #几何级增长
>>> np.geomspace(1, 1000, num=4)array([ 1., 10., 100., 1000.])>>> np.geomspace(1, 1000, num=3, endpoint=False)array([ 1., 10., 100.])>>> np.geomspace(1, 1000, num=4, endpoint=False)array([ 1. , 5.62341325, 31.6227766 , 177.827941 ])>>> np.geomspace(1, 256, num=9)array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
numpy.emshgrid(*xi,**kwargs) #把向量坐标转化为矩阵坐标;在二维度数组中长度为M,N的的俩个数组作为输入:如果indexing='ij',则shape(M,N)如果indexing='xy'则shape(N.M)
>>> nx, ny = (3, 2)>>> x = np.linspace(0, 1, nx)>>> y = np.linspace(0, 1, ny)>>> xv, yv = np.meshgrid(x, y)>>> xvarray([[ 0. , 0.5, 1. ], [ 0. , 0.5, 1. ]])>>> yvarray([[ 0., 0., 0.], [ 1., 1., 1.]])
numpy.diag(v,k=0) #提取对角或构建一个对角阵
>>> x = np.arange(9).reshape((3,3))>>> xarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])>>> np.diag(x)array([0, 4, 8])>>> np.diag(x, k=1)array([1, 5])>>> np.diag(x, k=-1array([3, 7])
numpy.diagflat(v,k=0) #一个扁平输入作为一个二维数组的对角
>>> np.diagflat([[1,2], [3,4]])array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
numpy.tri(N,M=None,k=0,dtype=<type 'float'>) #这个不会翻译,但是看数据有点映像
>>> np.tri(3, 5, 2, dtype=int)array([[1, 1, 1, 0, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1]])
numpy.vander(x,N=None,incresing=False) # 范德蒙式,程序不重要,重要的是科学计算
>>> x = np.array([1, 2, 3, 5])>>> N = 3>>> np.vander(x, N)array([[ 1, 1, 1], [ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]])
numpy.mat(data,dtype=None) #输入转化为矩阵,创建一个新变量,指向旧的数据
>>> x = np.array([[1, 2], [3, 4]])>>> m = np.asmatrix(x)>>> x[0,0] = 5>>> mmatrix([[5, 2], [3, 4]])
numpy.bmat(obj,ldict=None,gdict=None) #用字符串,嵌套序列,或者数组创建矩阵
>>> A = np.mat('1 1; 1 1')>>> B = np.mat('2 2; 2 2')>>> C = np.mat('3 4; 5 6')>>> D = np.mat('7 8; 9 0')>>> np.bmat([[A, B], [C, D]])matrix([[1, 1, 2, 2], [1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])>>> np.bmat(np.r_[np.c_[A, B], np.c_[C, D]])matrix([[1, 1, 2, 2], [1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])>>> np.bmat('A,B; C,D')matrix([[1, 1, 2, 2], [1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])