新聞中心
本篇內(nèi)容主要講解“Numpy創(chuàng)建數(shù)組的方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Numpy創(chuàng)建數(shù)組的方法是什么”吧!
成都創(chuàng)新互聯(lián)公司專注于邵原企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。邵原網(wǎng)站建設(shè)公司,為邵原等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
一、標(biāo)準(zhǔn)數(shù)組的創(chuàng)建
1.1 numpy.empty 創(chuàng)建空數(shù)組
用來創(chuàng)建一個指定形狀(shape)、數(shù)據(jù)類型(dtype)且未初始化的數(shù)組;由于未初始化,所以數(shù)組中的數(shù)據(jù)是隨機的;
numpy.empty(shape, dtype = float, order = 'C')
參數(shù) | 描述 |
---|---|
shape | 數(shù)組形狀 |
dtype | 數(shù)據(jù)類型,可選 |
order | 有"C"和"F"兩個選項,分別代表,行優(yōu)先和列優(yōu)先,在計算機內(nèi)存中的存儲元素的順序。 一般情況下不必關(guān)注 |
舉例:
a = np.empty((4,3),dtype=int) print(a) # 每次輸出都不同,因為沒有初始化 # [[-958363344 464 -958381568] # [ 464 -958387104 464] # [-958380912 464 -958380224] # [ 464 -958380224 464]]
1.2 numpy.zeros 創(chuàng)建0數(shù)組
用來創(chuàng)建一個指定形狀(shape)的數(shù)組,并全部初始化為0 舉例:
a = np.ones((4,3)) print(a) # [[1 1 1] # [1 1 1] # [1 1 1] # [1 1 1]]
1.3 numpy.ones 創(chuàng)建1數(shù)組
用來創(chuàng)建一個指定形狀(shape)的數(shù)組,并全部初始化為1 舉例:
a = np.zeros((4,3)) print(a) # [[0 0 0] # [0 0 0] # [0 0 0] # [0 0 0]]
二、創(chuàng)建一般數(shù)組
2.0 利用list 創(chuàng)建數(shù)組 numpy.array
格式為: numpy.array(object, dtype=None)
,其中:
參數(shù) | 描述 |
---|---|
object | 創(chuàng)建的數(shù)組的對象,可以為單個值,列表,元胞等。 |
dtype | 數(shù)據(jù)類型,可選 |
舉例:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print(array) # [0 1 2 3 4 5 6 7 8 9] print(array.dtype) # int32
2.1 利用list 創(chuàng)建數(shù)組 numpy.asarray
格式為: numpy.asarray(a, dtype = None, order = None)
,其中:
參數(shù) | 描述 |
---|---|
a | 任意形式的輸入?yún)?shù),可以是: 列表, 列表的元組, 元組, 元組的元組, 元組的列表,多維數(shù)組 |
dtype | 數(shù)據(jù)類型,可選 |
order | 可選,有"C"和"F"兩個選項,分別代表,行優(yōu)先和列優(yōu)先,在計算機內(nèi)存中的存儲元素的順序。 |
舉例:
a = [1,3,1,5,4] b = np.asarray(a) print(type(a)) #print(type(b)) #
2.2 利用可迭代對象創(chuàng)建數(shù)組 numpy.fromiter
格式為:numpy.fromiter(iterable, dtype, count=-1)
,其中:
參數(shù) | 描述 |
---|---|
iterable | 可迭代對象 |
dtype | 返回數(shù)組的數(shù)據(jù)類型 |
count | 讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù) |
舉例:
list=range(5) it=iter(list) x=np.fromiter(it, dtype=float) print(x)
2.3 利用數(shù)值范圍創(chuàng)建數(shù)組 numpy.arange
格式為:numpy.arange(start, stop, step, dtype)
,其中:
參數(shù) | 描述 |
---|---|
start | 起始值,默認(rèn)為0 |
stop | 終止值(不包含) |
step | 步長,默認(rèn)為1 |
dtype | 返回ndarray的數(shù)據(jù)類型,如果沒有提供,則會使用輸入數(shù)據(jù)的類型。 |
舉例:
a = np.arange(10) print(a) # 輸出:[0 1 2 3 4 5 6 7 8 9] b = np.arange(10,20,2) print(b) # 輸出:[10 12 14 16 18]
2.4 利用數(shù)值范圍創(chuàng)建數(shù)組 numpy.linspace
格式為:np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
,其中:
參數(shù) | 描述 |
---|---|
start | 序列的起始值 |
stop | 序列的終止值,如果endpoint為true,該值包含于數(shù)列中 |
num | 要生成的等步長的樣本數(shù)量,默認(rèn)為50 |
endpoint | 該值為 true 時,數(shù)列中包含stop值,反之不包含,默認(rèn)是True。 |
retstep | 如果為 True 時,生成的數(shù)組中會顯示間距,反之不顯示。 |
dtype | ndarray 的數(shù)據(jù)類型 |
舉例:
a = np.linspace(10,20,5,endpoint=False) # 從10開始,20結(jié)束,一共產(chǎn)生5個數(shù)字,不包含20 print(a) # 輸出:[10. 12. 14. 16. 18.] b = np.linspace(10,20,5,endpoint=True) print(b) # 輸出:[10. 12.5 15. 17.5 20. ]
三、創(chuàng)建隨機數(shù)組
3.1 創(chuàng)建整數(shù)隨機數(shù)組:np.random.randint
格式為 : np.random.randint(0, 100, (3, 4))
在使用random之前,可以通過 np.random.seed(666)
來設(shè)置隨機種子,這一點與Python一致;
舉例:
a = np.random.randint(0, 100, (3, 4)) print(a) # 輸出為: # [[92 58 18 32] # [ 4 87 81 1] # [12 11 13 68]]
3.2 創(chuàng)建浮點型隨機數(shù)組
只要在整數(shù)的基礎(chǔ)上除以整數(shù)即可,例如需要創(chuàng)建一個取值范圍在0,1之間,精度為0.01的浮點型數(shù)組,可以使用如下方法:
a = np.random.randint(0, 100, (3, 4)) b= a/100 print(b) # 輸出為: # [[0.05 0.48 0.72 0.95] # [0.68 0.78 0.22 0.98] # [0.17 0.45 0.7 0.85]]
到此,相信大家對“Numpy創(chuàng)建數(shù)組的方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享題目:Numpy創(chuàng)建數(shù)組的方法是什么
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/jjjghi.html