最近遇到numpy.zero()这个函数时有几个疑惑的地方:
1、想生成5×2阶的零矩阵时为什么不是zeros(5,2),而是多了个括号zeros((5,2))?
2、查到帮助文档的示例zeros((5,))=array([ 0., 0., 0., 0., 0.]),表示疑惑,(5,)代表什么呢?后面,省略了什么?0?1?
1.从帮助文档得到答案:第一个参数表示矩阵的shape,如果是zeros(5,2)则给两个参数赋值了shape=5, dytpe=2, 这是错误的,(5,2)代表一个参数而已。
help(numpy.zeros) Help on built-in function zeros in module numpy.core.multiarray: zeros(...) zeros(shape, dtype=float, order='C') Return a new array of given shape and type, filled with zeros. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.float64`. order : {'C', 'F'}, optional Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory. Returns ------- out : ndarray Array of zeros with the given shape, dtype, and order. Examples -------- np.zeros(5) array([ 0., 0., 0., 0., 0.]) np.zeros((5,), dtype=np.int) array([0, 0, 0, 0, 0]) np.zeros((2, 1)) array([[ 0.], [ 0.]]) s = (2,2) np.zeros(s) array([[ 0., 0.], [ 0., 0.]]) np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])
2、继续查帮助文档
help(numpy.zeros(5,)) Help on ndarray object: class ndarray(__builtin__.object) | ndarray(shape, dtype=float, buffer=None, offset=0, | strides=None, order=None) | | An array object represents a multidimensional, homogeneous array | of fixed-size items. An associated data-type object describes the | format of each element in the array (its byte-order, how many bytes it | occupies in memory, whether it is an integer, a floating point number, | or something else, etc.) | | Arrays should be constructed using `array`, `zeros` or `empty` (refer | to the See Also section below). The parameters given here refer to | a low-level method (`ndarray(...)`) for instantiating an array. | | For more information, refer to the `numpy` module and examine the | the methods and attributes of an array. | | Parameters | ---------- | (for the __new__ method; see Notes below) | | shape : tuple of ints | Shape of created array. | dtype : data-type, optional | Any object that can be interpreted as a numpy data type. | buffer : object exposing buffer interface, optional | Used to fill the array with data. | offset : int, optional | Offset of array data in buffer. | strides : tuple of ints, optional | Strides of data in memory. | order : {'C', 'F'}, optional | Row-major (C-style) or column-major (Fortran-style) order. | ..................
这时才发现zeros的第一个参数shape是由整数或tuple表示,这时意识到(5,)表示的应该是tuple,于是查了tuple的用法及注意事项就明白了,答案在下面标红标粗加大,简单理解(5,)就代表5,随便复习下tuple用法:
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字:
>>> classmates = ('Michael', 'Bob', 'Tracy')现在,classmates这个tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的,你可以正常地使用classmates[0],classmates[-1],但不能赋值成另外的元素。
不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
tuple的陷阱:当你定义一个tuple时,在定义的时候,tuple的元素就必须被确定下来,比如:
>>> t = (1, 2) >>> t (1, 2)如果要定义一个空的tuple,可以写成():
>>> t = () >>> t ()但是,要定义一个只有1个元素的tuple,如果你这么定义:
>>> t = (1) >>> t 1定义的不是tuple,是1这个数!这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1。
所以,只有1个元素的tuple定义时必须加一个逗号,,来消除歧义:
>>> t = (1,) >>> t (1,)Python在显示只有1个元素的tuple时,也会加一个逗号,,以免你误解成数学计算意义上的括号。
最后来看一个“可变的”tuple:
>>> t = ('a', 'b', ['A', 'B']) >>> t[2][0] = 'X' >>> t[2][1] = 'Y' >>> t ('a', 'b', ['X', 'Y'])这个tuple定义的时候有3个元素,分别是'a','b'和一个list。不是说tuple一旦定义后就不可变了吗?怎么后来又变了?
别急,我们先看看定义的时候tuple包含的3个元素:
当我们把list的元素'A'和'B'修改为'X'和'Y'后,tuple变为:
表面上看,tuple的元素确实变了,但其实变的不是tuple的元素,而是list的元素。tuple一开始指向的list并没有改成别的list,所以,tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。即指向'a',就不能改成指向'b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!
理解了“指向不变”后,要创建一个内容也不变的tuple怎么做?那就必须保证tuple的每一个元素本身也不能变。