NumPy之array

ndarray是一個包含了相同元素類型和大小的多維數組。

創建數組

1、使用系統方法

empty(shape[, dtype, order])     # 根據給定的參數創建一個ndarray數組,值用隨機數填充

例:

>>> np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])

empty_like(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]])

eye(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.]])

identity(n[, dtype])   #生成一個正方形的數組即N×N類型的數組,且索引萬惡哦0的斜線上維1,其他元素維0

例:

>>> np.identity(3)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],       
       [ 0.,  0.,  1.]])

ones(shape[, dtype, order])   #生成一個指定shape和dtype的數組,用1填充

例:

>>> np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> np.ones((2, 1))
array([[ 1.],
       [ 1.]])

ones_like(a[, dtype, order, subok])  #和ones的區別就是需要給定一個dnarray模板,新生成的array繼承了a的shape和dtype

例:

>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.ones_like(x)
array([[1, 1, 1],
       [1, 1, 1]])

zeros(shape[, dtype, order])   #根據給定的shape,和dtype生成一個由0填充的數組

例:

>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])

zeros_like(a[, dtype, order, subok])   #根據a模板生成一個新的用0 填充的ndarray數組

例:

>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.zeros_like(x)
array([[0, 0, 0],
       [0, 0, 0]])

full(shape, fill_value[, dtype, order])   #用指定的值填充數組

例:

>>> np.full((2, 2), 10)
array([[10, 10],
       [10, 10]])

full_like(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、用指定的數據填充

array(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]])

asarray(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])

asanyarray(a[, dtype, order])  #通過ndarray的子類創建array

>>> a = [1, 2]
>>> np.asanyarray(a)
array([1, 2])

ascontiguousarray(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

asmatrix(data[, dtype])    # 把數組轉化爲矩陣,新的變量沒有copy數據,只是指向原有的數據

>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],        [3, 4]])

copy(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

frombuffer(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')

frombuffer(buffer[, dtype, count, offset]) #從文件讀取數據 ps:該方法不長用用save替代

fromfunction(function, shape, **kwargs) #用方法計算出來的數據填充數組

>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],
       [1, 2, 3],       
       [2, 3, 4]])

fromiter(iterable, dtype[, count]) #通過迭代器生成一個一維數組

>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, np.float)
array([  0.,   1.,   4.,   9.,  16.])

fromstring(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)
arange([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)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 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=-1
array([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
>>> m
matrix([[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]])


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章