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
code>) 成为一个元组。这允许将其他参数(如 dtype
和 order
)也传递给该函数。
以 Matlab 为中心的函数假定所有参数都是大小的一部分。
np.random.randn
是 NumPy 以 Matlab 为中心的便捷函数之一,以 Matlab's randn 为模型. np.random.randn
更以 NumPy 为中心的替代方案是 np.random.standard_normal
.
关于python - 单括号和双括号 Numpy 数组有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634088/