python使用numpy生成18種特殊數(shù)組
所有創(chuàng)建數(shù)組的函數(shù)中,都有一個可選參數(shù) dtype
,表示創(chuàng)建的數(shù)組的數(shù)據(jù)類型。
指定維度 | empty, eye, identity, ones, zeros, full |
模仿維度 | empty_like, ones_like, zeros_like, full_like |
特殊矩陣 | diag, diagflat, tri, tril, triu, vander |
數(shù)值范圍 | arange, linspace, logspace, geomspace |
坐標網(wǎng)格 | meshgrid, mgrid, ogrid, indices |
單值數(shù)組
empty
生成指定維度的空數(shù)組。
ones
, zeros
和 full
生成所有元素都相同的數(shù)組,顧名思義ones和zeros分別是全1和全0的數(shù)組,而full則可以指定其 fill_value
,例如
np.ones(3) #生成全1的3x1數(shù)組 np.zeros([2,3]) #生成全0的2x3數(shù)組 x = np.full([2,4], 5) #生成元素均為5的2x4數(shù)組
以 _like
為后綴的函數(shù),表示生成一個和輸入數(shù)組維度相同的數(shù)組,以ones為例, np.ones_like(x)
等價于 np.ones(x.shape)
。
>>> y = np.full_like(x, 6)
特殊矩陣
eye
和 identity
都是生成對角為1,其他元素為0的矩陣,區(qū)別在于, identity
只能生成單位矩陣,即方陣;而 eye
則可以生成行列數(shù)不同的矩陣。 diagflat
和 diag
用于生成對角矩陣,下面用圖像的方式,來表現(xiàn)這四種對角矩陣
這些矩陣的生成方式就是每個子圖標題中所顯示的,完整的繪圖代碼附在文末。
diag
在 diagflat
基礎(chǔ)上,還可以提取對角元素,例如
>>> np.diag(np.ones([3,3])) #提取對角元素 array([1., 1., 1.])
tri(M,N,k)用于生成M行N列的三角陣,其元素為0或者1,k用于調(diào)節(jié)0和1的分界線相對于對角線的位置,下圖中,紅色表示1,藍色表示0.
tril, triu
可用于提取出矩陣的左下和右上的三角陣,其輸入?yún)?shù)除了待提取矩陣之外,另一個參數(shù)與 tri
中的 k
相同,把x設(shè)為
x = np.arange(20).reshape(4, 5)
則 tril
和 triu
作用在x上的效果分別如下,二者分別把右上角和左下角的值變成了0。
范德蒙德矩陣
范德蒙德矩陣可表示為
np.vander
可通過給定的 α i \alpha_i αi?生成范德蒙德矩陣,例如
x = np.array([1, 2, 3, 5])y = np.vander(x, increasing=True)
其結(jié)果為
數(shù)值范圍
arange是Numpy中使用頻率超高的一個數(shù)組生成器,其輸入?yún)?shù)可以為一個、兩個或者三個,調(diào)用結(jié)果如下圖,其中橫軸表示生成數(shù)組的下標,縱軸表示值
與 arange
作用相似的函數(shù)是 linspace
,其輸入?yún)?shù)為 np.linspace(a,b,num)
,表示在 a , b a,b a,b之間等間隔生成 num
個數(shù);如果指明 endpoint=False
,則不包含 b b b點。
np.linspace(1,2,5) # [1. , 1.25, 1.5 , 1.75, 2. ]
linspace
和 arange
非常相似,區(qū)別如下
linspace(a,b,N)
在 [ a , b ] [a,b] [a,b]中間生成N個值arange(a,b,delta)
在 [ a , b ) [a,b) [a,b)之間,以 d e l t a delta delta為間隔生成值
logspace
與 linspace
邏輯相似,都是在某個區(qū)間內(nèi)等間隔生成數(shù)組,但 logspace
是對數(shù)意義上的等間隔,其等價于 10**np.linspace
。
print(np.logspace(1,2,5)) # [ 10. 17.7827941 31.6227766 56.23413252 100. ] print(10**np.linspace(1,2,5)) # [ 10. 17.7827941 31.6227766 56.23413252 100. ]
logspace
中的 base
參數(shù),可以指定對數(shù)的底,例如
>>> print(np.logspace(1,2,5,base=2)) [2. 2.37841423 2.82842712 3.36358566 4.]
geomspace
同樣是等間隔生成對數(shù),但和 logspace
的區(qū)別是,同樣在 a,b
區(qū)間內(nèi)生成對數(shù), logspace
生成范圍是 [ 1 0 a , 1 0 b ] [10^a,10^b] [10a,10b], geomspace
的范圍則是 [ a , b ] [a,b] [a,b]。
>>> print(np.geomspace(1,2,5)) [1. 1.18920712 1.41421356 1.68179283 2.]
這種區(qū)別可能過于微妙,畫個圖可能理解起來更加容易
對比如下
最后,總結(jié)一下這三個 space
函數(shù)的區(qū)別
linspace(a,b,N)
在 [ a , b ] [a,b] [a,b]中間生成N個值logspace(a,b,N,base=c)
在 [ c a , c b ] [c^a, c^b] [ca,cb]之間等指數(shù)間隔生成N個值geomspace(a,b,N,base=c)
在 [ a , b ] [a,b] [a,b]之間,等指數(shù)間隔生成N個值
坐標網(wǎng)格
在三維圖的繪制過程中,一般需要 x , y , z x,y,z x,y,z之間的對應(yīng)關(guān)系,但對于圖像而言,其 x , y x,y x,y軸坐標是體現(xiàn)在像素柵格中的,從而圖像矩陣中的像素強度,其實表示的是 z z z軸的坐標,這種情況下如果想繪制三維散點圖,就需要生成圖像像素對應(yīng)的坐標網(wǎng)格。
在 Numpy
中,最常用的坐標網(wǎng)格生成函數(shù),就是 meshgrid
,其用法可參考下面的示例
x = [0,1,2,3,4] y = [0,1,2,3] xv, yv = np.meshgrid(x, y)
其中
直觀地說,就是對輸入的 x , y x,y x,y變量,分別向 y y y軸和 x x x軸方向進行了擴張。
mgrid
比 meshgrid
更加簡單,可以直接通過魔法函數(shù)生成坐標網(wǎng)格。
>>> xv, yv = np.mgrid[0:2, 2:5] >>> print(xv) [[0 0 0] [1 1 1]] >>> print(yv) [[2 3 4] [2 3 4]]
當然,這個維度和步長可以任意選擇,
>>> np.mgrid[1:5] array([1, 2, 3, 4]) >>> np.mgrid[1:10:5] array([1, 6]) >>> np.mgrid[1.1:10] array([1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1])
如果翻閱源碼,會發(fā)現(xiàn) mgrid
是 MGridClass()
的一個實例, MGridClass
則是 nd_grid
的一個子類,在 nd_grid
中,實現(xiàn)了 __getitem__
這個魔法函數(shù),從而達成了 []
的索引方法。
ogrid
的用法與 mgrid
相同,二者都是 nd_grid
的子類,但生成的數(shù)組不同,直接看案例
>>>x,y = ogrid[0:5,0:5]
其中, x = [ 0 , 1 , 2 , 3 , 4 ] T ,y=[0,1,2,3,4]。
如果想干脆一點,只是生成從0開始的等間隔的坐標網(wǎng)格,那么這里最推薦的是 indices
,這個函數(shù)只需輸入維度,就可以完成網(wǎng)格的創(chuàng)建。
接下來打開一張圖片演示一下
import numpy as np import matplotlib.pyplot as plt img = plt.imread('test.jpg') ax = plt.subplot(projection='3d') gray = img[:,:,1] yMat, xMat = np.indices(gray.shape) ax.plot_surface(xMat, yMat, gray) ax.axis('off') plt.show()
效果為
繪圖代碼
對角矩陣
import matplotlib.pyplot as plt import numpy as np fig = plt.figure(figsize=(9,3)) ax = fig.add_subplot(141) ax.imshow(np.identity(5)) plt.title("np.identity(5)") ax = fig.add_subplot(142) ax.imshow(np.eye(5,3)) plt.title("np.eye(5,3)") ax = fig.add_subplot(143) ax.imshow(np.diagflat([1,2,3])) plt.title("np.diagflat([1,2,3])") ax = fig.add_subplot(144) ax.imshow(np.diag([1,2,3])) plt.title("np.diag([1,2,3])") plt.colorbar() plt.tight_layout() plt.show()
tri矩陣
fig = plt.figure(figsize=(9,4)) cmap = plt.get_cmap('jet') for i in range(6): ax = fig.add_subplot(2,3,i+1) ax.invert_yaxis() ax.pcolor(np.tri(4,6,i), edgecolors='k', cmap=cmap) plt.title(f"np.tri(4,6,{i})") plt.tight_layout() plt.show()
tril和triu
x = np.arange(20).reshape(4, 5) fig = plt.figure(figsize=(9,4)) ax = fig.add_subplot(121) ax.invert_yaxis() ax.imshow(np.tril(x,-1), cmap=cmap) plt.title(f"np.tril(x,-1)") ax = fig.add_subplot(122) ax.invert_yaxis() ax.imshow(np.triu(x,-1), cmap=cmap) plt.title(f"np.triu(x,-1)") plt.tight_layout() plt.show()
arange
fig = plt.figure(figsize=(9,3)) ax = fig.add_subplot(131) plt.stem(np.arange(10)) plt.title("np.arange(10)") ax = fig.add_subplot(132) plt.stem(np.arange(3,10)) plt.title("np.arange(3,10)") ax = fig.add_subplot(133) plt.stem(np.arange(3,10,2)) plt.title("np.arange(3,10,2)") plt.tight_layout() plt.show()
logspace和geospace
import numpy as np import matplotlib.pyplot as plt x = np.linspace(1,2,20) y = {"logspace" : np.logspace(1,2,20), "geomspace" : np.geomspace(1,2,20)} fig = plt.figure() for i,key in zip([1,2],y.keys()): ax = fig.add_subplot(1,2,i) ax.plot(x,y[key],marker="*") ax.set_title(key) plt.show()
以上就是python使用numpy生成18種特殊數(shù)組的詳細內(nèi)容,更多關(guān)于python numpy生成數(shù)組的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pycharm最新免費激活碼至2099年(21.3.18親測可用)
這篇文章主要介紹了pycharm最新的激活碼及激活碼的使用方法,幫助大家更好的利用pycharm學習python,感興趣的朋友可以了解下。2021-03-03使用python和Django完成博客數(shù)據(jù)庫的遷移方法
下面小編就為大家分享一篇使用python和Django完成博客數(shù)據(jù)庫的遷移方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Python seek()和tell()函數(shù)的具體使用
本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02在python中創(chuàng)建指定大小的多維數(shù)組方式
今天小編就為大家分享一篇在python中創(chuàng)建指定大小的多維數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11