Numpy 常量

NumPy包括幾個常量:
np.e、np.pi、 np.inf、 np.nan、np.NINF、np.PZERO & np.NZERO、np.euler_gamma、np.newaxis

np.e

exp : 指數函數日誌:自然對數。
也稱爲歐拉的常數,自然對數的基礎,納皮爾的常數。

e = 2.71828182845904523536028747135266249775724709369995…


np.pi

pi:圓周率
pi = 3.1415926535897932384626433…


np.inf

注意!
Inf,Infinity,PINF 和 infty 是 inf 的別名
NumPy 使用IEEE二進制浮點算法標準(IEEE 754), 表示(正)無窮大
這意味着Not a Number不等於無窮大。 此外,正無窮大不等於負無窮大。 但無窮大相當於正無窮大。

返回

  • y : float (正無窮大的浮點表示。)

另見

  • isinf : 顯示哪些元素爲正或負無窮大。
  • isposinf : 顯示哪些元素是正無窮大。
  • isneginf : 顯示哪些元素爲負無窮大。
  • isnan : 顯示哪些元素不是數字。
  • isfinite : 顯示哪些元素是有限的(不是非數字,正無窮大和負無窮大中的一個)
>>> np.inf
inf
>>> np.array([1]) / 0.
array([ Inf])

np.nan

注意! NaN 和 NAN 是 nan 的別名。
NumPy使用IEEE二進制浮點算法標準(IEEE 754),表示非數字(NaN)
這意味着Not a Number不等於無窮大。

另見

  • isnan : 顯示哪些元素不是數字。
  • isfinite : 顯示哪些元素是有限的(不是非數字,正無窮大和負無窮大中的一個)
>>> np.nan
nan
>>> np.log(-1)
nan
>>> np.log([-1, 1, 2])
array([ NaN , 0. , 0.69314718 ])

np.NINF

注意!
NumPy使用IEEE二進制浮點算法標準(IEEE 754),表示負無窮大
這意味着Not a Number不等於無窮大。 此外,正無窮大不等於負無窮大。 但無窮大相當於正無窮大。

返回

  • y : float (負無窮大的浮點表示)
>>> np.NINF
-inf
>>> np.log(0)
-inf

np.PZERO & np.NZERO

注意
np.PZERO 表示正零,正零被認爲是有限數。
np.NZERO 表示負零,負零被認爲是有限數。

返回

  • y = np.PZERO() : float (正零的浮點表示)
  • y = np.NZERO() : float (負零點的浮點表示)

另外

  • isinf : 顯示哪些元素爲正或負無窮大。
  • isposinf : 顯示哪些元素是正無窮大。
  • isneginf : 顯示哪些元素爲負無窮大。
  • isnan : 顯示哪些元素不是數字。
  • isfinite : 顯示哪些元素是有限的 - 不是(非數字,正無窮大和負無窮大)之一。
>>> np.PZERO
0.0
>>> np.NZERO
-0.0

>>> np.isfinite([np.PZERO])
array([ True])
>>> np.isnan([np.PZERO])
array([False])
>>> np.isinf([np.PZERO])
array([False])

____________________________________________________________________

>>> np.NZERO
-0.0
>>> np.PZERO
0.0

>>> np.isfinite([np.NZERO])
array([ True])
>>> np.isnan([np.NZERO])
array([False])
>>> np.isinf([np.NZERO])
array([False])

np.euler_gamma

γ = 0.5772156649015328606065120900824024310421…


np.newaxis

None 的便捷別名,對索引數組很有用。

>>> newaxis is None
True
>>> x = np.arange(3)
>>> x
array([0, 1, 2])
>>> x[:, newaxis]
array([[0],
[1],
[2]])
>>> x[:, newaxis, newaxis]
array([[[0]],
[[1]],
[[2]]])
>>> x[:, newaxis] * x
array([[0, 0, 0],
[0, 1, 2],
[0, 2, 4]])

外積,與 outer(x, y) 相同:

>>> y = np.arange(3, 6)
>>> x[:, newaxis] * y
array([[ 0,  0,  0],
[ 3,  4,  5],
[ 6,  8, 10]])

x[newaxis, :] 相當於 x[newaxis] 和 x[None]:

>>> x[newaxis, :].shape
(1, 3)
>>> x[newaxis].shape
(1, 3)
>>> x[None].shape
(1, 3)
>>> x[:, newaxis].shape
(3, 1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章