单括号和双括号Numpy数组有什么区别? [英] What is the difference between single and double bracket Numpy array?

查看:978
本文介绍了单括号和双括号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))

输出:

Data type of A:  <class 'numpy.ndarray'>
Data type of A:  <class 'numpy.ndarray'>

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

In np.zeros(), to declare an array we give the input in 2 brackets whereas in np.random.radn(), we give it in 1 bracket?

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

Is there any specific reason for the syntax,as both of them are of same data type but follow a different syntax?

推荐答案

为简化Matlab用户向NumPy的过渡,构建了一些便捷功能,如randn,它们使用与Matlab等效项相同的调用签名.

In an effort to ease the transition for Matlab users to NumPy, some convenience functions like randn were built which use the same call signature as their Matlab equivalents.

以NumPy为中心(而不是以Matlab为中心)的NumPy函数(例如np.zeros)期望size(或shape)是一个元组.这也允许将其他参数(如dtypeorder)传递给该函数. 以Matlab为中心的函数假定所有参数都是大小的一部分.

The more NumPy-centric (as opposed to Matlab-centric) NumPy functions (such as np.zeros) expect the size (or shape) to be a tuple. This allows other parameters like dtype and order to be passed to the function as well. The Matlab-centric functions assume all the arguments are part of the size.

np.random.randn 是NumPy以Matlab为中心的便利功能之一,它以 Matlab的randn为模型. np.random.randn的更以NumPy为中心的替代方法是

np.random.randn is one of NumPy's Matlab-centric convenience functions, modeled after Matlab's randn. The more NumPy-centric alternative to np.random.randn is np.random.standard_normal.

这篇关于单括号和双括号Numpy数组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆