numpy
numpy是一個(gè)開源的python科學(xué)計(jì)算庫,使用ndarray對象處理任意維度的數(shù)組
1.ndarray相對原生python列表優(yōu)勢:
1.內(nèi)存塊存儲(chǔ)優(yōu)勢:ndarray在存儲(chǔ)元素是內(nèi)存連續(xù)屿聋,而python原生list存儲(chǔ)元素是選擇元素外置的形式案淋,查找時(shí)通過尋址方式找到下一個(gè)元素匀奏,在科學(xué)計(jì)算時(shí)埂陆,ndarray的速度快于list乘盖。
2.ndarray支持并行化運(yùn)算(向量化運(yùn)算)
3.numpy底層編寫使用c語言早歇,內(nèi)部解除了GIL(全局解釋鎖)
2.ndarray的屬性:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a
array([[1, 2, 3],
? ? ? [4, 5, 6]])
ndarray.shape 數(shù)組維度的元組
a.shape
(2, 3)
ndarray.ndim數(shù)組維數(shù)
a.ndim
2
ndarray.size 數(shù)組眾元素的數(shù)量
a.size
6
ndarray.itemsize 一個(gè)數(shù)組元素的長度(字節(jié))
a.itemsize
4
ndarray.dtype? 數(shù)組元素的類型
a.dtype
dtype('int32')
3.創(chuàng)建數(shù)組的時(shí)候指定
數(shù)組
類型
a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)
a.dtype
dtype('float32')
4.astype方法顯式地轉(zhuǎn)換其dtype類型
a.astype(np.int64)
array([[1, 2, 3],
? ? ? [4, 5, 6]], dtype=int64)
5. 生成數(shù)組的方法
1 生成0和1的數(shù)組?
np.ones([3,4])?
array([[1., 1., 1., 1.],
? ? ? [1., 1., 1., 1.],
? ? ? [1., 1., 1., 1.]])
np.zeros([3,4])
array([[0., 0., 0., 0., 0.],
? ? ? [0., 0., 0., 0., 0.]])
2.從現(xiàn)有數(shù)組生成
b = np.array(a)
c = np.copy(a)
d = np.asarray(a)
array([[1, 2, 3],
? ? ? [4, 5, 6]], dtype=int64)
當(dāng)改變a中元素時(shí)倾芝,b,c中元素不改變,d中元素也隨之改變
b,c相當(dāng)于深拷貝箭跳,d相當(dāng)于淺拷貝
3.生成固定范圍的數(shù)組
np.linspace(start,stop,num,endpoint,retstep,dtype)
start 序列的起始值? ?stop 序列的終止值晨另,
如果endpoint為true,該值包含于序列中
num 要生成的等間隔樣例數(shù)量谱姓,默認(rèn)為50
endpoint 序列中是否包含stop值借尿,默認(rèn)為ture
retstep 如果為true,返回樣例屉来,以及連續(xù)數(shù)字之間的步長
dtype 輸出ndarray的數(shù)據(jù)類型
np.linspace(0,100,10)
array([ 0. , 11.11111111, 22.22222222, 33.33333333,
? ? ? ? 44.44444444,? 55.55555556,? 66.66666667,? 77.77777778,
? ? ? ? 88.88888889, 100.? ? ? ? ])
numpy.arange(start,stop, step, dtype)
np.arange(0,100,10)
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
4 生成隨機(jī)數(shù)組?
均勻分布:
np.random.rand(d0,?d1,?...,?dn)
返回[0.0路翻,1.0)內(nèi)的一組均勻分布的數(shù)。
np.random.uniform(ow=0.0,?high=1.0,?size=None)
從一個(gè)均勻分布[low,high)中隨機(jī)采樣茄靠,注意定義域是左閉右開茂契,即包含low,不包含high.
size: 輸出樣本數(shù)目嘹黔,為int或元組(tuple)類型
返回值:ndarray類型账嚎,其形狀和參數(shù)size中描述一致。
np.random.randint(low,?high=None,?size=None,?dtype='l')
從一個(gè)均勻分布中隨機(jī)采樣儡蔓,生成一個(gè)整數(shù)或N維整數(shù)數(shù)組郭蕉,取數(shù)范圍:若high不為None時(shí),取[low,high)之間隨機(jī)整數(shù)喂江,否則取值[0,low)之間隨機(jī)整數(shù)召锈。
正態(tài)分布:
np.ramdom.randn(d0, d1, …, dn)
功能:從標(biāo)準(zhǔn)正態(tài)分布中返回一個(gè)或多個(gè)樣本值
np.random.normal(loc=0.0,?scale=1.0,?size=None)
loc:float? ?此概率分布的均值(對應(yīng)著整個(gè)分布的中心centre)
scale:float 此概率分布的標(biāo)準(zhǔn)差(對應(yīng)于分布的寬度,scale越大越矮胖获询,scale越小涨岁,越瘦高)
size:int or tuple of ints? 輸出的shape拐袜,默認(rèn)為None,只輸出一個(gè)值
np.random.standard_normal(size=None)
返回指定形狀的標(biāo)準(zhǔn)正態(tài)分布的數(shù)組梢薪。
5.數(shù)組的索引蹬铺、切片 先行后列
a[0][1] 或a[0,1]? 單個(gè)
2?
a[0][1:3] 或a[0,1:3] 多個(gè)
array([2, 3], dtype=int64)
6.形狀修改
ndarray.reshape(shape[, order])
a.reshape([3,2])??只是將形狀進(jìn)行了修改,但并沒有將行列進(jìn)行轉(zhuǎn)換
array([[ 1, 2],
? ? ? [? 3,? 4],
? ? ? [100,? 6]], dtype=int64)
a.reshape([-1,3])??數(shù)組的形狀被修改為:[-1,3], -1: 表示通過待計(jì)算,確定好了列秉撇,行通過計(jì)算甜攀,自動(dòng)得到,但列也必須能和行相乘琐馆,等于元素總數(shù)规阀。
array([[ 1, 2, 3],
? ? ? [? 4, 100,? 6]], dtype=int64)
ndarray.T 數(shù)組的轉(zhuǎn)置
將數(shù)組的行、列進(jìn)行互換
a.T
array([[ 1, 4],
? ? ? [? 2, 100],
? ? ? [? 3,? 6]], dtype=int64)
ndarray.resize(new_shape[, refcheck]) ,直接修改原數(shù)組形狀
a.resize([3,2])
a
array([[ 1, 2],
? ? ? [? 3,? 4],
? ? ? [100,? 6]], dtype=int64)
7.ndarray對象轉(zhuǎn)化為其它類型對象
a.tostring(),a.tobytes(),a.tolist()
type(a)
numpy.ndarray
b = a.tobytes()
type(b)
bytes
8.數(shù)組的去重,變?yōu)橐痪S的ndarray對象
ndarray.unique()
a[0,0] = 2
a
np.unique(a)
array([ 2, 3, 4, 6, 100], dtype=int64)
9.ndarray運(yùn)算
邏輯運(yùn)算
a > 3
array([[False, False],
? ? ? [False,? True],
? ? ? [ True,? True]])
通用判斷函數(shù)
np.all(),只要有一個(gè)false瘦麸,返回false谁撼,全是True,就返回True
np.all(a>3)
False
np.any()只要有一個(gè)true,返回true滋饲,全是False,就返回False
np.any(a>3)
True
np.where 三元運(yùn)算符
np.where(a>3,1111111111,0)
array([[ 0, 0],
? ? ? [? ? ? ? 0, 1111111111],
? ? ? [1111111111, 1111111111]])
進(jìn)行復(fù)合邏輯厉碟,聯(lián)合np.logical_and和np.logical_or
np.where(np.logical_and(a>3,a<5),222,0),在數(shù)組a中,大于3并且小于5的換為222屠缭,其它0.
array([[ 0, 0],
? ? ? [? 0, 222],
? ? ? [? 0,? 0]])
統(tǒng)計(jì)運(yùn)算
np.min(a[, axis,? keepdims]
axis=0/1 按行墨榄,還是按列
keedims=true:保存矩陣的二維特性
a
array([[ 2, 2],
? ? ? [? 3,? 4],
? ? ? [100,? 6]], dtype=int64)
np.min(a) 數(shù)組中最小元素
2?
np.min(a,axis=0) 每列最小,跨行
array([2, 2], dtype=int64)
np.min(a,axis=1)? 每行最小勿她,跨列
array([2, 3, 6], dtype=int64)
?max? mean? median var std同理
返回最大值、最小值所在位置阵翎,若有axis逢并,則返回一維ndarray對象,記錄每行或者每列中最大值或者最小值的下標(biāo)位置郭卫。
np.argmax()??
np.argmax(a,axis=0)? 按每列砍聊,跨行
array([2, 2], dtype=int64)
np.argmax(a,axis=1) 按每行,跨列
array([0, 1, 0], dtype=int64)
np.argmin()同理
10.數(shù)組間的運(yùn)算:
數(shù)組與數(shù)的運(yùn)算
a*10
array([[ 20, 20],
? ? ? [? 30,? 40],
? ? ? [1000,? 60]], dtype=int64)
數(shù)組與數(shù)組的運(yùn)算
廣播機(jī)制:只有在以下情況贰军,兩個(gè)數(shù)組才能夠進(jìn)行數(shù)組與數(shù)組的運(yùn)算玻蝌。
????維度相等:相同維度,元素?cái)?shù)量一致词疼。
????shape(其中相對應(yīng)的一個(gè)地方為1)
a.shape
(3, 2)
b.shape
(3,1)
a+b
array([[ 3, 3],
? ? ? [? 4,? 5],
? ? ? [101,? 7]], dtype=int64)
矩陣運(yùn)算:
矩陣俯树,英文matrix,矩陣 一定是 二維數(shù)組贰盗,二維數(shù)組 不一定是 矩陣
矩陣乘法?
形狀要求
A(m, n) * B(n, l) = AB(m, l)
?運(yùn)算規(guī)則
A(2, 3) * B(3, 2) = AB(2, 2)
二維數(shù)組轉(zhuǎn)化為矩陣:
np.mat()
a_mat = np.mat(a)
b_mat= np.mat(b)
a_mat * b_mat
matrix([[ 4, 4, 4, 4],
? ? ? ? [? 7,? 7,? 7,? 7],
? ? ? ? [106, 106, 106, 106]], dtype=int64)
直接使用np.dot()/np.matmul(),進(jìn)行二維數(shù)組矩陣乘法運(yùn)算
np.dot(a,b)
array([[ 4, 4, 4, 4],
? ? ? [? 7,? 7,? 7,? 7],
? ? ? [106, 106, 106, 106]], dtype=int64)
使用@许饿,也能使二維數(shù)組進(jìn)行矩陣乘法運(yùn)算
a @ b
array([[ 4, 4, 4, 4],
? ? ? [? 7,? 7,? 7,? 7],
? ? ? [106, 106, 106, 106]], dtype=int64)
11.合并與分割
水平拼接
np.hstack()
np.hstack((a,b))? ? a,b行數(shù)相等
array([[ 2, 2, 1, 1, 1, 1],
? ? ? [? 3,? 4,? 1,? 1,? 1,? 1],
? ? ? [100,? 6,? 1,? 1,? 1,? 1]], dtype=int64)
? ?豎直拼接
?np.vstack()? ?a,b行數(shù)相等
np.concatenate((a1, a2), axis=)
?axis=1時(shí)候,按照數(shù)組的列方向拼接在一起舵盈,?a,b行數(shù)相等
axis=0時(shí)候陋率,按照數(shù)組的行方向拼接在一起?a,b列數(shù)相等
分割
np.split(ary, indices_or_sections, axis=0)?
ary:分割數(shù)組對象
indices_or_sections:整數(shù):分為幾分球化,列表:列表中的元素作為邊界
axis=0/1:按行方向還是按列方向
np.split(a,2,axis=1)
[array([[ 2],
? ? ? ? [? 3],
? ? ? ? [100]], dtype=int64), array([[2],
? ? ? ? [4],
? ? ? ? [6]], dtype=int64)]
x = np.arange(8.0)
array([0.,1.,2.,3.,4.,5.,6.,7.])
>>> np.split(x, [3,5,6,10])
[array([0.,1.,2.]), array([3.,4.]), array([5.]), array([6.,7.]), array([], dtype=float64)]