1. 列表與數(shù)組
列表:數(shù)據(jù)類型可以不同;
數(shù)組:數(shù)據(jù)類型相同
2. ndarray: NumPy的數(shù)組對(duì)象
NumPy是SciPy踊赠、Pandas庫的基礎(chǔ)
#1. 安裝
$ pip3 install numpy
#我的服務(wù)器下顯示已安裝
Requirement already satisfied: numpy in ./miniconda3/lib/python3.6/site-packages (1.15.4)
#2. 引用: import numpy as np
$ python3 #進(jìn)入交互式編程環(huán)境
>>> import numpy as np
>>> def npsum():
... a = np.array([0,1,2,3])
... b = np.array([3,2,1,0])
... c = a**2 + b**2
... return c
...
>>> print(npsum())
[9 5 5 9]
3. ndarray實(shí)例
ndarray在程序中叫array, np.array()用于創(chuàng)建一個(gè)ndarray數(shù)組锦爵。
>>> a = np.array([[1,2,3],
... [4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> print(a)
[[1 2 3]
[4 5 6]]
4. ndarray對(duì)象的屬性
.ndim -- 軸/維度的數(shù)量
.shape -- 幾行幾列
.size -- 元素的個(gè)數(shù)
.dtype -- 元素類型
.itemsize -- 每個(gè)元素字節(jié)大小
>>> a.ndim
2
>>> a.shape
(2, 3)
>>> a.size
6
>>> a.dtype
dtype('int64')
>>> a.itemsize
8
5. ndarray數(shù)組的創(chuàng)建方法
- 從Python列表、元組類型創(chuàng)建ndarray數(shù)組
x = np.array(list/tuple[, dtype=np.float32])
eg: x = np.array([[1,2],(3,4)]) - 使用NumPy中的函數(shù)創(chuàng)建:np.arrange(n)生成0到n-1、np.full(shape,val)根據(jù)shape元組生成元素值都為val的數(shù)組延曙、np.full_like(a,val)根據(jù)數(shù)組a的形狀生成數(shù)組、np.linspace()
6. ndarray數(shù)組的維度變換和類型變換
方法 | 說明 |
---|---|
.reshape(shape) | 不改變數(shù)組元素亡哄,返回shape形狀的數(shù)組枝缔,原數(shù)組不變 |
.resize(shape) | 與.reshape()功能一致,但修改原數(shù)組 |
.swapaxes(ax1,ax2) | 將數(shù)組n個(gè)維度中兩個(gè)維度進(jìn)行調(diào)換 |
.flatten() | 對(duì)數(shù)組進(jìn)行降維蚊惯,返回折疊后的一維數(shù)組愿卸,原數(shù)組不變 |
.astype(new_type) | 創(chuàng)建新的數(shù)組(原始數(shù)據(jù)的拷貝),即使兩個(gè)類型一致 |
>>> a=np.ones((2,3),dtype=np.int32)
>>> print(a)
[[1 1 1]
[1 1 1]]
>>> a.reshape((6))
array([1, 1, 1, 1, 1, 1], dtype=int32)
>>> print(a)
[[1 1 1]
[1 1 1]]
>>> a.resize(6)
>>> print(a)
[1 1 1 1 1 1]
>>> a.astype(np.float)
array([ 1., 1., 1., 1., 1., 1.])
>>> print(a)
[1 1 1 1 1 1]
7. 數(shù)組的索引和切片
>>> b=np.array([9,8,7,6,5,4,3,2,1,0])
>>> b[2]
7
>>> b[0:6:2] #起始編號(hào):終止編號(hào)(不含):步長
array([9, 7, 5])
>>> b=b.reshape(2,5)
>>> print(b)
[[9 8 7 6 5]
[4 3 2 1 0]]
>>> b[1,2] #一個(gè)維度一個(gè)索引值截型,逗號(hào)分隔趴荸,[]括起來
2
>>> b[1,:]
array([4, 3, 2, 1, 0]) #一個(gè)維度全選用:
>>> b[1,::2] #此時(shí)也可以結(jié)合步長使用
array([4, 2, 0])
8. 數(shù)組與標(biāo)量之間的運(yùn)算
數(shù)組與標(biāo)量之間的運(yùn)算作用于數(shù)組的每一個(gè)元素
>>> b.mean()
4.5
>>> b-b.mean()
array([[ 4.5, 3.5, 2.5, 1.5, 0.5],
[-0.5, -1.5, -2.5, -3.5, -4.5]])
9. NumPy一元/二元運(yùn)算函數(shù)
對(duì)ndarray中的數(shù)據(jù)執(zhí)行元素級(jí)運(yùn)算的函數(shù)
一元函數(shù)
函數(shù) | 說明 |
---|---|
np.abs(x) np.fabs(x) | 計(jì)算數(shù)組各元素的絕對(duì)值 |
np.sqrt(x) | 計(jì)算數(shù)組各元素的平方根 |
np.square(x) | 計(jì)算數(shù)組各元素的平方 |
np.log(x) np.log10(x) np.log2(x) | 計(jì)算數(shù)組各元素的自然對(duì)數(shù)、10底對(duì)數(shù)和2底對(duì)數(shù) |
np.ceil(x) np.floor(x) | 計(jì)算數(shù)組各元素的ceiling值 或 floor值 |
np.rint(x) | 計(jì)算數(shù)組各元素的四舍五入值 |
np.modf(x) | 將數(shù)組各元素的小數(shù)和整數(shù)部分以兩個(gè)獨(dú)立數(shù)組形式返回 |
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) | 計(jì)算數(shù)組各元素的普通型和雙曲型三角函數(shù) |
np.exp(x) | 計(jì)算數(shù)組各元素的指數(shù)值 |
np.sign(x) | 計(jì)算數(shù)組各元素的符號(hào)值宦焦,1(+), 0, ‐1(‐) |
二元函數(shù)
函數(shù) | 說明 |
---|---|
+ ‐ * / ** | 兩個(gè)數(shù)組各元素進(jìn)行對(duì)應(yīng)運(yùn)算 |
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin() | 元素級(jí)的最大值/最小值計(jì)算 |
np.mod(x,y) | 元素級(jí)的模運(yùn)算 |
np.copysign(x,y) | 將數(shù)組y中各元素值的符號(hào)賦值給數(shù)組x對(duì)應(yīng)元素 |
> < >= <= == != | 算術(shù)比較发钝,產(chǎn)生布爾型數(shù)組 |