php科学计算库,科学计算库numpy基础
numpy
numpy的核心數(shù)據(jù)結(jié)構(gòu)是ndarray,可以創(chuàng)建N維數(shù)組
ndarray的特點(diǎn)
ndarray(N-dimensional array):N維數(shù)組
一種由相同類型的元素組成的多維數(shù)組,元素?cái)?shù)量是事先給定好的
元素的數(shù)據(jù)類型由dtype(data-type)對(duì)象來指定,每個(gè)ndarray只有一種dtype類型
ndarray的大小固定,創(chuàng)建好數(shù)組后數(shù)組大小是不會(huì)再發(fā)生改變
ndarray的創(chuàng)建
array函數(shù):接收一個(gè)普通的python序列,并將其轉(zhuǎn)換為ndarray
b = np.array([
[1,2,3],
[4,5,6]
])
print(b)
out:
[[1 2 3]
[4 5 6]]
zeros函數(shù):創(chuàng)建指定長度或者形狀的全零數(shù)組。
d = np.zeros((2,3))
print(d)
out:
[[ 0. 0. 0.]
[ 0. 0. 0.]]
ones函數(shù):創(chuàng)建指定長度或者形狀的全1數(shù)組。
e = np.ones((3,4))
print(e)
out:
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
empty函數(shù):創(chuàng)建一個(gè)沒有任何具體值的數(shù)組(準(zhǔn)確地說是創(chuàng)建一些未初始化的ndarray多維數(shù)組)
f = np.empty((6,6))
print(f)
ndarray創(chuàng)建基本數(shù)據(jù)類型
image.png
image.png
code:
import numpy as np
a1 = np.array(["Python","Java","C++","PHP"])
print(a1)
a1
out:
['Python' 'Java' 'C++' 'PHP']
Out[81]:
array(['Python', 'Java', 'C++', 'PHP'], dtype='
code:
a2 = np.array(["哈哈","嘿嘿","呼呼","嘎嘎"])
print(a2)
a2
out:
[b'Python' b'Java' b'C++' b'PHP']
Out[83]:
array([b'Python', b'Java', b'C++', b'PHP'], dtype='|S8')
ndarray其它創(chuàng)建方式
arange函數(shù): 類似python的range函數(shù),通過指定開始值、終值和步長來創(chuàng)建一個(gè)一維數(shù)組(不包含終值)。
g = np.arange(10,50,5)
print(g) #輸出:[10 15 20 25 30 35 40 45]
h = np.arange(30,20,-2)
print(h) #輸出: [30 28 26 24 22]
linspace函數(shù):通過指定開始值、終值和元素個(gè)數(shù)來創(chuàng)建一個(gè)一維數(shù)組,數(shù)組的數(shù)據(jù)元素符合等差數(shù)列,可以通過endpoint關(guān)鍵字指定是否包含終值,默認(rèn)包含終值
i = np.linspace(0,25,6,endpoint = True)
print(i) #輸出:[ 0. 5. 10. 15. 20. 25.]
logspace函數(shù):和linspace函數(shù)類似,不過創(chuàng)建的是等比數(shù)列數(shù)組
j = np.logspace(2,3,5)
print(j) # 輸出:[ 100. 177.827941 316.22776602 562.34132519 1000. ]
random函數(shù):使用隨機(jī)數(shù)填充數(shù)組,使用numpy.random中的random()函數(shù)來創(chuàng)建隨機(jī)元素,數(shù)組包含的元素?cái)?shù)量由參數(shù)決定
j = np.random.random((2,3,4))
print(j)
輸出:
[[[ 0.67133713 0.80188756 0.06388015 0.81575917]
[ 0.21830916 0.90382401 0.0095 0.95252789]
[ 0.54048634 0.07984948 0.9527077 0.85444074]]
[[ 0.15047247 0.14771948 0.425606 0.02572186]
[ 0.71512809 0.81017573 0.80882504 0.87543752]
[ 0.75518265 0.73766281 0.93846421 0.31309056]]]
ndarray的屬性
dtype:用于說明數(shù)組元素?cái)?shù)據(jù)類型的對(duì)象
astype:用于對(duì)數(shù)組元素?cái)?shù)據(jù)類型進(jìn)行轉(zhuǎn)換
shape:數(shù)組的各個(gè)維度大小的元組,即數(shù)組的形狀
size:元素總個(gè)數(shù),即shape中各個(gè)數(shù)的相乘
ndim:數(shù)組的維度數(shù)量
c= np.array([
[
[1,4,7],[2,5,8]
],
[
[3,6,9],[6,6,6]
]
])
print(c)
輸出:
[[[1 4 7]
[2 5 8]]
[[3 6 9]
[6 6 6]]]
print(c.ndim) # 數(shù)組的緯度數(shù)為:3
print(c.dtype) # 數(shù)組元素?cái)?shù)據(jù)類型為:int32
print(c.shape) # 數(shù)組各個(gè)緯度的大小:(2, 2, 3)
print(c.size) # 數(shù)組的元素個(gè)數(shù):12
d= c.astype(float)
print(d.dtype) #數(shù)組元素?cái)?shù)據(jù)類型為:float64
ndarray修改數(shù)組結(jié)構(gòu)
對(duì)于一個(gè)已經(jīng)存在的ndarray數(shù)組對(duì)象而言,可以通過調(diào)用修改形狀的方法從而改變數(shù)組的結(jié)構(gòu)形狀。
直接修改數(shù)組ndarray的shape值, 要求修改后乘積不變。
直接使用reshape函數(shù)創(chuàng)建一個(gè)改變尺寸的新數(shù)組,原數(shù)組的shape保持不變,但是新數(shù)組和原數(shù)組共享一個(gè)內(nèi)存空間,修改數(shù)組中的值都會(huì)對(duì)另外一個(gè)產(chǎn)生影響,另外要求新數(shù)組的元素個(gè)數(shù)和原數(shù)組一致。
當(dāng)指定某一個(gè)軸為-1的時(shí)候,表示將根據(jù)數(shù)組元素的數(shù)量自動(dòng)計(jì)算該軸的長度值。
code:
b1 = np.arange(0,20,2)
print(b1)
print(b1.size)
print(b1.shape)
out:
[ 0 2 4 6 8 10 12 14 16 18]
10
(10,)
code:
b2 = b1.reshape(2,5)
print(b2)
out:
[[ 0 2 4 6 8]
[10 12 14 16 18]]
code
b3 = b1.reshape(-1,2)
print(b3)
b3[2][1] = 100
print(b1)
out:
[[ 0 2]
[ 4 6]
[ 8 10]
[12 14]
[16 18]]
[ 0 2 4 6 8 100 12 14 16 18]
code:
b2.shape = (1,10)
print(b2)
out:
[[ 0 2 4 6 8 100 12 14 16 18]]
NumPy的基本操作
ndarray多維數(shù)組的索引
c1 = np.array([
[
[5,2,4],
[3,8,2],
],
[
[6,0,4],
[0,1,6]
]
])
print(c1[1,0,2]) # out:4
print(c1[0][1][2]) # out:2
ndarray花式索引
code:
f1=np.arange(32).reshape((8,4))
print(f1)
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]
[24 25 26 27]
[28 29 30 31]]
code:
f2 = f1[[2,3,5]] #取第2,3,5行
print(f2)
out:
[[ 8 9 10 11]
[12 13 14 15]
[20 21 22 23]]
code:
f3 = f1[[2,3,5],[1,2,3]] #分別取第2,3,5行中第1,2,3個(gè)元素
print(f3) #out: [ 9 14 23]
code:
f4 = f1[np.ix_([2,3,5],[1,2,3])] #分別取第2,3,5行中第1,2,3列
#f4 = f1[[2,3,5]][:,[1,2,3]]
print(f4)
out:
[[ 9 10 11]
[13 14 15]
[21 22 23]]
ndarray數(shù)組的切片
通過切片獲得的新數(shù)組只是原數(shù)組的一個(gè)視圖,當(dāng)改變新數(shù)組中的數(shù)值時(shí),原數(shù)組跟著改變。
ndarray數(shù)組的切片同Python中的切片
ndarray布爾類型索引
code:
d1 = np.arange(0,12,1)
d1.shape = (3,4)
print(d1)
out:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
code:
d2 = d1 < 6
print(d2)
out:
[[ True True True True]
[ True True False False]
[False False False False]]
code:
print(d1[d2])
# print(d1[d1<6])
out:
[0 1 2 3 4 5]
code:
names=np.array(['Gerry','Tom','John'])
scores=np.array([
[98,87,76,65],
[45,45,66,90],
[87,76,67,91]
])
classs=np.array([u'語文',u'數(shù)學(xué)',u'英語',u'體育'])
e1 = names=='Gerry'
print(e1) # [ True False False]
print(scores[e1].reshape((-1))) #[98 87 76 65]
print(scores[(names=='Gerry')|(names=='Tom')])
out:
[[98 87 76 65]
[45 45 66 90]]
ndarray數(shù)組與標(biāo)量、數(shù)組之間的運(yùn)算
數(shù)組不用循環(huán)即可對(duì)每個(gè)元素執(zhí)行批量的算術(shù)運(yùn)算操作,這個(gè)過程叫做矢量化,即用數(shù)組表達(dá)式代替循環(huán)的做法。矢量化數(shù)組運(yùn)算性能比純Python方式快上一兩個(gè)數(shù)據(jù)級(jí)。
大小相等的兩個(gè)數(shù)組之間的任何算術(shù)運(yùn)算都會(huì)將其運(yùn)算應(yīng)用到元素級(jí)上的操作,在NumPy中,大小相等的數(shù)組之間的運(yùn)算,為元素級(jí)運(yùn)算,即只用于位置相同的元素之間,所得的運(yùn)算結(jié)果組成一個(gè)新的數(shù)組,運(yùn)算結(jié)果的位置跟操作數(shù)位置相同。
ndarray數(shù)組的矩陣積
image.png
矩陣:多維數(shù)組即矩陣
矩陣C = 矩陣A*矩陣B(矩陣A的列數(shù)必須等于矩陣B的行數(shù)時(shí),A和B才可以相乘)
code
arr1 = np.array([
[5,2,4],
[3,8,2],
[6,0,4],
[0,1,6]
])
arr2 = np.array([
[2,4],
[1,3],
[3,2]
])
arr = arr1.dot(arr2)
print(arr)
out
[[24 34]
[20 40]
[24 32]
[19 15]]
ndarray數(shù)組轉(zhuǎn)置與軸對(duì)換
數(shù)組轉(zhuǎn)置是指將shape進(jìn)行重置操作,并將其值重置為原始shape元組的倒置,比如原始的shape值為:(2,3,4),那么轉(zhuǎn)置后的新元組的shape的值為: (4,3,2)
對(duì)于二維數(shù)組而言(矩陣)數(shù)組的轉(zhuǎn)置其實(shí)就是矩陣的轉(zhuǎn)置可以通過調(diào)用數(shù)組的transpose函數(shù)或者T屬性進(jìn)行數(shù)組轉(zhuǎn)置操作
code:
g1 = np.arange(12).reshape((3,4))
print(g1)
print(g1.shape)
out:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
(3, 4)
code:
g2 = g1.transpose()
print(g2)
print(g2.shape)
out:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
(4, 3)
code:
g3 = g1.T
print(g3)
print(g3.shape)
out:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
(4, 3)
常用函數(shù)
常用一元函數(shù)
image.png
image.png
常用二元函數(shù)
image.png
聚合函數(shù)
聚合函數(shù)是對(duì)一組值(eg.一個(gè)數(shù)組)進(jìn)行操作,返回一個(gè)單一值作為結(jié)果的函數(shù)。當(dāng)然聚合函數(shù)也可以指定對(duì)某個(gè)具體的軸進(jìn)行數(shù)據(jù)聚合操作;常將的聚合操作有:平均值、最大值、最小值、方差等等
i1 = np.array([
[1,2,3,4],
[5,6,7,8],
[9,0,-2,-4]
])
print(i1)
print(np.max(i1)) #最大值
print(np.min(i1)) #最小值
print(np.mean(i1)) #平均值
print(np.std(i1)) # 標(biāo)準(zhǔn)差
print(np.var(i1)) #方差
print(np.max(i1,axis=1)) #axis = 1表示對(duì)行數(shù)據(jù)進(jìn)行操作
print(np.mean(i1,axis=0)) #axis = 0表示對(duì)列數(shù)據(jù)進(jìn)行操作
print(np.sum(i1,axis=1)) # 對(duì)行求和
where函數(shù)
where函數(shù)是三元表達(dá)式x if condition else y的矢量化版本
j1 = np.array([1.1,1.2,1.3,1.4])
j2 = np.array([2.1,2.2,0.3,2.4])
condition = j1
result1 = [x if c else y for(x,y,c) in zip(j1,j2,condition)]
print(result1) # [1.1000000000000001, 1.2, 1.3, 1.3999999999999999]
print(condition)
#condition為True獲取j1中的內(nèi)容,為false獲取j2中的內(nèi)容
result2 = np.where(condition,j1,j2)
print(result2)
out:
[ 1.1 1.2 0.3 1.4]
[ True True False True]
[ 1.1 1.2 0.3 1.4]
unique函數(shù)
將數(shù)組中的元素進(jìn)行去重操作
k1 = np.array(["a","b","c","e","b","c"])
k2 = np.unique(k1)
print(k2) #['a' 'b' 'c' 'e']
random、randn、rand的區(qū)別
numpy.random.random(name,A)
這個(gè)可以改你要的隨機(jī)數(shù)是什么分布,可以調(diào)整隨機(jī)數(shù)的參數(shù),例如正態(tài)分布可以改兩個(gè)參數(shù)
numpy.random.randn(d0, d1, …, dn)
從標(biāo)準(zhǔn)正態(tài)分布中返回一個(gè)或多個(gè)樣本值。
numpy.random.rand(d0, d1, …, dn)
均勻分布隨機(jī)樣本位于[0, 1)中。
總結(jié)
以上是生活随笔為你收集整理的php科学计算库,科学计算库numpy基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 50x30的显示器是多少寸的
- 下一篇: wordpress留言板comments