Numpy中創(chuàng)建數(shù)組的9種方式小結(jié)
1、使用empty方法創(chuàng)建數(shù)組
該方式可以創(chuàng)建一個空數(shù)組,dtype可以指定隨機數(shù)的類型,否則隨機采用一種類型生成隨機數(shù)。
import numpy as np dt = np.numpy([2, 2], dtype=int)

2、使用array創(chuàng)建數(shù)組
使用array方法可以基于Python列表創(chuàng)建數(shù)組,在不設(shè)置dtype的情況下,從列表中自動推斷數(shù)據(jù)類型。
import numpy as np
dt = np.array([1, 2, 3, 4, 5])
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)
dt = np.array([1, 2, 3, 4, 5], dtype='f8') # 64位浮點數(shù)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

3、使用zeros/ones創(chuàng)建數(shù)組
調(diào)用zeros/ones方法會創(chuàng)建一個全為‘0’/‘1’值的數(shù)組,通常在數(shù)組元素位置,大小一致的情況下來生成臨時數(shù)組。‘0’/‘1’充當(dāng)占位符。
import numpy as np
dt = np.zeros([3, 5], dtype=int)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)
dt = np.ones([5, 3], dtype=float)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

4、使用arange創(chuàng)建數(shù)組
使用arange方法可以基于一個數(shù)據(jù)范圍來創(chuàng)建數(shù)組。
import numpy as np
dt = np.arange(10, 30, 5)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

5、使用linspace創(chuàng)建數(shù)組
linspace是基于一個范圍來構(gòu)造數(shù)組,參數(shù)num是開始值和結(jié)束值之間需要創(chuàng)建多少個數(shù)值。retstep會改變計算的輸出,返回一個元組,而元組的兩個元素分別是需要生成的數(shù)組和數(shù)組的步差值。
import numpy as np
dt = np.linspace(20, 30, num=5)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)
dt = np.linspace(20, 30, num=5, endpoint=False)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)
dt = np.linspace(20, 30, num=5, retstep=True)
print('元組:', dt)

6、使用numpy.random.rand創(chuàng)建數(shù)組
很多情況下手動創(chuàng)建的數(shù)組往往不能滿足業(yè)務(wù)需求,因此需要創(chuàng)建隨機數(shù)組。
import numpy as np
dt = np.random.rand(10)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

7、使用numpy.random.randn創(chuàng)建數(shù)組
numpy.random.randn方法也是產(chǎn)生隨機數(shù)組的一種方式,并且它能產(chǎn)生符合正態(tài)分布的隨機數(shù)。
import numpy as np
dt = np.random.randn(3, 5)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

8、使用numpy.random.randint創(chuàng)建數(shù)組
在10和30之間產(chǎn)生隨機數(shù),并從中取5個數(shù)值來構(gòu)建數(shù)組。
import numpy as np
dt = np.random.randint(10, 30, 5)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

9、使用fromfunction創(chuàng)建數(shù)組
fromfunction方法可以通過一個函數(shù)規(guī)則來創(chuàng)建數(shù)組。該方法中shape參數(shù)制定了創(chuàng)建數(shù)組的規(guī)則,shape=(4,5),最終創(chuàng)建的結(jié)果就是4行5列的二維數(shù)組。
import numpy as np
dt = np.fromfunction(lambda i, j:i + j, (4, 5), dtype=int)
print('數(shù)組:', dt)
print('數(shù)據(jù)類型:', dt.dtype)

到此這篇關(guān)于Numpy中創(chuàng)建數(shù)組的9種方式小結(jié)的文章就介紹到這了,更多相關(guān)Numpy 創(chuàng)建數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python訓(xùn)練數(shù)據(jù)時打亂訓(xùn)練數(shù)據(jù)與標簽的兩種方法小結(jié)
今天小編就為大家分享一篇python訓(xùn)練數(shù)據(jù)時打亂訓(xùn)練數(shù)據(jù)與標簽的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python接口自動化淺析logging封裝及實戰(zhàn)操作
本篇文章主要給大家介紹將了logging常用配置放入yaml配置文件、logging日志封裝及結(jié)合登錄用例,講解日志如何在接口測試中運用的實例操作2021-08-08
詳解Pycharm出現(xiàn)out of memory的終極解決方法
這篇文章主要介紹了詳解Pycharm出現(xiàn)out of memory的終極解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

