Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import numpypy as N
- except:
- import numpy as N
- def empty_like(a, dtype=None, order='K', subok=True):
- '''
- Return a new array with the same shape and type as a given array.
- Parameters
- ----------
- a : array_like
- The shape and data-type of `a` define these same attributes of the
- returned array.
- dtype : data-type, optional
- Overrides the data type of the result.
- order : {'C', 'F', 'A', or 'K'}, optional
- Added for compatibility with cpython numpy, currently ignored.
- In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
- 'F' means F-order, 'A' means 'F' if ``a`` is Fortran contiguous,
- 'C' otherwise. 'K' means match the layout of ``a`` as closely
- as possible.
- subok : bool, optional.
- If True, then the newly created array will use the sub-class
- type of 'a', otherwise it will be a base-class array. Defaults
- to True.
- Returns
- -------
- out : ndarray
- Array of uninitialized (arbitrary) data with the same
- shape and type as `a`.
- See Also
- --------
- ones_like : Return an array of ones with shape and type of input.
- zeros_like : Return an array of zeros with shape and type of input.
- empty : Return a new uninitialized array.
- ones : Return a new array setting values to one.
- zeros : Return a new array setting values to zero.
- Notes
- -----
- This function does *not* initialize the returned array; to do that use
- `zeros_like` or `ones_like` instead. It may be marginally faster than
- the functions that do set the array values.
- Examples
- --------
- >>> a = ([1,2,3], [4,5,6]) # a is array-like
- >>> np.empty_like(a)
- array([[-1073741821, -1073741821, 3], #random
- [ 0, 0, -1073741821]])
- >>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
- >>> np.empty_like(a)
- array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000],#random
- [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
- '''
- if hasattr(a, 'shape') and hasattr(a, 'dtype'):
- # may be ndarray, matrix, sparse matrix or their subclass
- r = N.empty(a.shape, a.dtype if dtype is None else dtype)
- Class = a.__class__
- elif isinstance(a, (list, tuple)):
- # we can't use len(), because a may be list of lists or like that
- tmp = N.asarray(a)
- Class = tmp.__class__
- r = N.empty(tmp.shape, tmp.dtype if dtype is None else dtype)
- else:
- r = N.empty((), type(a))
- Class = a.__class__ if hasattr(a,'__class__') else object
- if subok and issubclass(Class, N.ndarray) and a.__class__ != r.__class__:
- assert 0, 'empty_like with ndarray subclass output is unimplemented yet'
- #r.__class__ = a.__class__ and other tried tricks doesn't work yet
- return r
- def zeros_like(a, dtype=None, order='K', subok=True):
- '''
- zeros_like(a, dtype=None, order='K', subok=True)
- Return an array of zeros with the same shape and type as a given array.
- With default parameters, is equivalent to ``a.copy().fill(0)``.
- Parameters
- ----------
- a : array_like
- The shape and data-type of `a` define these same attributes of
- the returned array.
- dtype : data-type, optional
- Overrides the data type of the result.
- order : {'C', 'F', 'A', or 'K'}, optional
- Added for compatibility with cpython numpy, currently ignored.
- In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
- 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
- 'C' otherwise. 'K' means match the layout of `a` as closely
- as possible.
- Returns
- -------
- out : ndarray
- Array of zeros with the same shape and type as `a`.
- See Also
- --------
- ones_like : Return an array of ones with shape and type of input.
- empty_like : Return an empty array with shape and type of input.
- zeros : Return a new array setting values to zero.
- ones : Return a new array setting values to one.
- empty : Return a new uninitialized array.
- Examples
- --------
- >>> x = np.arange(6)
- >>> x = x.reshape((2, 3))
- >>> x
- array([[0, 1, 2],
- [3, 4, 5]])
- >>> np.zeros_like(x)
- array([[0, 0, 0],
- [0, 0, 0]])
- >>> y = np.arange(3, dtype=np.float)
- >>> y
- array([ 0., 1., 2.])
- >>> np.zeros_like(y)
- array([ 0., 0., 0.])
- '''
- res = empty_like(a, dtype=dtype, order=order, subok=subok)
- res.fill(0)
- return res
- def ones_like(a, dtype=None, order='K', subok=True):
- '''
- ones_like(a, dtype=None, order='K', subok=True)
- Return an array of ones with the same shape and type as a given array.
- With default parameters, is equivalent to ``a.copy().fill(0)``.
- Parameters
- ----------
- a : array_like
- The shape and data-type of `a` define these same attributes of
- the returned array.
- dtype : data-type, optional
- Overrides the data type of the result.
- order : {'C', 'F', 'A', or 'K'}, optional
- Added for compatibility with cpython numpy, currently ignored.
- In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
- 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
- 'C' otherwise. 'K' means match the layout of `a` as closely
- as possible.
- Returns
- -------
- out : ndarray
- Array of ones with the same shape and type as `a`.
- See Also
- --------
- ones_like : Return an array of ones with shape and type of input.
- empty_like : Return an empty array with shape and type of input.
- zeros : Return a new array setting values to zero.
- ones : Return a new array setting values to one.
- empty : Return a new uninitialized array.
- Examples
- --------
- >>> x = np.arange(6)
- >>> x = x.reshape((2, 3))
- >>> x
- array([[0, 1, 2],
- [3, 4, 5]])
- >>> np.ones_like(x)
- array([[1, 1, 1],
- [1, 1, 1]])
- >>> y = np.arange(3, dtype=np.float)
- >>> y
- array([ 0., 1., 2.])
- >>> np.ones_like(y)
- array([ 1., 1., 1.])
- '''
- res = empty_like(a, dtype=dtype, order=order, subok=subok)
- res.fill(1)
- return res
- if __name__ == '__main__':
- for a in [N.array(1), N.array([1, 2, 3]), N.array([[2.0, 4.0],[3,6]]), N.ones((3, 4, 5)), N.zeros((3, 4, 5, 6))]:
- b = empty_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- b = ones_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- b = zeros_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- for a in [1, 1.0, N.int16(1), N.float32(1)]:
- b = empty_like(a)
- # b.dtype is not equal to type(a) for a=1, because b.dtype is int64 while type(a) is int
- assert b.shape == () and b.size == 1
- b = ones_like(a)
- assert b.shape == () and b.size == 1
- b = zeros_like(a)
- assert b.shape == () and b.size == 1
- print('passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement