Numpy中創(chuàng)建數(shù)組的9種方式小結
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ù)組,在不設置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ù)組
調用zeros/ones方法會創(chuàng)建一個全為‘0’/‘1’值的數(shù)組,通常在數(shù)組元素位置,大小一致的情況下來生成臨時數(shù)組。‘0’/‘1’充當占位符。
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是基于一個范圍來構造數(shù)組,參數(shù)num是開始值和結束值之間需要創(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è)務需求,因此需要創(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方法也是產生隨機數(shù)組的一種方式,并且它能產生符合正態(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之間產生隨機數(shù),并從中取5個數(shù)值來構建數(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)建的結果就是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)

到此這篇關于Numpy中創(chuàng)建數(shù)組的9種方式小結的文章就介紹到這了,更多相關Numpy 創(chuàng)建數(shù)組內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python英文文章詞頻統(tǒng)計(14份劍橋真題詞頻統(tǒng)計)
這篇文章主要介紹了Python英文文章詞頻統(tǒng)計(14份劍橋真題詞頻統(tǒng)計),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
python MySQLdb Windows下安裝教程及問題解決方法
這篇文章主要介紹了python MySQLdb Windows下安裝教程及問題解決方法,本文講解了安裝數(shù)據(jù)庫mysql、安裝MySQLdb等步驟,需要的朋友可以參考下2015-05-05
python 如何把classification_report輸出到csv文件
這篇文章主要介紹了python 把classification_report輸出到csv文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
python標準算法實現(xiàn)數(shù)組全排列的方法
這篇文章主要介紹了python標準算法實現(xiàn)數(shù)組全排列的方法,實例分析了全排列的原理與Python實現(xiàn)技巧,需要的朋友可以參考下2015-03-03

