[英]What is the difference between single and double bracket Numpy array?
import numpy as np
a=np.random.randn(1, 2)
b=np.zeros((1,2))
print("Data type of A: ",type(a))
print("Data type of A: ",type(b))
輸出:
Data type of A: <class 'numpy.ndarray'>
Data type of A: <class 'numpy.ndarray'>
在np.zeros()中,為了聲明一個數組,我們在2個括號中給出輸入,而在np.random.radn()中,我們在1個括號中給它?
是否有任何特定的語法原因,因為它們都是相同的數據類型但遵循不同的語法?
為了簡化Matlab用戶到NumPy的過渡,我們構建了一些像randn
這樣的便利函數,它們使用與Matlab等效的相同的調用簽名。
以NumPy為中心(與Matlab相對的)NumPy函數(例如np.zeros
)期望size
(或shape
)為元組。 這允許其他參數如dtype
和order
也被傳遞給函數。 以Matlab為中心的函數假設所有參數都是大小的一部分。
np.random.randn
是NumPy以Matlab為中心的便利函數之一,以Matlab的 np.random.randn
為藍本。 np.random.randn
的以NumPy為中心的替代np.random.randn
是np.random.standard_normal
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.