Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么是size=()的numpy数组?为什么它乘以1会返回一个numpy int,这两者有什么不同? 问题_Python_Arrays_Numpy - Fatal编程技术网

Python 什么是size=()的numpy数组?为什么它乘以1会返回一个numpy int,这两者有什么不同? 问题

Python 什么是size=()的numpy数组?为什么它乘以1会返回一个numpy int,这两者有什么不同? 问题,python,arrays,numpy,Python,Arrays,Numpy,我有一个非常简单的函数,它使用numpy.where()进行非常简单的计算 如果输入是标量,则输出是大小为()的numpy数组 如果我把它乘以1,它就会变成一个相同大小的numpy int32 我的问题是: 1和2的区别是什么 它们都是标量吗?像numpy标量这样的东西真的存在吗 为什么乘以1会改变类型?这是一个已知的、有文档记录的功能/错误吗 为什么其他numpy函数,例如np.arange(5,6)返回一个大小为(1,)的数组 我怀疑我是第一个遇到这个问题的人,但我在网上没有找到太多 我

我有一个非常简单的函数,它使用
numpy.where()
进行非常简单的计算

  • 如果输入是标量,则输出是大小为()的numpy数组
  • 如果我把它乘以1,它就会变成一个相同大小的numpy int32
  • 我的问题是: 1和2的区别是什么

    • 它们都是标量吗?像numpy标量这样的东西真的存在吗
    • 为什么乘以1会改变类型?这是一个已知的、有文档记录的功能/错误吗
    • 为什么其他numpy函数,例如
      np.arange(5,6)
      返回一个大小为
      (1,)
      的数组
    我怀疑我是第一个遇到这个问题的人,但我在网上没有找到太多

    我发现了形状数组(n,)和形状数组(n,1)之间的区别,但这是另一回事

    玩具示例: Spyder截图
    是的,有一个
    numpy
    标量

    numpy数组可以具有0、1、2或更多维度。两者之间有很多重叠

    np.int64(3)          # numpy int
    np.array(3)          # 0d array
    np.array([3])        # 1d array with 1 element
    np.int(3)            # python int
    3                    # python int
    
    前3个具有数组属性,如
    shape
    dtype
    。前两者之间的差别很小

    在像
    where
    这样的函数中,
    numpy
    首先将参数转换为数组,例如
    np.array(5)
    np.array(1)

    但类似数学的加上标量可能会返回numpy标量:

    In [164]: np.array(5) + 1
    Out[164]: 6
    In [165]: type(_)
    Out[165]: numpy.int64
    In [166]: np.array(5) * 1
    Out[166]: 5
    In [167]: type(_)
    Out[167]: numpy.int64
    
    对数组进行索引也可以产生这样的标量:

    In [182]: np.arange(3)[1]
    Out[182]: 1
    In [183]: type(_)
    Out[183]: numpy.int64
    
    其中
    “广播”参数,因此结果形状在广播意义上是“最大的”:

    如果
    spyder
    具有类似
    ipython
    的制表符完成功能,则可以获得附加到对象的所有方法的列表。
    np.int64(3)
    的方法与
    np.array(3)
    的方法非常相似。但与
    3
    大不相同

    如果其中一个维度为0,则也有包含0个元素的数组

    Out[184]: array([], dtype=int64)
    In [185]: _.shape
    Out[185]: (0,)
    In [186]: np.arange(1)
    Out[186]: array([0])
    In [187]: _.shape
    Out[187]: (1,)
    
    显然,0d不能有0个元素,因为它没有任何0维

    为0d数组(或numpy标量)编制索引是一个小技巧(但仍然符合逻辑):

    加法的返回可以用“array\u priority”来解释

    dtype
    在这样的操作中不保留。将浮点值添加到int,并获取浮点值

    In [203]: type(np.array(3, np.int16) + 3)
    Out[203]: numpy.int64
    In [204]: type(np.array(3, np.int16) + 3.0)
    Out[204]: numpy.float64
    
    ufunc
    casting
    +
    实际上是对
    np.add
    ufunc的调用
    ufunc
    采用一些关键词,如
    casting
    ,可以更好地控制结果:

    In [214]: np.add(np.array(3, np.int16), 3)
    Out[214]: 6
    In [215]: np.add(np.array(3, np.int16), 3, casting='no')
    Traceback (most recent call last):
      File "<ipython-input-215-631cb3a3b303>", line 1, in <module>
        np.add(np.array(3, np.int16), 3, casting='no')
    UFuncTypeError: Cannot cast ufunc 'add' input 0 from dtype('int16') to dtype('int64') with casting rule 'no'
        
    In [217]: np.add(np.array(3, np.int16), 3, casting='safe')
    Out[217]: 6
    
    我不知道它是在哪里记录的,但是一个操作通常会返回一个
    numpy标量
    ,而不是一个0d数组

    我刚刚想起/发现了0d和numpy标量之间的一个区别——易变性

    In [222]: x
    Out[222]: array(3)
    In [223]: x[...] = 4
    In [224]: x
    Out[224]: array(4)
    In [225]: x = np.int64(3)
    In [226]: x[...] = 4
    Traceback (most recent call last):
      File "<ipython-input-226-f7dca2cc5565>", line 1, in <module>
        x[...] = 4
    TypeError: 'numpy.int64' object does not support item assignment
    
    [222]中的
    :x
    Out[222]:数组(3)
    在[223]中:x[…]=4
    In[224]:x
    Out[224]:数组(4)
    In[225]:x=np.int64(3)
    在[226]中:x[…]=4
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    x[…]=4
    TypeError:“numpy.int64”对象不支持项分配
    

    Python类可以共享许多行为/方法,但在其他方面有所不同。

    非常有洞察力,谢谢。但是,我仍然不清楚为什么将它乘以1会返回标量,从而有效地改变数据类型。也不知道为什么其他numpy函数返回大小为(1,)大小为1或
    shape
    (1,)的数组
    np.int64(3)
    np.array(3)
    ,np.array([3])`都有大小1。你说得对,我指的是形状
    In [189]: np.array(3)[()]      # 0 element indexing tuple
    Out[189]: 3
    In [190]: type(_)
    Out[190]: numpy.int64
    In [191]: np.array(3).item()
    Out[191]: 3
    In [192]: type(_)
    Out[192]: int
    In [193]: np.array(3)[()][()]
    Out[193]: 3
    
    In [203]: type(np.array(3, np.int16) + 3)
    Out[203]: numpy.int64
    In [204]: type(np.array(3, np.int16) + 3.0)
    Out[204]: numpy.float64
    
    In [214]: np.add(np.array(3, np.int16), 3)
    Out[214]: 6
    In [215]: np.add(np.array(3, np.int16), 3, casting='no')
    Traceback (most recent call last):
      File "<ipython-input-215-631cb3a3b303>", line 1, in <module>
        np.add(np.array(3, np.int16), 3, casting='no')
    UFuncTypeError: Cannot cast ufunc 'add' input 0 from dtype('int16') to dtype('int64') with casting rule 'no'
        
    In [217]: np.add(np.array(3, np.int16), 3, casting='safe')
    Out[217]: 6
    
    In [194]: np.array(3).__array_priority__
    Out[194]: 0.0
    In [195]: np.int64(3).__array_priority__
    Out[195]: -1000000.0
    In [196]: np.array(3) + np.int64(3)
    Out[196]: 6
    In [197]: type(_)
    Out[197]: numpy.int64
    
    In [222]: x
    Out[222]: array(3)
    In [223]: x[...] = 4
    In [224]: x
    Out[224]: array(4)
    In [225]: x = np.int64(3)
    In [226]: x[...] = 4
    Traceback (most recent call last):
      File "<ipython-input-226-f7dca2cc5565>", line 1, in <module>
        x[...] = 4
    TypeError: 'numpy.int64' object does not support item assignment