开发者

Python Numpy ndarray

I have 3 code snippets:

(1)

x = array([[]]) #x.ndim=2
x=append(x,[[1,2]]) #after this, x.ndim=1???????????????????
x=append(x,[[3,4]],axis=0) #error b/c dimension

(2)
    x = array([[]]) #x.ndim=2
    x=append(x,[[1,2]],axis=0) #error b/c dimension?????????????????

(3)
    x=append([[1,2]],[[3,4]],axis=0) #Good

The (???????????) is the part I don't understand. Can you explain?

I prefer (2) which is declare an nump开发者_StackOverflowy.ndarray of 2 axises first then append data later. How can I do that?

Thanks.


From the append documentation:

Definition:     append(arr, values, axis=None)
Docstring:
    Append values to the end of an array.                                                                      

    Parameters
    ----------
    arr : array_like
        Values are appended to a copy of this array.
    values : array_like
        These values are appended to a copy of `arr`.  It must be of the
        correct shape (the same shape as `arr`, excluding `axis`).  If `axis`
        is not specified, `values` can be any shape and will be flattened
        before use.
    axis : int, optional
        The axis along which `values` are appended.  If `axis` is not given,
        both `arr` and `values` are flattened before use.

That is, why your example (1) fails is that as you don't specify an axis argument for the first append, x is flattened, and hence the second append fails as the shapes no longer match.

Your second example fails because the shapes don't match. At the start, x.shape = (1,0), that is 1 row and 0 columns. Then you try to add an array with shape (1,2) along the 0'th axis (that is, you want the result array to have more rows but the same number of columns) which of course doesn't work since the number of columns doesn't match.

As an aside, personally I almost never use append() when working with numpy. It's much more efficient to allocate the correct size upfront and then use slicing to fill it in rather than using append which implies a reallocation and copy every time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜