App下載

numpy之linspace()函數(shù)使用詳解

猿友 2021-01-04 17:58:31 瀏覽數(shù) (57440)
反饋

linspace() 函數(shù)

作為序列生成器, numpy.linspace()函數(shù)用于在線性空間中以均勻步長生成數(shù)字序列。

Numpy通常可以使用numpy.arange()生成序列,但是當我們使用浮點參數(shù)時,可能會導致精度損失,這可能會導致不可預測的輸出。為了避免由于浮點精度而造成的任何精度損失,numpy在numpy.linspace()為我們提供了一個單獨的序列生成器,如果您已經(jīng)知道所需的元素數(shù),則這是首選。 但是通常使用帶有適當參數(shù)的linspace()arange()可以得到相同的輸出,因此可以為同一任務選擇兩者。

例如,以下代碼使用numpy.linspace()在0到10之間繪制2個線性序列,以顯示該序列生成的均勻性。

import numpy as np

import matplotlib.pyplot as plt

 

y = np.zeros(5)

x1 = np.linspace(0, 10, 5)

x2 = np.linspace(0, 10, 5)

plt.plot(x1, y, 'o')

plt.plot(x2, y + 0.5, 'o')

plt.ylim([-0.5, 1])

plt.show()

輸出 :

lan

語法:

格式: array = numpy.linspace(start, end, num=num_points)將在startend之間生成一個統(tǒng)一的序列,共有num_points個元素。

  • start -> Starting point (included) of the rangestart ->范圍的起點(包括)
  • end -> Endpoint (included) of the rangeend ->范圍的端點(包括)
  • num -> Total number of points in the sequencenum >序列中的總點數(shù)

讓我們通過幾個示例來理解這一點:

import numpy as np

 

a = np.linspace(0.02, 2, 10)

 

print('Linear Sequence from 0.02 to 2:', a)

print('Length:', len(a))

輸出

Linear Sequence from 0.02 to 2: [0.02 0.24 0.46 0.68 0.9  1.12 1.34 1.56 1.78 2.  ]

Length: 10

上面的代碼段生成了0.02到2之間的均勻序列,其中包含10個元素。

endpoint 關(guān)鍵字參數(shù)

如果您不想在序列計算中包括最后一點,則可以使用另一個關(guān)鍵字參數(shù)endpoint ,可以將其設置為False 。 (默認為True )

import numpy as np

 

a = np.linspace(0.02, 2, 10, endpoint=False)

 

print('Linear Sequence from 0.02 to 2:', a)

print('Length:', len(a))

輸出

Linear Sequence from 0.02 to 2: [0.02  0.218 0.416 0.614 0.812 1.01  1.208 1.406 1.604 1.802]

Length: 10

如您所見,最后一點(2)沒有包含在序列中,因此步長也不同,這將產(chǎn)生一個完全不同的序列。

retstep 關(guān)鍵字參數(shù) 

這是一個布爾型可選參數(shù)(如果已指定),還將返回步長以及序列數(shù)組,從而產(chǎn)生一個元組作為輸出

import numpy as np

 

a = np.linspace(0.02, 2, 10, retstep=True)

 

print('Linear Sequence from 0.02 to 2:', a)

print('Length:', len(a))

輸出

Linear Sequence from 0.02 to 2: (array([0.02, 0.24, 0.46, 0.68, 0.9 , 1.12, 1.34, 1.56, 1.78, 2.  ]), 0.22)

Length: 2

由于輸出是元組,因此它的長度是2,而不是10!

axis 關(guān)鍵字參數(shù) 

這將在結(jié)果中設置軸以存儲樣本。 僅當開始和端點為數(shù)組數(shù)據(jù)類型時才使用它。

默認情況下( axis=0 ),采樣將沿著在開始處插入的新軸進行。 我們可以使用axis=-1來獲得末端的軸。

import numpy as np

 

p = np.array([[1, 2], [3, 4]])

q = np.array([[5, 6], [7, 8]])

 

r = np.linspace(p, q, 3, axis=0)

print(r)

s = np.linspace(p, q, 3, axis=1)

print(s)

輸出

array([[[1., 2.],

        [3., 4.]],

 

       [[3., 4.],

        [5., 6.]],

 

       [[5., 6.],

        [7., 8.]]])

 

array([[[1., 2.],

        [3., 4.],

        [5., 6.]],

 

       [[3., 4.],

        [5., 6.],

        [7., 8.]]])

在第一種情況下,由于axis = 0 ,我們從第一個軸獲取序列限制。

在這里,限制是子數(shù)組對[1, 2] and [5,6]以及[3, 4] and [7,8] ,它們?nèi)∽?code>p和q的第一軸。 現(xiàn)在,我們比較結(jié)果對中的相應元素以生成序列。

因此,第一行的順序為[[1 to 5], [2 to 6]] ,第二行的順序為[[1 to 5], [2 to 6]] [[3 to 7], [4 to 8]] ,對其進行評估并組合形成[ [[1, 2], [3, 4]], [[3, 4], [5, 6]], [[5, 6], [7,8]] ] 。

第二種情況將在axis=1或列中插入新元素。 因此,新軸將通過列序列生成。 而不是行序列。

考慮序列[1, 2] to [5, 7][3, 4] to [7, 8]并將其插入到結(jié)果的列中,得到[[[1, 2], [3, 4], [5, 6]], [[3, 4], [5, 6], [7, 8]]] 。

推薦好課:Python3進階:數(shù)據(jù)分析及可視化、Python 自動化辦公


0 人點贊