Numpy
NumPy是SciPy单料、Pandas等數(shù)據(jù)處理或科學計算庫的基礎
NumPy是一個開源的Python科學計算基礎庫辑莫,包含:
- 一個強大的N維數(shù)組對象 ndarray
- 廣播功能函數(shù)
- 整合C/C++/Fortran代碼的工具
- 線性代數(shù)桥言、傅里葉變換脂新、隨機數(shù)生成等功能
引入:
import numpy as np
N維數(shù)組對象:ndarray
Python已有列表類型熙含,為什么需要一個數(shù)組對象(類型)?
- 數(shù)組對象可以去掉元素間運算所需的循環(huán)割粮,使一維向量更像單個數(shù)據(jù)
- 設置專門的數(shù)組對象盾碗,經(jīng)過優(yōu)化,可以提升這類應用的運算速度
- 觀察:科學計算中舀瓢,一個維度所有數(shù)據(jù)的類型往往相同
- 數(shù)組對象采用相同的數(shù)據(jù)類型廷雅,有助于節(jié)省運算和存儲空間
ndarray是一個多維數(shù)組對象,由兩部分構成:
- 實際的數(shù)據(jù)
- 描述這些數(shù)據(jù)的元數(shù)據(jù)(數(shù)據(jù)維度京髓、數(shù)據(jù)類型等)
[In]:
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
[Out]:
[[0 1 2 3 4]
[9 8 7 6 5]]
np.array()生成一個ndarray數(shù)組
ndarray對象的屬性
屬性 | 說明 |
---|---|
.ndim | 秩航缀,即軸的數(shù)量或維度的數(shù)量 |
.shape | ndarray對象的尺度,對于矩陣堰怨,n行m列 |
.size | ndarray對象元素的個數(shù)芥玉,相當于.shape中n*m的值 |
.dtype | ndarray對象的元素類型 |
.itemsize | ndarray對象中每個元素的大小,以字節(jié)為單位 |
ndarray的元素類型
數(shù)據(jù)類型 | 說明 |
---|---|
bool | 布爾類型备图,True或False |
intc | 與C語言中的int類型一致灿巧,一般是int32或int64 |
intp | 用于索引的整數(shù),與C語言中ssize_t一致诬烹,int32或int64 |
int8 | 字節(jié)長度的整數(shù)砸烦,取值:[‐128,127] |
int16 | 16位長度的整數(shù),取值:[‐32768,32767] |
int32 | 32位長度的整數(shù)绞吁,取值:[‐2^31 ,2^31 ‐1] |
int64 | 64位長度的整數(shù)幢痘,取值:[‐2^63 ,2^63 ‐1] |
uint8 | 8位無符號整數(shù),取值:[0,255] |
uint16 | 16位無符號整數(shù)家破,取值:[0,65535] |
uint32 | 32位無符號整數(shù)颜说,取值:[0,2^32 ‐1] |
uint64 | 32位無符號整數(shù),取值:[0,2^64 ‐1] |
float16 | 16位半精度浮點數(shù):1位符號位汰聋,5位指數(shù)门粪,10位尾數(shù) |
float32 | 32位半精度浮點數(shù):1位符號位,8位指數(shù)烹困,23位尾數(shù) |
float64 | 64位半精度浮點數(shù):1位符號位玄妈,11位指數(shù),52位尾數(shù) |
complex64 | 復數(shù)類型髓梅,實部和虛部都是32位浮點數(shù) |
complex128 | 復數(shù)類型拟蜻,實部和虛部都是64位浮點數(shù) |
實部(.real) + j虛部(.imag)
ndarray數(shù)組的創(chuàng)建方法
- 從Python中的列表、元組等類型創(chuàng)建ndarray數(shù)組
- 使用NumPy中函數(shù)創(chuàng)建ndarray數(shù)組枯饿,如:arange,ones,zeros等
- 從字節(jié)流(raw bytes)中創(chuàng)建ndarray數(shù)組
- 從文件中讀取特定格式酝锅,創(chuàng)建ndarray數(shù)組
1.從Python中的列表、元組等類型創(chuàng)建ndarray數(shù)組
x = np.array(list/tuple, dtype=np.float32)
不指定dtype時奢方,Numpy將根據(jù)數(shù)據(jù)情況關聯(lián)一個dtype類型
從列表類型創(chuàng)建:
[In]:
a = np.array([0,1,2,3,4])
[Out]:
[0 1 2 3 4]
從元組類型創(chuàng)建:
[In]:
a = np.array((4,5,6,7))
[Out]:
[4 5 6 7]
從列表和元組混合類型創(chuàng)建:
[In]:
a = np.array([[1,2],[9,8],(0.1,0.2)])
[Out]:
[[ 1. 2. ]
[ 9. 8. ]
[ 0.1 0.2]]
2.使用NumPy中函數(shù)創(chuàng)建ndarray數(shù)組搔扁,如:arange,ones,zeros等
函數(shù) | 說明 |
---|---|
np.arange(n) | 類似range()函數(shù)爸舒,返回ndarray類型,元素從0到n‐1 |
np.ones(shape) | 根據(jù)shape生成一個全1數(shù)組稿蹲,shape是元組類型 |
np.zeros(shape) | 根據(jù)shape生成一個全0數(shù)組扭勉,shape是元組類型 |
np.full(shape,val) | 根據(jù)shape生成一個數(shù)組,每個元素值都是val |
np.eye(n) | 創(chuàng)建一個正方的n*n單位矩陣场绿,對角線為1剖效,其余為0 |
np.ones_like(a) | 根據(jù)數(shù)組a的形狀生成一個全1數(shù)組 |
np.zeros_like(a) | 根據(jù)數(shù)組a的形狀生成一個全0數(shù)組 |
np.full_like(a,val) | 根據(jù)數(shù)組a的形狀生成一個數(shù)組,每個元素值都是val |
3.使用NumPy中其他函數(shù)創(chuàng)建ndarray數(shù)組
函數(shù) | 說明 |
---|---|
np.linspace() | 根據(jù)起止數(shù)據(jù)等間距地填充數(shù)據(jù)焰盗,形成數(shù)組 |
np.concatenate() | 將兩個或多個數(shù)組合并成一個新的數(shù)組 |
[In]:
a = np.linspace(1,10,4)
[Out]:
[ 1. 4. 7. 10.]
[In]:
a = np.linspace(1,10,4,endpoint=False)
[Out]:
[ 1. 3.25 5.5 7.75]
ndarray數(shù)組的變換
方法 | 說明 |
---|---|
.reshape(shape) | 不改變數(shù)組元素,返回一個shape形狀的數(shù)組咒林,原數(shù)組不變 |
.resize(shape) | 與.reshape()功能一致熬拒,但修改原數(shù)組 |
.swapaxes(ax1,ax2) | 將數(shù)組n個維度中兩個維度進行調換 |
.flatten() | 對數(shù)組進行降維,返回折疊后的一維數(shù)組垫竞,原數(shù)組不變 |
ndarray數(shù)組的類型變換
new_a = a.astype(new_type)
[In]:
a = np.ones((2,3,4),dtype=np.int32)
b = a.astype(np.float32)
[Out]:
[[[ 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數(shù)組向列表的轉換
ls = a.tolist()
[In]:
a = np.full((2,3,4),25,dtype=np.int32)
b = a.tolist()
[Out]:
[[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]], [[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]]]
ndarray數(shù)組的操作
數(shù)組的索引和切片
- 索引:獲取數(shù)組中特定位置元素的過程
- 切片:獲取數(shù)組元素子集的過程
一維數(shù)組的索引和切片:與Python的列表類似
[In]:
a = np.array([9,8,7,6,5])
b = a[1:4:2]
[Out]:
[8 6]
起始編號:終止編號(不含):步長
多維數(shù)組的索引:
第二個數(shù)組澎粟,第三行第四列;
倒數(shù)第一個數(shù)組欢瞪,倒數(shù)第二行活烙,倒數(shù)第三列:
[In]:
a = np.arange(24).reshape((2,3,4))
b = a[1,2,3]
c = a[-1,-2,-3]
[Out]:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
23
17
多維數(shù)組的切片:
選取一個維度用;
每一個維度的切片方法與一維數(shù)組相同遣鼓;
每個維度可以使用步長跳躍切片啸盏;
[In]:
a = np.arange(24).reshape((2,3,4))
b = a[:,1,-3]
c = a[:,1:3,:]
d = a[:,:,::2]
[Out]:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
b:
[5 17]
c:
[[[ 4 5 6 7]
[ 8 9 10 11]]
[[16 17 18 19]
[20 21 22 23]]]
d:
[[[ 0 2]
[ 4 6]
[ 8 10]]
[[12 14]
[16 18]
[20 22]]]
ndarray數(shù)組的運算
數(shù)組與標量之間的運算
數(shù)組與標量之間的運算作用于數(shù)組的每一個元素
[In]:
a = np.arange(24).reshape((2,3,4))
b = a.mean()
c = a / a.mean()
[Out]
11.5
[[[ 0. 0.08695652 0.17391304 0.26086957]
[ 0.34782609 0.43478261 0.52173913 0.60869565]
[ 0.69565217 0.7826087 0.86956522 0.95652174]]
[[ 1.04347826 1.13043478 1.2173913 1.30434783]
[ 1.39130435 1.47826087 1.56521739 1.65217391]
[ 1.73913043 1.82608696 1.91304348 2. ]]]
NumPy一元函數(shù)
函數(shù) | 說明 |
---|---|
np.abs(x) np.fabs(x) | 計算數(shù)組各元素的絕對值 |
np.sqrt(x) | 計算數(shù)組各元素的平方根 |
np.square(x) | 計算數(shù)組各元素的平方 |
np.log(x) np.log10(x) np.log2(x) | 計算數(shù)組各元素的自然對數(shù)、10底對數(shù)和2底對數(shù) |
np.ceil(x) np.floor(x) | 計算數(shù)組各元素的ceiling值 或 floor值 |
np.rint(x) | 計算數(shù)組各元素的四舍五入值 |
np.modf(x) | 將數(shù)組各元素的小數(shù)和整數(shù)部分以兩個獨立數(shù)組形式返回 |
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) | 計算數(shù)組各元素的普通型和雙曲型三角函數(shù) |
np.exp(x) | 計算數(shù)組各元素的指數(shù)值 |
np.sign(x) | 計算數(shù)組各元素的符號值骑祟,1(+), 0, ‐1(‐) |
NumPy二元函數(shù)
函數(shù) | 說明 |
---|---|
+ ‐ * / ** | 兩個數(shù)組各元素進行對應運算 |
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin() | 元素級的最大值/最小值計算 |
np.mod(x,y) | 元素級的模運算 |
np.copysign(x,y) | 將數(shù)組y中各元素值的符號賦值給數(shù)組x對應元素 |
> < >= <= == != | 算術比較回懦,產(chǎn)生布爾型數(shù)組 |