php科学计算库,科学计算库numpy基础
numpy
numpy的核心數據結構是ndarray,可以創建N維數組
ndarray的特點
ndarray(N-dimensional array):N維數組
一種由相同類型的元素組成的多維數組,元素數量是事先給定好的
元素的數據類型由dtype(data-type)對象來指定,每個ndarray只有一種dtype類型
ndarray的大小固定,創建好數組后數組大小是不會再發生改變
ndarray的創建
array函數:接收一個普通的python序列,并將其轉換為ndarray
b = np.array([
[1,2,3],
[4,5,6]
])
print(b)
out:
[[1 2 3]
[4 5 6]]
zeros函數:創建指定長度或者形狀的全零數組。
d = np.zeros((2,3))
print(d)
out:
[[ 0. 0. 0.]
[ 0. 0. 0.]]
ones函數:創建指定長度或者形狀的全1數組。
e = np.ones((3,4))
print(e)
out:
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
empty函數:創建一個沒有任何具體值的數組(準確地說是創建一些未初始化的ndarray多維數組)
f = np.empty((6,6))
print(f)
ndarray創建基本數據類型
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其它創建方式
arange函數: 類似python的range函數,通過指定開始值、終值和步長來創建一個一維數組(不包含終值)。
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函數:通過指定開始值、終值和元素個數來創建一個一維數組,數組的數據元素符合等差數列,可以通過endpoint關鍵字指定是否包含終值,默認包含終值
i = np.linspace(0,25,6,endpoint = True)
print(i) #輸出:[ 0. 5. 10. 15. 20. 25.]
logspace函數:和linspace函數類似,不過創建的是等比數列數組
j = np.logspace(2,3,5)
print(j) # 輸出:[ 100. 177.827941 316.22776602 562.34132519 1000. ]
random函數:使用隨機數填充數組,使用numpy.random中的random()函數來創建隨機元素,數組包含的元素數量由參數決定
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:用于說明數組元素數據類型的對象
astype:用于對數組元素數據類型進行轉換
shape:數組的各個維度大小的元組,即數組的形狀
size:元素總個數,即shape中各個數的相乘
ndim:數組的維度數量
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) # 數組的緯度數為:3
print(c.dtype) # 數組元素數據類型為:int32
print(c.shape) # 數組各個緯度的大小:(2, 2, 3)
print(c.size) # 數組的元素個數:12
d= c.astype(float)
print(d.dtype) #數組元素數據類型為:float64
ndarray修改數組結構
對于一個已經存在的ndarray數組對象而言,可以通過調用修改形狀的方法從而改變數組的結構形狀。
直接修改數組ndarray的shape值, 要求修改后乘積不變。
直接使用reshape函數創建一個改變尺寸的新數組,原數組的shape保持不變,但是新數組和原數組共享一個內存空間,修改數組中的值都會對另外一個產生影響,另外要求新數組的元素個數和原數組一致。
當指定某一個軸為-1的時候,表示將根據數組元素的數量自動計算該軸的長度值。
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多維數組的索引
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個元素
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數組的切片
通過切片獲得的新數組只是原數組的一個視圖,當改變新數組中的數值時,原數組跟著改變。
ndarray數組的切片同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'數學',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數組與標量、數組之間的運算
數組不用循環即可對每個元素執行批量的算術運算操作,這個過程叫做矢量化,即用數組表達式代替循環的做法。矢量化數組運算性能比純Python方式快上一兩個數據級。
大小相等的兩個數組之間的任何算術運算都會將其運算應用到元素級上的操作,在NumPy中,大小相等的數組之間的運算,為元素級運算,即只用于位置相同的元素之間,所得的運算結果組成一個新的數組,運算結果的位置跟操作數位置相同。
ndarray數組的矩陣積
image.png
矩陣:多維數組即矩陣
矩陣C = 矩陣A*矩陣B(矩陣A的列數必須等于矩陣B的行數時,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數組轉置與軸對換
數組轉置是指將shape進行重置操作,并將其值重置為原始shape元組的倒置,比如原始的shape值為:(2,3,4),那么轉置后的新元組的shape的值為: (4,3,2)
對于二維數組而言(矩陣)數組的轉置其實就是矩陣的轉置可以通過調用數組的transpose函數或者T屬性進行數組轉置操作
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)
常用函數
常用一元函數
image.png
image.png
常用二元函數
image.png
聚合函數
聚合函數是對一組值(eg.一個數組)進行操作,返回一個單一值作為結果的函數。當然聚合函數也可以指定對某個具體的軸進行數據聚合操作;常將的聚合操作有:平均值、最大值、最小值、方差等等
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)) # 標準差
print(np.var(i1)) #方差
print(np.max(i1,axis=1)) #axis = 1表示對行數據進行操作
print(np.mean(i1,axis=0)) #axis = 0表示對列數據進行操作
print(np.sum(i1,axis=1)) # 對行求和
where函數
where函數是三元表達式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中的內容,為false獲取j2中的內容
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函數
將數組中的元素進行去重操作
k1 = np.array(["a","b","c","e","b","c"])
k2 = np.unique(k1)
print(k2) #['a' 'b' 'c' 'e']
random、randn、rand的區別
numpy.random.random(name,A)
這個可以改你要的隨機數是什么分布,可以調整隨機數的參數,例如正態分布可以改兩個參數
numpy.random.randn(d0, d1, …, dn)
從標準正態分布中返回一個或多個樣本值。
numpy.random.rand(d0, d1, …, dn)
均勻分布隨機樣本位于[0, 1)中。
總結
以上是生活随笔為你收集整理的php科学计算库,科学计算库numpy基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 50x30的显示器是多少寸的
- 下一篇: 动态规划算法php,php算法学习之动态