生活随笔
收集整理的這篇文章主要介紹了
【Numpy】array操作总结
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- 官方Document: https://www.numpy.org/devdocs/reference/routines.array-manipulation.html
- 開發(fā)測試環(huán)境
- Win10
- Python 3.6.4
- NumPy 1.14.2
Basic operations
函數(shù)原型作用
| [copyto](dst, src[, casting, where]) | Copies values from one array to another, broadcasting as necessary. |
Changing array shape
函數(shù)原型作用
| [reshape](a, newshape[, order]) | Gives a new shape to an array without changing its data. |
| [ravel](a[, order]) | Return a contiguous flattened array. |
| [ndarray.flat] | A 1-D iterator over the array. |
| ndarray.flatten | Return a copy of the array collapsed into one dimension. |
Transpose-like operations
函數(shù)原型作用
| [moveaxis](a, source, destination) | Move axes of an array to new positions. |
| [rollaxis](a, axis[, start]) | Roll the specified axis backwards, until it lies in a given position. |
| [swapaxes](a, axis1, axis2) | Interchange two axes of an array. |
| [ndarray.T] | Same as self.transpose(), except that self is returned if self.ndim < 2. |
| [transpose](a[, axes]) | Permute the dimensions of an array. |
Changing number of dimensions
函數(shù)原型作用
| atleast_1d(*arys) | Convert inputs to arrays with at least one dimension. |
| atleast_2d(*arys) | View inputs as arrays with at least two dimensions. |
| atleast_3d(*arys) | View inputs as arrays with at least three dimensions. |
| broadcast | Produce an object that mimics broadcasting. |
| broadcast_to(array, shape[, subok]) | Broadcast an array to a new shape. |
| broadcast_arrays(*args, **kwargs) | Broadcast any number of arrays against each other. |
| expand_dims(a, axis) | Expand the shape of an array. |
| squeeze(a[, axis]) | Remove single-dimensional entries from the shape of an array. |
expand_dims
擴展array的shape, 插入一個新的軸,該軸將出現(xiàn)在擴展陣列形狀的軸位置
>>> x = np.array([
1,
2])
>>> x.shape
(
2,)
以下操作相當于 x[np.newaxis,:] or x[np.newaxis]:
>>> y = np.expand_dims(x, axis=
0)
>>> y
array(
[[1, 2]])
>>> y.shape
(
1,
2)
>>> y = np.expand_dims(x, axis=
1) # Equivalent to x[:,np.newaxis]
>>> y
array(
[[1],[2]])
>>> y.shape
(
2,
1)
注意在一些列子中使用None替換np.newaxis
>>> np.newaxis
is None
True
squeeze
從數(shù)組的形狀中移除一維條目
- a : array
- axis: None或者整數(shù)或者整數(shù)元組,默認是None
選擇shape單維度的條目,若選取的axis的shape條目不為1,則會拋出異常
>>>
x = np
.array([[[
0], [
1], [
2]]])
>>>
x.shape
(
1,
3,
1)
>>> np
.squeeze(
x)
.shape
(
3,)
>>> np
.squeeze(
x, axis=
0)
.shape
(
3,
1)
>>> np
.squeeze(
x, axis=
1)
.shape
Traceback (most recent
call last):
...
ValueError: cannot select an axis to squeeze
out which has size not equal to one
>>> np
.squeeze(
x, axis=
2)
.shape
(
1,
3)
axis參數(shù)輸入為整數(shù)列表時,起作用相當于axis=None時的作用
Changing kind of array
函數(shù)原型作用
| [asarray](a[, dtype, order]) | Convert the input to an array. |
| [asanyarray](a[, dtype, order]) | Convert the input to an ndarray, but pass ndarray subclasses through. |
| [asmatrix](data[, dtype]) | Interpret the input as a matrix. |
| [asfarray](a[, dtype]) | Return an array converted to a float type. |
| [asfortranarray](a[, dtype]) | Return an array laid out in Fortran order in memory. |
| [ascontiguousarray](a[, dtype]) | Return a contiguous array in memory (C order). |
| [asarray_chkfinite](a[, dtype, order]) | Convert the input to an array, checking for NaNs or Infs. |
| asscalar | Convert an array of size 1 to its scalar equivalent. |
| [require](a[, dtype, requirements]) | Return an ndarray of the provided type that satisfies requirements. |
Joining arrays
函數(shù)原型作用
| concatenate((a1, a2, …)[, axis, out]) | Join a sequence of arrays along an existing axis. |
| stack(arrays[, axis, out]) | Join a sequence of arrays along a new axis. |
| column_stack(tup) | Stack 1-D arrays as columns into a 2-D array. |
| dstack(tup) | Stack arrays in sequence depth wise (along third axis). |
| hstack(tup) | Stack arrays in sequence horizontally (column wise). |
| vstack(tup) | Stack arrays in sequence vertically (row wise). |
| block(arrays) | Assemble an nd-array from nested lists of blocks. |
concatenate
沿著指定的維度進行合并,結果是該維度上shape增加
- a1, a2, … : array序列,除了axis對應的維度外,其他shape相同
- axis : 沿著axis維進行結合,結合后這個維的shape是序列這個維的shape之和
>>> a = np.array([[
1,
2], [
3,
4]])
>>> b = np.array([[
5,
6]])
>>> a.shape, b.shape
((
2,
2), (
1,
2))
>>> c = np.concatenate((a, b), axis=
0)
>>> c.shape
(
3,
2)
>>> c
array([[
1,
2],[
3,
4],[
5,
6]])
>>> d = np.concatenate((a, b.T), axis=
1)
>>> d
array([[
1,
2,
5],[
3,
4,
6]])
>>> d.shape
(
2,
3)
Splitting arrays
函數(shù)原型作用
| [split](ary, indices_or_sections[, axis]) | Split an array into multiple sub-arrays. |
| [array_split](ary, indices_or_sections[, axis]) | Split an array into multiple sub-arrays. |
| [dsplit](ary, indices_or_sections) | Split array into multiple sub-arrays along the 3rd axis (depth). |
| [hsplit](ary, indices_or_sections) | Split an array into multiple sub-arrays horizontally (column-wise). |
| [vsplit](ary, indices_or_sections) | Split an array into multiple sub-arrays vertically (row-wise). |
Tiling arrays
函數(shù)原型作用
| [tile](A, reps) | Construct an array by repeating A the number of times given by reps. |
| [repeat](a, repeats[, axis]) | Repeat elements of an array. |
Adding and removing elements
函數(shù)原型作用
| [delete](arr, obj[, axis]) | Return a new array with sub-arrays along an axis deleted. |
| [insert](arr, obj, values[, axis]) | Insert values along the given axis before the given indices. |
| [append](arr, values[, axis]) | Append values to the end of an array. |
| [resize](a, new_shape) | Return a new array with the specified shape. |
| [trim_zeros](filt[, trim]) | Trim the leading and/or trailing zeros from a 1-D array or sequence. |
| [unique](ar[, return_index, return_inverse, …]) | Find the unique elements of an array. |
Rearranging elements
函數(shù)原型作用
| [flip](m[, axis]) | Reverse the order of elements in an array along the given axis. |
| fliplr | Flip array in the left/right direction. |
| flipud | Flip array in the up/down direction. |
| [reshape](a, newshape[, order]) | Gives a new shape to an array without changing its data. |
| [roll](a, shift[, axis]) | Roll array elements along a given axis. |
| [rot90](m[, k, axes]) | Rotate an array by 90 degrees in the plane specified by axes. |
總結
以上是生活随笔為你收集整理的【Numpy】array操作总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。