ndarray 數(shù)組除了可以使用底層 ndarray 構(gòu)造器來(lái)創(chuàng)建外,也可以通過(guò)以下幾種方式來(lái)創(chuàng)建。
numpy.empty 方法用來(lái)創(chuàng)建一個(gè)指定形狀(shape)、數(shù)據(jù)類型(dtype)且未初始化的數(shù)組:
numpy.empty(shape, dtype = float, order = 'C')
返回給定形狀和類型的新數(shù)組,而無(wú)需初始化條目。
參數(shù)
prototype - 空數(shù)組的形狀。dtype(可選) - 數(shù)組所需的輸出數(shù)據(jù)類型,例如numpy.int8。默認(rèn)值為 numpy.float64。order(可選,默認(rèn):'C') - 有"C"和"F"兩個(gè)選項(xiàng),分別代表,行優(yōu)先和列優(yōu)先,在計(jì)算機(jī)內(nèi)存中的存儲(chǔ)元素的順序。
返回值:給定數(shù)組形狀,但數(shù)據(jù)類型和順序未初始化(任意)數(shù)據(jù)的數(shù)組。
>>> import numpy as np >>> np.empty([2, 2]) array([[1., 0.], [0., 1.]]) >>> np.empty([2, 2], dtype=int) array([[4607182418800017408, 0], [ 0, 4607182418800017408]]) >>>
numpy.empty 返回形狀和類型與給定數(shù)組相同的新數(shù)組。
numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None)
返回形狀和類型與給定數(shù)組相同的新數(shù)組
參數(shù)
prototype - 源數(shù)組的形狀和數(shù)據(jù)類型定義了返回?cái)?shù)組的這些相同屬性。dtype - 覆蓋結(jié)果的數(shù)據(jù)類型。order - 覆蓋結(jié)果的內(nèi)存布局。如果prototypeFortran連續(xù),則“ A”表示“ F”,否則為“ C”?!?K”表示prototype 盡可能匹配布局。subok - 如果為True,則新創(chuàng)建的數(shù)組將使用'a'的子類類型,否則它將是基類數(shù)組。默認(rèn)為True。shape - 覆蓋結(jié)果的形狀。如果order ='K'并且尺寸數(shù)量不變,將嘗試保持順序,否則,則暗示order ='C'。
返回值:形狀和類型與原型相同的未初始化(任意)數(shù)據(jù)數(shù)組。
>>> import numpy as np >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[ 6917529027641081856, -6917520256071729910, 98], [ 0, 0, 0]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ 2.68156159e+154, 2.68156159e+154, 3.32479618e+181], [ 1.78476163e+185, -1.73059781e-077, 4.21535080e-309]])
創(chuàng)建指定大小的數(shù)組,數(shù)組元素以 0 來(lái)填充:
numpy.zeros(shape, dtype = float, order = 'C')
參數(shù)
shape - 空數(shù)組的形狀。dtype - 數(shù)組所需的輸出數(shù)據(jù)類型,例如numpy.int8。默認(rèn)值為 numpy.float64。order - '{'C','F'},可選,默認(rèn):'C',表示以行(C)或列(Fortran)樣式的順序存儲(chǔ)數(shù)據(jù)在內(nèi)存中。
返回值:給定數(shù)組形狀,數(shù)據(jù)類型和順序的零數(shù)組。
>>> import numpy as np >>> np.zeros(5) array([0., 0., 0., 0., 0.]) >>> np.zeros((5,), dtype=int) array([0, 0, 0, 0, 0]) >>> np.zeros((2, 1)) array([[0.], [0.]]) >>> s = (2,2) >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4' ),="" ('y',="" '
zeros_like返回與給定數(shù)組具有相同形狀和類型的零數(shù)組。
參數(shù)
a - 一個(gè)限定返回的數(shù)組形狀和數(shù)據(jù)類型的屬性。。dtype - 數(shù)組所需的輸出數(shù)據(jù)類型。order - 覆蓋結(jié)果的內(nèi)存布局。subok - 如果為True,則新創(chuàng)建的數(shù)組將使用'a'的子類類型,否則它將是基類數(shù)組。默認(rèn)為True。shapeint - 覆蓋結(jié)果的形狀。返回值:一個(gè)具有相同形狀和類型的零的陣列。
>>> import numpy as np >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> print(x) [[0 1 2] [3 4 5]] >>> np.zeros_like(x) array([[0, 0, 0], [0, 0, 0]]) >>> y = np.arange(3, dtype=float) >>> y array([0., 1., 2.]) >>> np.zeros_like(y) array([0., 0., 0.])
創(chuàng)建指定形狀的數(shù)組,數(shù)組元素以 1 來(lái)填充:
numpy.ones(shape, dtype = None, order = 'C')
參數(shù)
shape - 數(shù)組形狀。dtype - 數(shù)據(jù)類型,可選order - 'C' 用于 C 的行數(shù)組,或者 'F' 用于 FORTRAN 的列數(shù)組
>>> import numpy as np >>> np.ones(5) array([1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=int) array([1, 1, 1, 1, 1]) >>> np.ones((2, 1)) array([[1.], [1.]]) >>> s = (2,2) >>> np.ones(s) array([[1., 1.], [1., 1.]])
zeros_like返回形狀與類型與給定數(shù)組相同的數(shù)組。
numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
參數(shù)
a - 一個(gè)限定返回的數(shù)組形狀和數(shù)據(jù)類型的屬性。。dtype - 數(shù)組所需的輸出數(shù)據(jù)類型。order - 覆蓋結(jié)果的內(nèi)存布局。subok - 如果為True,則新創(chuàng)建的數(shù)組將使用'a'的子類類型,否則它將是基類數(shù)組。默認(rèn)為True。shape - 覆蓋結(jié)果的形狀。
返回值:一個(gè)具有相同形狀和類型的零的陣列。
>>> import numpy as np >>> 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]]) >>> y = np.arange(3, dtype=float) >>> y array([0., 1., 2.]) >>> np.ones_like(y) array([1., 1., 1.])
numpy.arange([ start,] stop,[ step,] dtype = None )
返回給定間隔內(nèi)的均勻間隔的值。
在半開(kāi)間隔 (即包括start但不包括stop的間隔)內(nèi)生成值。對(duì)于整數(shù)參數(shù),該函數(shù)等效于Python內(nèi)置的 range函數(shù),但返回ndarray而不是列表。[start, stop)
參數(shù)
start - 間隔的開(kāi)始。間隔包括該值。默認(rèn)起始值為0。stop - 間隔結(jié)束。該間隔不包括該值,除非在某些情況下,step不是整數(shù),并且浮點(diǎn)舍入會(huì)影響out的長(zhǎng)度。step - 值之間的間距。對(duì)于任何輸出出來(lái),這是兩個(gè)相鄰值之間的距離,默認(rèn)步長(zhǎng)為1。dtype - 輸出數(shù)組的類型。
返回值:均勻間隔的值的數(shù)組。
>>> import numpy as np >>> 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])
numpy.linspace 函數(shù)用于創(chuàng)建一個(gè)一維數(shù)組,數(shù)組是一個(gè)等差數(shù)列構(gòu)成的,格式如下:
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
參數(shù)
start - 序列的起始值。stop - 序列的終止值,如果endpoint為true,該值包含于數(shù)列中。num - 要生成的等步長(zhǎng)的樣本數(shù)量,默認(rèn)為50。endpoint - 該值為 true 時(shí),數(shù)列中包含stop值,反之不包含,默認(rèn)是True。retstep - 如果為 True 時(shí),生成的數(shù)組中會(huì)顯示間距,反之不顯示。dtype - ndarray 的數(shù)據(jù)類型。
以下實(shí)例用到三個(gè)參數(shù),設(shè)置起始點(diǎn)為 1 ,終止點(diǎn)為 10,數(shù)列個(gè)數(shù)為 10。
>>> import numpy as np >>> a = np.linspace(1,10,10) >>> print(a) [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] >>> a = np.linspace(10, 20, 5, endpoint = False) >>> print(a) [10. 12. 14. 16. 18.] >>> a =np.linspace(1,10,10,retstep= True) >>> print(a) (array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0) >>> b =np.linspace(1,10,10).reshape([10,1]) >>> print(b) [[ 1.] [ 2.] [ 3.] [ 4.] [ 5.] [ 6.] [ 7.] [ 8.] [ 9.] [10.]]
直接對(duì) Python 的基礎(chǔ)數(shù)據(jù)類型(如列表、元組等)進(jìn)行轉(zhuǎn)換來(lái)生成 ndarray:
>>> import numpy as np >>> ls1 = [10, 42, 0, -17, 30] >>> nd1 =np.array(ls1) >>> print(nd1) [ 10 42 0 -17 30] >>> >>> print(type(nd1))
numpy.asarray類似 numpy.array,但 numpy.asarray 參數(shù)只有三個(gè),比 numpy.array 少兩個(gè)。
numpy.asarray(a, dtype = None, order = None)
參數(shù)
a - 任意形式的輸入?yún)?shù),可以是,列表, 列表的元組, 元組, 元組的元組, 元組的列表,多維數(shù)組。dtype - 數(shù)據(jù)類型,可選。order - 要生成的等步長(zhǎng)的樣本數(shù)量,默認(rèn)為50。endpoint - 可選,有"C"和"F"兩個(gè)選項(xiàng),分別代表,行優(yōu)先和列優(yōu)先,在計(jì)算機(jī)內(nèi)存中的存儲(chǔ)元素的順序。
>>> import numpy as np >>> x = [1,2,3] >>> a = np.asarray(x) >>> a array([1, 2, 3]) >>> x = (1,2,3) >>> a = np.asarray(x) >>> print (a) [1 2 3] >>> x = [(1,2,3),(4,5)] >>> a = np.asarray(x) >>> print(a) [(1, 2, 3) (4, 5)] >>> x = [1,2,3] >>> a = np.asarray(x, dtype = float) >>> print(a) [1. 2. 3.]
numpy.frombuffer 用于實(shí)現(xiàn)動(dòng)態(tài)數(shù)組。
numpy.frombuffer 接受 buffer 輸入?yún)?shù),以流的形式讀入轉(zhuǎn)化成 ndarray 對(duì)象。
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
參數(shù)
buffer - 可以是任意對(duì)象,會(huì)以流的形式讀入。dtype - 返回?cái)?shù)組的數(shù)據(jù)類型,可選count - 讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)。offset - 讀取的起始位置,默認(rèn)為0。
>>> import numpy as np >>> s = b'Hello Nhooo' >>> a = np.frombuffer(s, dtype = 'S1') >>> print(a) [b'H' b'e' b'l' b'l' b'o' b' ' b'L' b'i' b'd' b'i' b'h' b'u' b'o']
numpy.fromiter 方法從可迭代對(duì)象中建立 ndarray 對(duì)象,返回一維數(shù)組。
numpy.fromiter(iterable, dtype, count=-1)
參數(shù)
iterable - 可迭代對(duì)象。dtype - 返回?cái)?shù)組的數(shù)據(jù)類型count - 讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)。
>>> import numpy as np >>> # 使用 range 函數(shù)創(chuàng)建列表對(duì)象 >>> list=range(5) >>> it=iter(list) >>> x=np.fromiter(it, dtype=float) >>> print(x) [0. 1. 2. 3. 4.]
為了更有效地訓(xùn)練模型,提高模型的性能,有些初始化還需要滿足一定的條件,如滿足正態(tài)分布或均勻分布等。這里介紹了幾種 np.random 模塊中常用的方法,如下表所示。
函數(shù) | 描述 |
np.random.random | 生成0到1之間的隨機(jī)數(shù) |
np.random.uniform | 生成均勻分布的隨機(jī)數(shù) |
np.random.randn | 生成標(biāo)準(zhǔn)正態(tài)的隨機(jī)數(shù) |
np.random.randint | 生成隨機(jī)的整數(shù) |
np.random.normal | 生成正態(tài)分布 |
np.random.shuffle | 隨機(jī)打亂順序 |
np.random.seed | 設(shè)置隨機(jī)數(shù)種子 |
random_sample | 生成隨機(jī)的浮點(diǎn)數(shù) |
>>> import numpy as np >>> # 生成全是 0 的 3x3 矩陣 >>> nd5 =np.zeros([3, 3]) >>> print("nd5 =\n", nd5) nd5 = [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] >>> # 生成全是 1 的 3x3 矩陣 >>> nd6 = np.ones([3, 3]) >>> print("nd6 =\n", nd6) nd6 = [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] >>> >>> # 生成 4 階的單位矩陣 >>> nd7 = np.eye(4) >>> print("nd7 =\n", nd7) nd7 = [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] >>> # 生成 4 階對(duì)角矩陣 >>> nd8 = np.diag([1, 8, 3, 10]) >>> print("nd8 =\n", nd8) nd8 = [[ 1 0 0 0] [ 0 8 0 0] [ 0 0 3 0] [ 0 0 0 10]]