numpy.zeros_like
Возвращает массив нулей с той же формой и типом,что и заданный массив.
Parameters aarray_like
Форма и тип данных a определяют те же атрибуты возвращаемого массива.
dtypedata-type, optional
Переопределяет тип данных результата.
Новинка в версии 1.6.0.
Переопределяет расположение памяти результата. «C» означает C-порядок, «F» означает F-порядок, «A» означает «F», если a является непрерывным на Фортране, «C» в противном случае. «К» означает максимально точное a макету .
Новинка в версии 1.6.0.
Если True, то вновь созданный массив будет использовать тип подкласса a , в противном случае это будет массив базового класса. По умолчанию True.
shape int или последовательность целых чисел, необязательно.
Переопределяет форму результата.Если порядок=’K’ и число измерений не меняется,то будет сделана попытка сохранить порядок,в противном случае подразумевается порядок=’C’.
NumPy: numpy.zeros_like() function
The numpy.zeros_like() function takes an array_like object as input and returns an array of the same shape, size, and dtype with all elements set to zero.
This function is useful when you want to create an array of zeros with the same shape and type as another array without explicitly specifying the shape and data type. It can also be used to initialize a new array with the same shape as an existing array but with all elements set to zero.
Syntax:
Parameters:
| Name | Description | Required / Optional |
|---|---|---|
| a | The shape and data-type of a define these same attributes of the returned array. | Required |
| dtype | Overrides the data type of the result. New in version 1.6.0. | optional |
| order | 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. New in version 1.6.0. | optional |
| subok | 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. | optional |
Return value:
[ndarray] Array of zeros with the same shape and type as a.
Example: Creating an array of zeros with the same shape and data type as a given array using numpy.zeros_like()
In the above code an array a is created using np.arange(4) and is reshaped into a 2×2 array using .reshape((2,2)). Next, numpy.zeros_like(a) is called, which returns an array of zeros with the same shape and data type as a. This means that the returned array will also be a 2×2 array of integers filled with zeros.
Pictorial Presentation:
Example: Creating an array of zeros with the same shape and data type as an existing array
Here the code creates a one-dimensional NumPy array b containing float values from 0 to 4 using the arange() function. Then, the zeros_like() function creates a new NumPy array of zeros with the same shape and data type as b. Since b is a one-dimensional array of length 5, zeros_like(b) also creates a one-dimensional array of the same length, containing all zeros and having the same data type (float).
Pictorial Presentation:
Python — NumPy Code Editor:
Previous: zeros()
Next: full()
Follow us on Facebook and Twitter for latest update.
- Weekly Trends
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
numpy.zeros_like#
Return an array of zeros 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.
New in version 1.6.0.
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.
New in version 1.6.0.
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.
shape int or sequence of ints, optional.
Overrides the shape of the result. If order=’K’ and the number of dimensions is unchanged, will try to keep order, otherwise, order=’C’ is implied.
numpy.zeros_like¶
Return an array of zeros with the same shape and type as a given array.
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.
New in version 1.6.0.
order : <‘C’, ‘F’, ‘A’, or ‘K’>, optional
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.
New in version 1.6.0.
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.
out : ndarray
Array of zeros with the same shape and type as a.
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.