python-numpy
python中的數據
一維數據
用列表和集合表示
數組與列表的關系
列表:數據類型可以不同
數組:數據類型可以相同
多維數據
用列表表示
高維數據
用字典表示
高維數據僅利用最基本的二元關系展示數據之間的復雜結構。
N維數組對象
ndarray
Python已有列表類型,為什么需要一個數組對象(類型)?
看一下下面兩個例子就知道了。
def pySum():
a = [1,2,3,4]
b = [4,5,6,7]
c = []
for i in range(len(a)):
c.append(a[i]**2 + b[i]**2)
return c
print(pySum())
import numpy as np
def npSum():
a = np.array([1,2,3,4])
b = np.array([4,5,6,7])
c = a**2 + b**2
return c
print(npSum())
從上面兩個例子可以看出,Python自帶的list相當於標量化操作,而ndarray相當於向量化操作。
- 數組對象可以去掉元素間運算所需的循環,使一維向量更像單個數據
- 數組對象采用相同的數據類型,有助於節省運算和存儲空間
- numpy的底層是用c寫的,因而運算速度更快。
ndarray對象的屬性
屬性 | 說明 |
---|---|
.ndim | 秩,即軸的數量或維度的數量 |
.shape | ndarray對象的尺度,對於矩陣,n行m列 |
.size | ndarray對象元素的個數,相當於.shape中n*m的值 |
.dtype | ndarray對象的元素類型 |
.itemsize | ndarray對象中每個元素的大小,以字節為單位 |
下面體會一下實際用法:
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a.ndim
2
>>> a.shape
(2, 3)
>>> a.size
6
>>> a.dtype
dtype('int32')
>>> a.itemsize
4
ndarray數組的元素類型
數據類型 | 說明 |
---|---|
bool | 布爾類型,True或False |
intc | 與C語言中的int類型一致,一般是int32或int64 |
intp | 用於索引的整數,與C語言中ssize_t一致,int32或int64 |
int8 | 字節長度的整數,取值:[‐128, 127] |
int16 | 16位長度的整數,取值:[‐32768, 32767] |
int32 | 32位長度的整數,取值:[‐2^31, 2^31‐1] |
int64 | 64位長度的整數,取值:[‐2^63, 2^63‐1] |
uint8 | 8位無符號整數,取值:[0, 255] |
uint16 | 16位無符號整數,取值:[0, 65535] |
uint32 | 32位無符號整數,取值:[0, 232‐1] |
uint64 | 32位無符號整數,取值:[0, 264‐1] |
float16 | 16位半精度浮點數:1位符號位,5位指數,10位尾數 |
float32 | 32位半精度浮點數:1位符號位,8位指數,23位尾數 |
float64 | 64位半精度浮點數:1位符號位,11位指數,52位尾數 |
complex64 | 復數類型,實部和虛部都是32位浮點數 |
complex128 | 復數類型,實部和虛部都是64位浮點數 |
ndarray數組的創建方法
從Python中的列表、元組等類型創建ndarray數組
用法:
x = np.array(list/tuple)
x = np.array(list/tuple, dtype=np.float32)
當np.array()不指定dtype時,NumPy將根據數據情況關聯一個dtype類型
實例:
>>> x = np.array([1,2,3])
>>> x
array([1, 2, 3])
>>> print(x)
[1 2 3]
>>> y = np.array([4,5,6])
>>> print(y)
[4 5 6]
>>> z = np.array([[1,2],[3,4],(5,6)])
>>> print(z)
[[1 2]
[3 4]
[5 6]]
使用NumPy中函數創建ndarray數組,如:arange, ones, zeros等
函數 | 說明 |
---|---|
np.arange(n) | 類似range()函數,返回ndarray類型,元素從0到n‐1 |
np.ones(shape) | 根據shape生成一個全1數組,shape是元組類型 |
np.zeros(shape) | 根據shape生成一個全0數組,shape是元組類型 |
np.full(shape,val) | 根據shape生成一個數組,每個元素值都是val |
np.eye(n) | 創建一個正方的n*n單位矩陣,對角線為1,其余為0 |
實例:
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.ones((3,4))
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
>>> np.zeros((3,4),dtype=np.int32)
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
>>> np.eye(5)
array([[ 1., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])
>>> x = np.ones((2,3,4))
>>> print(x)
[[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]]
>>> x.shape
(2, 3, 4)
函數 | 說明 |
---|---|
np.ones_like(a) | 根據數組a的形狀生成一個全1數組 |
np.zeros_like(a) | 根據數組a的形狀生成一個全0數組 |
np.full_like(a,val) | 根據數組a的形狀生成一個數組,每個元素值都是val |
使用NumPy中其他函數創建ndarray數組
函數 | 說明 |
---|---|
np.linspace() | 根據起止數據等間距地填充數據,形成數組 |
np.concatenate() | 將兩個或多個數組合並成一個新的數組 |
>>> a = np.linspace(1,10,4)
>>> a
array([ 1., 4., 7., 10.])
>>> b = np.linspace(1,10,4,endpoint=False)
>>> b
array([ 1. , 3.25, 5.5 , 7.75])
>>> c = np.concatenate((a,b))
>>> c
array([ 1. , 4. , 7. , 10. , 1. , 3.25, 5.5 , 7.75])
從字節流(raw bytes)中創建ndarray數組
從文件中讀取特定格式,創建ndarray數組
ndarray數組的維度變換
方法 | 說明 |
---|---|
.reshape(shape) | 不改變數組元素,返回一個shape形狀的數組,原數組不變 |
.resize(shape) | 與.reshape()功能一致,但修改原數組 |
.swapaxes(ax1,ax2) | 將數組n個維度中兩個維度進行調換 |
.flatten() | 對組進行數降維,返回折疊后的一維數組,原數組不變 |
>>> a = np.ones((2,3,4), dtype=np.int32)
>>> a.reshape((3,8))
array([[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]])
>>> a
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
>>> a.resize((3,8))
>>> a
array([[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]])
>>> a = np.ones((2,3,4), dtype=np.int32)
>>> a.flatten()
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1])
>>> a
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
>>> b = a.flatten()
>>> b
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1])
ndarray數組的類型變換
>>> a = np.ones((2,3,4), dtype=np.int32)
>>> a.astype(np.float)
array([[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]])
>>> a
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
ndarray數組向列表的轉換
>>> a = np.full((2,3,4),25,dtype=np.int)
>>> a
array([[[25, 25, 25, 25],
[25, 25, 25, 25],
[25, 25, 25, 25]],
[[25, 25, 25, 25],
[25, 25, 25, 25],
[25, 25, 25, 25]]])
>>> a.tolist()
[[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]], [[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]]]
數組的索引和切片
索引:獲取數組中特定位置元素的過程
切片:獲取數組元素子集的過程
一維數組的索引和切片:與Python的列表類似
多維數組的切片也類似
ndarray數組的運算
數組與標量之間的運算
數組與標量之間的運算作用於數組的每一個元素
NumPy一元函數
函數 | 說明 |
---|---|
np.abs(x) np.fabs(x) | 計算數組各元素的絕對值 |
np.sqrt(x) | 計算數組各元素的平方根 |
np.square(x) | 計算數組各元素的平方 |
np.log(x) np.log10(x) np.log2(x) | 計算數組各元素的自然對數、10底對數和2底對數 |
np.ceil(x) np.floor(x) | 計算數組各元素的ceiling值或floor值 |
np.rint(x) | 計算數組各元素的四舍五入值 |
np.modf(x) | 將數組各元素的小數和整數部分以兩個獨立數組形式返回 |
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) | 計算數組各元素的普通型和雙曲型三角函數 |
np.exp(x) | 計算數組各元素的指數值 |
np.sign(x) | 計算數組各元素的符號值,1(+), 0, ‐1(‐) |
>>> a = np.arange(24).reshape((2,3,4))
>>> np.square(a)
array([[[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]],
[[144, 169, 196, 225],
[256, 289, 324, 361],
[400, 441, 484, 529]]], dtype=int32)
>>> a = np.sqrt(a)
>>> a
array([[[ 0. , 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974, 2.64575131],
[ 2.82842712, 3. , 3.16227766, 3.31662479]],
[[ 3.46410162, 3.60555128, 3.74165739, 3.87298335],
[ 4. , 4.12310563, 4.24264069, 4.35889894],
[ 4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
>>> np.modf(a)
(array([[[ 0. , 0. , 0.41421356, 0.73205081],
[ 0. , 0.23606798, 0.44948974, 0.64575131],
[ 0.82842712, 0. , 0.16227766, 0.31662479]],
[[ 0.46410162, 0.60555128, 0.74165739, 0.87298335],
[ 0. , 0.12310563, 0.24264069, 0.35889894],
[ 0.47213595, 0.58257569, 0.69041576, 0.79583152]]]), array([[[ 0., 1., 1., 1.],
[ 2., 2., 2., 2.],
[ 2., 3., 3., 3.]],
[[ 3., 3., 3., 3.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.]]]))
NumPy二元函數
函數 | 說明 |
---|---|
+ ‐ * / ** | 兩個數組各元素進行對應運算 |
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin() | 元素級的最大值/最小值計算 |
np.mod(x,y) | 元素級的模運算 |
np.copysign(x,y) | 將數組y中各元素值的符號賦值給數組x對應元素 |
> < >= <= == != | 算術比較,產生布爾型數組 |
參考