单括号和双括号Numpy数组有什么区别?

MVS*_*ath 3 python arrays numpy

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))
Run Code Online (Sandbox Code Playgroud)

输出:

Data type of A:  <class 'numpy.ndarray'>
Data type of A:  <class 'numpy.ndarray'>
Run Code Online (Sandbox Code Playgroud)

在np.zeros()中,要声明一个数组,我们在2个方括号中给出输入,而在np.random.radn()中,在1个方括号中给出输入?

语法是否有任何特定原因,因为它们都是相同的数据类型,但是遵循不同的语法?

unu*_*tbu 6

为了简化Matlab用户向NumPy的过渡,randn构建了一些便利功能,例如使用与Matlab等效项相同的呼叫签名。

以NumPy为中心(相对于以Matlab为中心)的NumPy函数(例如np.zeros)期望size(或shape)是一个元组。这允许其他参数(例如dtype和)order也可以传递给函数。以Matlab为中心的函数假定所有参数都是大小的一部分。

np.random.randn是NumPy以Matlab为中心的便利功能之一,以Matlab的randn为模型。以NumPy为中心的替代方案np.random.randnnp.random.standard_normal