20221130numpy
numpy 练习
-
1.打印当前numpy版本
import numpy as np
print(np.__version__)
1.20.1
-
构造一个全零的矩阵,并打印其所占内存的大小
a = np.zeros([3,3])
a
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
print('%d bytes'%(a.size * a.itemsize))
72 bytes
-
打印一个函数的帮助文档,步入numpy.add
print(help(np.add))
Help on ufunc object:
add = class ufunc(builtins.object)
| Functions that operate element by element on whole arrays.
|
| To see the documentation for a specific ufunc, use `info`. For
| example, ``np.info(np.sin)``. Because ufuncs are written in C
| (for speed) and linked into Python with NumPy's ufunc facility,
| Python's help() function finds this page whenever help() is called
| on a ufunc.
|
| A detailed explanation of ufuncs can be found in the docs for :ref:`ufuncs`.
|
| **Calling ufuncs:** ``op(*x[, out], where=True, **kwargs)``
|
| Apply `op` to the arguments `*x` elementwise, broadcasting the arguments.
|
| The broadcasting rules are:
|
| * Dimensions of length 1 may be prepended to either array.
| * Arrays may be repeated along dimensions of length 1.
|
| Parameters
| ----------
| *x : array_like
| Input arrays.
| out : ndarray, None, or tuple of ndarray and None, optional
| Alternate array object(s) in which to put the result; if provided, it
| must have a shape that the inputs broadcast to. A tuple of arrays
| (possible only as a keyword argument) must have length equal to the
| number of outputs; use None for uninitialized outputs to be
| allocated by the ufunc.
| where : array_like, optional
| This condition is broadcast over the input. At locations where the
| condition is True, the `out` array will be set to the ufunc result.
| Elsewhere, the `out` array will retain its original value.
| Note that if an uninitialized `out` array is created via the default
| ``out=None``, locations within it where the condition is False will
| remain uninitialized.
| **kwargs
| For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`.
|
| Returns
| -------
| r : ndarray or tuple of ndarray
| `r` will have the shape that the arrays in `x` broadcast to; if `out` is
| provided, it will be returned. If not, `r` will be allocated and
| may contain uninitialized values. If the function has more than one
| output, then the result will be a tuple of arrays.
|
| Methods defined here:
|
| __call__(self, /, *args, **kwargs)
| Call self as a function.
|
| __repr__(self, /)
| Return repr(self).
|
| __str__(self, /)
| Return str(self).
|
| accumulate(...)
| accumulate(array, axis=0, dtype=None, out=None)
|
| Accumulate the result of applying the operator to all elements.
|
| For a one-dimensional array, accumulate produces results equivalent to::
|
| r = np.empty(len(A))
| t = op.identity # op = the ufunc being applied to A's elements
| for i in range(len(A)):
| t = op(t, A[i])
| r[i] = t
| return r
|
| For example, add.accumulate() is equivalent to np.cumsum().
|
| For a multi-dimensional array, accumulate is applied along only one
| axis (axis zero by default; see Examples below) so repeated use is
| necessary if one wants to accumulate over multiple axes.
|
| Parameters
| ----------
| array : array_like
| The array to act on.
| axis : int, optional
| The axis along which to apply the accumulation; default is zero.
| dtype : data-type code, optional
| The data-type used to represent the intermediate results. Defaults
| to the data-type of the output array if such is provided, or the
| the data-type of the input array if no output array is provided.
| out : ndarray, None, or tuple of ndarray and None, optional
| A location into which the result is stored. If not provided or None,
| a freshly-allocated array is returned. For consistency with
| ``ufunc.__call__``, if given as a keyword, this may be wrapped in a
| 1-element tuple.
|
| .. versionchanged:: 1.13.0
| Tuples are allowed for keyword argument.
|
| Returns
| -------
| r : ndarray
| The accumulated values. If `out` was supplied, `r` is a reference to
| `out`.
|
| Examples
| --------
| 1-D array examples:
|
| >>> np.add.accumulate([2, 3, 5])
| array([ 2, 5, 10])
| >>> np.multiply.accumulate([2, 3, 5])
| array([ 2, 6, 30])
|
| 2-D array examples:
|
| >>> I = np.eye(2)
| >>> I
| array([[1., 0.],
| [0., 1.]])
|
| Accumulate along axis 0 (rows), down columns:
|
| >>> np.add.accumulate(I, 0)
| array([[1., 0.],
| [1., 1.]])
| >>> np.add.accumulate(I) # no axis specified = axis zero
| array([[1., 0.],
| [1., 1.]])
|
| Accumulate along axis 1 (columns), through rows:
|
| >>> np.add.accumulate(I, 1)
| array([[1., 1.],
| [0., 1.]])
|
| at(...)
| at(a, indices, b=None, /)
|
| Performs unbuffered in place operation on operand 'a' for elements
| specified by 'indices'. For addition ufunc, this method is equivalent to
| ``a[indices] += b``, except that results are accumulated for elements that
| are indexed more than once. For example, ``a[[0,0]] += 1`` will only
| increment the first element once because of buffering, whereas
| ``add.at(a, [0,0], 1)`` will increment the first element twice.
|
| .. versionadded:: 1.8.0
|
| Parameters
| ----------
| a : array_like
| The array to perform in place operation on.
| indices : array_like or tuple
| Array like index object or slice object for indexing into first
| operand. If first operand has multiple dimensions, indices can be a
| tuple of array like index objects or slice objects.
| b : array_like
| Second operand for ufuncs requiring two operands. Operand must be
| broadcastable over first operand after indexing or slicing.
|
| Examples
| --------
| Set items 0 and 1 to their negative values:
|
| >>> a = np.array([1, 2, 3, 4])
| >>> np.negative.at(a, [0, 1])
| >>> a
| array([-1, -2, 3, 4])
|
| Increment items 0 and 1, and increment item 2 twice:
|
| >>> a = np.array([1, 2, 3, 4])
| >>> np.add.at(a, [0, 1, 2, 2], 1)
| >>> a
| array([2, 3, 5, 4])
|
| Add items 0 and 1 in first array to second array,
| and store results in first array:
|
| >>> a = np.array([1, 2, 3, 4])
| >>> b = np.array([1, 2])
| >>> np.add.at(a, [0, 1], b)
| >>> a
| array([2, 4, 3, 4])
|
| outer(...)
| outer(A, B, /, **kwargs)
|
| Apply the ufunc `op` to all pairs (a, b) with a in `A` and b in `B`.
|
| Let ``M = A.ndim``, ``N = B.ndim``. Then the result, `C`, of
| ``op.outer(A, B)`` is an array of dimension M + N such that:
|
| .. math:: C[i_0, ..., i_{M-1}, j_0, ..., j_{N-1}] =
| op(A[i_0, ..., i_{M-1}], B[j_0, ..., j_{N-1}])
|
| For `A` and `B` one-dimensional, this is equivalent to::
|
| r = empty(len(A),len(B))
| for i in range(len(A)):
| for j in range(len(B)):
| r[i,j] = op(A[i], B[j]) # op = ufunc in question
|
| Parameters
| ----------
| A : array_like
| First array
| B : array_like
| Second array
| kwargs : any
| Arguments to pass on to the ufunc. Typically `dtype` or `out`.
|
| Returns
| -------
| r : ndarray
| Output array
|
| See Also
| --------
| numpy.outer : A less powerful version of ``np.multiply.outer``
| that `ravel`\ s all inputs to 1D. This exists
| primarily for compatibility with old code.
|
| tensordot : ``np.tensordot(a, b, axes=((), ()))`` and
| ``np.multiply.outer(a, b)`` behave same for all
| dimensions of a and b.
|
| Examples
| --------
| >>> np.multiply.outer([1, 2, 3], [4, 5, 6])
| array([[ 4, 5, 6],
| [ 8, 10, 12],
| [12, 15, 18]])
|
| A multi-dimensional example:
|
| >>> A = np.array([[1, 2, 3], [4, 5, 6]])
| >>> A.shape
| (2, 3)
| >>> B = np.array([[1, 2, 3, 4]])
| >>> B.shape
| (1, 4)
| >>> C = np.multiply.outer(A, B)
