日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么初始化數(shù)組

因為畫圖中x軸與y軸的數(shù)據(jù)通常為數(shù)組格式的數(shù)據(jù),所以先總結(jié)一下如何初始化數(shù)組:

榆社網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

(1)list得到數(shù)組

# 通過array函數(shù)傳遞list對象
L = [1, 2, 3, 4, 5, 6]
a = np.array(L)
# 若傳遞的是多層嵌套的list,將創(chuàng)建多維數(shù)組
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# 可以通過dtype參數(shù)在創(chuàng)建時指定元素類型
d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.float)
# 如果更改元素類型,可以使用astype安全的轉(zhuǎn)換
f = d.astype(np.int)

(2)使用arange

# 和python的range類似,arange同樣不包括終值;但arange可以生成浮點類型,而range只能是整數(shù)類型
# 1為開始值,10為終止值(不包括),0.5為步長
a = np.arange(1, 10, 0.5)

相關(guān)推薦:《Python基礎(chǔ)教程》

(3)使用ones、zeros、empty

# np.ones(shape, dtype),生成元素全為1(默認(rèn)浮點型)的數(shù)組
# shape可以為一個整數(shù)得到一個一維數(shù)組,也可以為(整數(shù)1,整數(shù)2)的格式得到二維數(shù)組,同理可得多維數(shù)組
a = np.ones((3, 3), dtype=np.int32)
print("a: \n", a)
   
# np.zeros(shape, dtype),生成元素全為0(默認(rèn)浮點型)的數(shù)組
# 用法與np.noes()一樣
b = np.zeros((3, 3), dtype=np.int32)
print("b: \n", b)
   
# np.empty(shape, dtype),生成元素為隨機數(shù)(默認(rèn)浮點型)的數(shù)組
# 用法與np.ones()一樣
c = np.empty((3, 4), dtype=np.int32)
print("c: \n", c)

# np.ones()、np.zeros()、np.empty()都具有如下形式復(fù)制一個結(jié)構(gòu)一樣的數(shù)組,但數(shù)據(jù)類型可選擇
np.ones_like(array, dtype=)
np.zeros_like(array, dtype=)
np.empty_like(array, dtype=)

(4)等差數(shù)列

# linspace函數(shù)通過指定起始值、終止值和元素個數(shù)來創(chuàng)建等差數(shù)組,元素之間是等步長的
# endpoint表示是否包括終止值,默認(rèn)為True
b = np.linspace(1, 10, 10,endpoint=True)

(5)等比數(shù)列

# 指定起始值、終止值、元素個數(shù)和基數(shù)來創(chuàng)建等比數(shù)列
# base表示基數(shù),下式創(chuàng)建了一個1到4之間的有10個數(shù)的等比數(shù)列
d = np.logspace(1, 2, 10, endpoint=True, base=2)
# 基數(shù)為10,下式創(chuàng)建了一個10到100之間的有10個數(shù)的等比數(shù)列
d = np.logspace(1, 2, 10, endpoint=True, base=10)

(6)隨機數(shù)

rand()

# 返回一個服從“0~1”均勻分布的隨機數(shù),該隨機數(shù)在[0, 1)內(nèi),也可以返回一個由服從“0~1”均勻分布的隨機數(shù)組成的數(shù)組。
# np.random.rand(d0, d1, …, dn)
# 返回一個隨機值,隨機值在[0, 1)內(nèi)
In[15]: np.random.rand()
Out[15]: 0.9027797355532956
# 返回一個3x3的數(shù)組,數(shù)組元素在[0, 1)內(nèi)
In[16]:np.random.rand(3,3)
Out[16]: 
array([[ 0.47507608,  0.64225621,  0.9926529 ],
    [ 0.95028412,  0.18413813,  0.91879723],
    [ 0.89995217,  0.42356103,  0.81312942]])
In[17]: np.random.rand(3,3,3)
  
# 返回一個3x3x3的數(shù)組
Out[17]: 
array([[[ 0.30295904,  0.76346848,  0.33125168],
    [ 0.77845927,  0.75020602,  0.84670385],
    [ 0.2329741 ,  0.65962263,  0.93239286]],
    [[ 0.24575304,  0.9019242 ,  0.62390674],
    [ 0.43663215,  0.93187574,  0.75302239],
    [ 0.62658734,  0.01582182,  0.66478944]],
    [[ 0.22152418,  0.51664503,  0.41196781],
    [ 0.47723318,  0.19248885,  0.29699868],
    [ 0.11664651,  0.66718804,  0.39836448]]])

randn()

# 產(chǎn)生標(biāo)準(zhǔn)正態(tài)分布隨機數(shù)或隨機數(shù)組,用法與rand(d0, d1, …, dn)方法一樣
np.random.randn(d0, d1, …, dn)

randint()

# 可以生成隨機數(shù),也可以生成多維隨機數(shù)組
# np.random.randint(low, high=None, size=None, dtype=)
# [0,4)之間的隨機數(shù)
In[7]: np.random.randint(4)
Out[7]: 1
# [0,4)之間的一維數(shù)組
In[8]: np.random.randint(4,size=4)
Out[8]: array([2, 2, 2, 0])
# [4,10)之間的一維數(shù)組
In[9]: np.random.randint(4,10,size=6)
Out[9]: array([7, 9, 7, 8, 6, 9])
# [4,10)之間的2x2數(shù)組
np.random.randint(4,10,size=(2,2),dtype='int32')
Out[10]:
array([[7, 4],[6, 9]])

uniform()

# 產(chǎn)生[low, high)之間的均勻分布隨機數(shù)或隨機數(shù)組,low默認(rèn)為0.0,high默認(rèn)為1.0
np.random.uniform(low=0.0, high=1.0, size=None)

normal()

# 產(chǎn)生均值為loc,方差為scale的服從正太分布的隨機數(shù)或隨機數(shù)組,loc默認(rèn)為0,scale默認(rèn)為1
np.random.normal(loc=0.0, scale=1.0, size=None)

文章名稱:創(chuàng)新互聯(lián)Python教程:python怎么初始化數(shù)組
轉(zhuǎn)載源于:http://www.5511xx.com/article/dhpieji.html