NumPy
- python list和Numpy的list
In [10]: L3 = [True, "2", 3.0, 4]
In [11]: [type(item) for item in L3]
Out[11]: [bool, str, float, int]
python這種靈活性是要付出一定的代價(jià):要允許這些靈活的類型稠项,列表中的每個(gè)項(xiàng)目都必須包含自己的類型信息,引用計(jì)數(shù)和其他信息-也就是說粥谬,每個(gè)項(xiàng)目都是一個(gè)完整的Python對(duì)象宙地。在所有變量都是同一類型的特殊情況下送漠,許多信息都是多余的:將數(shù)據(jù)存儲(chǔ)在固定類型的數(shù)組中會(huì)更加有效搂蜓。下圖說明了動(dòng)態(tài)類型列表和固定類型(NumPy樣式)數(shù)組之間的區(qū)別:
從上面實(shí)現(xiàn)可以看到狼荞,數(shù)組本質(zhì)上包含一個(gè)指向一個(gè)連續(xù)數(shù)據(jù)塊的指針。另一方面帮碰,Python列表包含一個(gè)指向一組指針的指針相味,每個(gè)指針都指向一個(gè)完整的Python對(duì)象,例如我們之前看到的Python整數(shù)殉挽。同樣丰涉,python列表的優(yōu)點(diǎn)是靈活性:由于每個(gè)列表元素都是包含數(shù)據(jù)和類型信息的完整結(jié)構(gòu),因此可以用任何所需類型的數(shù)據(jù)填充列表斯碌。固定類型的NumPy樣式的數(shù)組缺乏這種靈活性一死,但是在存儲(chǔ)和處理數(shù)據(jù)方面效率更高。
Python提供了幾種不同的選項(xiàng)來將數(shù)據(jù)存儲(chǔ)在高效的固定類型數(shù)據(jù)緩沖區(qū)中傻唾。內(nèi)置的數(shù)組模塊(自Python 3.3起可用)可用于創(chuàng)建統(tǒng)一類型的密集數(shù)組:
In [12]:
...: import array
...: L = list(range(10))
...: A = array.array('i', L)
...: A
...:
...:
Out[12]: array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
這里的i表示數(shù)組類型是integer
ython的數(shù)組對(duì)象提供了基于數(shù)組的數(shù)據(jù)的有效存儲(chǔ)投慈,但是NumPy包的ndarray對(duì)象更加有用,NumPy對(duì)該數(shù)據(jù)進(jìn)行了有效的操作冠骄。我們將在后面的部分中探討這些操作伪煤。下面,我們將演示創(chuàng)建NumPy數(shù)組的幾種方法凛辣。
In [14]: import numpy as np
In [15]: np.array([1,3,5,9])
Out[15]: array([1, 3, 5, 9])
與Python列表不同的是抱既,NumPy限于所有包含相同類型的數(shù)組。如果類型不匹配扁誓,則NumPy將在可能的情況下向上轉(zhuǎn)換(此處防泵,向上轉(zhuǎn)換為浮點(diǎn)型整數(shù))甚至?xí)D(zhuǎn)換Unicode類型如下:
In [16]: np.array([1,3,5,'3'])
Out[16]: array(['1', '3', '5', '3'], dtype='<U11')
In [17]: np.array([3.14, 4, 2, 3])
Out[17]: array([3.14, 4. , 2. , 3. ])
# 指定類型
In [18]: np.array([1, 2, 3, 4], dtype='float32')
Out[18]: array([1., 2., 3., 4.], dtype=float32)
另外numpy的array還可以創(chuàng)建多維數(shù)組
In [19]: np.array([range(i,i+4) for i in [1,2,3]])
Out[19]:
array([[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
特別是對(duì)于較大的數(shù)組,使用內(nèi)置在NumPy中使用常規(guī)從頭開始創(chuàng)建數(shù)組會(huì)更有效率跋理。下面幾個(gè)例子說明
In [20]: # 長(zhǎng)度為10择克,默認(rèn)填充0的int類型數(shù)組
...: np.zeros(10, dtype=int)
Out[20]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
#創(chuàng)建默認(rèn)為1 的3*5 float類型的數(shù)組
In [21]: np.ones((3,5),dtype=float)
Out[21]:
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
In [22]: # Create a 3x5 array 指定使用 3.14填充
...: np.full((3, 5), 3.14)
Out[22]:
array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])
# 創(chuàng)建從1到20步長(zhǎng)2的數(shù)組
In [24]: np.arange(1,10,2)
Out[24]: array([1, 3, 5, 7, 9])
In [26]: #從0到1的長(zhǎng)度,取間隔相同的四個(gè)點(diǎn)
...: np.linspace(0, 1, 4)
Out[26]: array([0. , 0.33333333, 0.66666667, 1. ])
In [29]: # Create a 3x3 array of uniformly distributed
...: # random values between 0 and 1
...: np.random.random((3, 3))
Out[29]:
array([[0.42978944, 0.54364465, 0.54672853],
[0.51082736, 0.97954524, 0.80516577],
[0.50711922, 0.71589174, 0.85489927]])
In [31]: # 0-10 3*3 的隨機(jī)整數(shù)
...: np.random.randint(0, 10, (3, 3))
Out[31]:
array([[2, 4, 6],
[5, 4, 6],
[6, 9, 7]])
In [35]: #返回一個(gè)2維數(shù)組前普,對(duì)角線上1肚邢,其他位置為零。
...: np.eye(2, dtype=int)
Out[35]:
array([[1, 0],
[0, 1]])
- NumPy 的標(biāo)準(zhǔn)類型
NumPy數(shù)組包含單個(gè)類型的值拭卿。因?yàn)镹umPy是用C內(nèi)置的骡湖,所以C,F(xiàn)ortran和其他相關(guān)語言的用戶會(huì)熟悉這些類型峻厚。
在創(chuàng)建NumPy 數(shù)據(jù)可以指定類型
In [39]: np.zeros(3,dtype='int8')
Out[39]: array([0, 0, 0], dtype=int8)
或者用numpy指定
In [40]: np.zeros(3,dtype=np.int8)
Out[40]: array([0, 0, 0], dtype=int8)
具體類型可參考:
Data type Description
bool_ Boolean (True or False) stored as a byte
int_ Default integer type (same as C long; normally either int64 or int32)
intc Identical to C int (normally int32 or int64)
intp Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_ Shorthand for complex128.
complex64 Complex number, represented by two 32-bit floats
complex128 Complex number, represented by two 64-bit floats