https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
注意arange等函数生成的对象没有维度.既.shape属性,第二个数字是空.要用reshape来将它转换为多维度,这样才能像矩阵那样操作.
创建数组
自动生成数组
name | describe |
---|
empty(shape[, dtype, order]) | 生成给定形状的数组,数组数据是随机数 |
empty_like(a[, dtype, order, subok]) | Return a new array with the same shape and type as a given array. |
eye(N[, M, k, dtype]) | Return a 2-D array with ones on the diagonal and zeros elsewhere. |
identity(n[, dtype]) | Return the identity array. |
ones(shape[, dtype, order]) | 生成给定形状的数组,数组的数据是1 |
ones_like(a[, dtype, order, subok]) | Return an array of ones with the same shape and type as a given array. |
zeros(shape[, dtype, order]) | 生成给定形状的数组,数组的数据是0 |
zeros_like(a[, dtype, order, subok]) | Return an array of zeros with the same shape and type as a given array. |
full(shape, fill_value[, dtype, order]) | 生成给定形状的数组,数组的数据用fill_value填充 |
full_like(a, fill_value[, dtype, order, subok]) | Return a full array with the same shape and type as a given array. |
arange([start,] stop[, step,][, dtype]) | 生成等差数组,设定步长 |
linspace(start, stop[, num, endpoint, …]) | 生成等差数组,设定生成的数目 |
logspace(start, stop[, num, endpoint, base, …]) | Return numbers spaced evenly on a log scale. |
geomspace(start, stop[, num, endpoint, dtype]) | Return numbers spaced evenly on a log scale (a geometric progression). |
meshgrid(*xi, **kwargs) | Return coordinate matrices from coordinate vectors. |
mgrid | nd_grid instance which returns a dense multi-dimensional “meshgrid”.) |
ogrid | nd_grid instance which returns an open multi-dimensional “meshgrid”.) |
从已有数据转化为数组
name | describe |
---|
array(object[, dtype, copy, order, subok, ndmin]) | Create an array. |
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. |
ascontiguousarray(a[, dtype]) | Return a contiguous array in memory (C order). |
asmatrix(data[, dtype]) | Interpret the input as a matrix. |
copy(a[, order]) | Return an array copy of the given object. |
frombuffer(buffer[, dtype, count, offset]) | Interpret a buffer as a 1-dimensional array. |
fromfile(file[, dtype, count, sep]) | Construct an array from data in a text or binary file. |
fromfunction(function, shape, **kwargs) | Construct an array by executing a function over each coordinate. |
fromiter(iterable, dtype[, count]) | Create a new 1-dimensional array from an iterable object. |
fromstring(string[, dtype, count, sep]) | A new 1-D array initialized from raw binary or text data in a string. |
loadtxt(fname[, dtype, comments, delimiter, …]) | Load data from a text file. |
数组属性
属性 | 描述 |
---|
Memory layout | |
ndarray.flags | Information about the memory layout of the array. |
ndarray.shape | Tuple of array dimensions. |
ndarray.strides | Tuple of bytes to step in each dimension when traversing an array. |
ndarray.ndim | Number of array dimensions. |
ndarray.data | Python buffer object pointing to the start of the array’s data. |
ndarray.size | Number of elements in the array. |
ndarray.itemsize | Length of one array element in bytes. |
ndarray.nbytes | Total bytes consumed by the elements of the array. |
ndarray.base | Base object if memory is from some other object. |
Data type | |
ndarray.dtype | Data-type of the array’s elements. |
Other attributes | |
ndarray.T | Same as self.transpose(), except that self is returned if self.ndim < 2. |
ndarray.real | The real part of the array. |
ndarray.imag | The imaginary part of the array. |
ndarray.flat | A 1-D iterator over the array. |
ndarray.ctypes | An object to simplify the interaction of the array with the ctypes module. |
ctypes foreign function interface | |
ndarray.ctypes | An object to simplify the interaction of the array with the ctypes module. |
数组方法
数组转换
方法 | 描述 |
---|
ndarray.item(*args) | Copy an element of an array to a standard Python scalar and return it. |
ndarray.tolist() | Return the array as a (possibly nested) list. |
ndarray.itemset(*args) | Insert scalar into an array (scalar is cast to array’s dtype, if possible) |
ndarray.tostring([order]) | Construct Python bytes containing the raw data bytes in the array. |
ndarray.tobytes([order]) | Construct Python bytes containing the raw data bytes in the array. |
ndarray.tofile(fid[, sep, format]) | Write array to a file as text or binary (default). |
ndarray.dump(file) | Dump a pickle of the array to the specified file. |
ndarray.dumps() | Returns the pickle of the array as a string. |
ndarray.astype(dtype[, order, casting, …]) | Copy of the array, cast to a specified type. |
ndarray.byteswap(inplace) | Swap the bytes of the array elements |
ndarray.copy([order]) | Return a copy of the array. |
ndarray.view([dtype, type]) | New view of array with the same data. |
ndarray.getfield(dtype[, offset]) | Returns a field of the given array as a certain type. |
ndarray.setflags([write, align, uic]) | Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively. |
ndarray.fill(value) | Fill the array with a scalar value. |
形状操作
方法 | 描述 |
---|
ndarray.reshape(shape[, order]) | Returns an array containing the same data with a new shape. |
ndarray.resize(new_shape[, refcheck]) | Change shape and size of array in-place. |
ndarray.transpose(*axes) | Returns a view of the array with axes transposed. |
ndarray.swapaxes(axis1, axis2) | Return a view of the array with axis1 and axis2 interchanged. |
ndarray.flatten([order]) | Return a copy of the array collapsed into one dimension. |
ndarray.ravel([order]) | Return a flattened array. |
ndarray.squeeze([axis]) | Remove single-dimensional entries from the shape of a. |
元素选择和操作
方法 | 操作 |
---|
ndarray.take(indices[, axis, out, mode]) | Return an array formed from the elements of a at the given indices. |
ndarray.put(indices, values[, mode]) | Set a.flat[n] = values[n] for all n in indices. |
ndarray.repeat(repeats[, axis]) | 复制数组里面的值 |
ndarray.choose(choices[, out, mode]) | Use an index array to construct a new array from a set of choices. |
ndarray.sort([axis, kind, order]) | Sort an array, in-place. |
ndarray.argsort([axis, kind, order]) | Returns the indices that would sort this array. |
ndarray.partition(kth[, axis, kind, order]) | Rearranges the elements in the array in such a way that value of the element in kth position is in the position it would be in a sorted array. |
ndarray.argpartition(kth[, axis, kind, order]) | Returns the indices that would partition this array. |
ndarray.searchsorted(v[, side, sorter]) | Find indices where elements of v should be inserted in a to maintain order. |
ndarray.nonzero() | Return the indices of the elements that are non-zero. |
ndarray.compress(condition[, axis, out]) | Return selected slices of this array along given axis. |
ndarray.diagonal([offset, axis1, axis2]) | 返回对角线元素 |
计算
方法 | 描述 |
---|
ndarray.argmax([axis, out]) | 返回序列中第一个最大值的索引 |
ndarray.min([axis, out, keepdims]) | 返回最小值 |
ndarray.argmin([axis, out]) | 返回序列中第一个最小值的索引 |
ndarray.ptp([axis, out]) | Peak to peak (maximum - minimum) 最大值减最小值,幅度 |
ndarray.clip([min, max, out]) | Return an array whose values are limited to [min, max]. |
ndarray.conj() | Complex-conjugate all elements. |
ndarray.round([decimals, out]) | Return a with each element rounded to the given number of decimals. |
ndarray.trace([offset, axis1, axis2, dtype, out]) | Return the sum along diagonals of the array. |
ndarray.sum([axis, dtype, out, keepdims]) | Return the sum of the array elements over the given axis. |
ndarray.cumsum([axis, dtype, out]) | Return the cumulative sum of the elements along the given axis. |
ndarray.mean([axis, dtype, out, keepdims]) | Returns the average of the array elements along given axis. |
ndarray.var([axis, dtype, out, ddof, keepdims]) | Returns the variance of the array elements, along given axis. |
ndarray.std([axis, dtype, out, ddof, keepdims]) | Returns the standard deviation of the array elements along given axis. |
ndarray.prod([axis, dtype, out, keepdims]) | Return the product of the array elements over the given axis |
ndarray.cumprod([axis, dtype, out]) | Return the cumulative product of the elements along the given axis. |
ndarray.all([axis, out, keepdims]) | Returns True if all elements evaluate to True. |
ndarray.any([axis, out, keepdims]) | Returns True if any of the elements of a evaluate to True. |