Numpy陣列操作

maxchine發表於2020-10-25

變形

  • numpy.ndarray.shape表示陣列的維度,返回一個元組,這個元組的長度就是維度的數目,即 ndim 屬性(秩)
  • numpy.ndarray.flat 將陣列轉換為一維的迭代器,可以用for訪問陣列每一個元素。
  • numpy.ndarray.flatten([order=‘C’]) 將陣列的副本轉換為一維陣列,並返回。

轉置

  • numpy.transpose(a, axes=None) Permute the dimensions of an array.
  • numpy.ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.

維度

  • numpy.newaxis = None None的別名,對索引陣列很有用。
  • numpy.squeeze(a, axis=None) 從陣列的形狀中刪除單維度條目,即把shape中為1的維度去掉。

組合

  • numpy.concatenate((a1, a2, …), axis=0, out=None) Join a sequence of arrays along an existing axis.
  • numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis.
  • numpy.vstack(tup)Stack arrays in sequence vertically (row wise).
  • numpy.hstack(tup)Stack arrays in sequence horizontally (column wise).

拆分

  • numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays as views into ary.
  • numpy.vsplit(ary, indices_or_sections) Split an array into multiple sub-arrays vertically (row-wise).
  • numpy.hsplit(ary, indices_or_sections) Split an array into multiple sub-arrays horizontally (column-wise).

平鋪

  • numpy.tile(A, reps) Construct an array by repeating A the number of times given by reps.
  • numpy.repeat(a, repeats, axis=None) Repeat elements of an array.

增刪

  • numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None) Find the unique elements of an array.

相關文章