簡體   English   中英

為什么 Numpy 陣列重塑需要兩個括號?

[英]Why Numpy array reshape takes two brackets?

在學習 Python numpy 庫時,我看到數組reshape了 function。

a = [1,2,3]
b = [5,6,7]
c = a + b
d = np.array(c)
e = d.reshape((2,3))
print(e)

在上面的代碼中,為什么reshape需要兩個括號?

為什么我不能這樣寫?

e = d.reshape(2,3)

我在其他任何地方都看到了這個問題。

reshape 方法獲取形狀參數,它應該是元組為 (2, 3)

    def reshape(self, shape, *shapes, order='C'): # known case of numpy.core.multiarray.ndarray.reshape
        """
        a.reshape(shape, order='C')
        
            Returns an array containing the same data with a new shape.
        
            Refer to `numpy.reshape` for full documentation.
        
            See Also
            --------
            numpy.reshape : equivalent function
        
            Notes
            -----
            Unlike the free function `numpy.reshape`, this method on `ndarray` allows
            the elements of the shape parameter to be passed in as separate arguments.
            For example, ``a.reshape(10, 11)`` is equivalent to
            ``a.reshape((10, 11))``.
        """
        pass
    ```

如 numpy.rehsape 的numpy文檔中所述:

numpy.reshape(a, newshape, order='C')

newshape 必須是整數或整數元組。

ndarray.reshape 的文檔中:

ndarray.reshape(shape, order='C')

"Unlike the free function numpy.reshape, this method on ndarray allows the elements of the shape parameter to be passed in as separate arguments. For example, a.reshape(10, 11) is equivalent to a.reshape((10, 11 ))。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM