numpy.zeros_like()函数
这个numpy方法返回给定形状和类型的数组,即给定数组,并带有0。
语法:
numpy.zeros_like(array, dtype = None, order = 'K', subok = True)
参数:
array : array_like 输入
subok : [optional, boolean]如果为真,则新创建的array将是array的子类;否则,为基类数组
order : C_contiguous 或 F_contiguous
C-contiguous order in memory(last index varies the fastest)
C order 意味着对数组进行行上升操作将会稍微快一些
FORTRAN-contiguous order in memory (first index varies the fastest).
F order 意味着按列操作将更快。
dtype : [optional, float(byDefault)] 返回数组的数据类型。
返回值:
一组具有给定形状、顺序和数据类型的零。
示例1
# Python Programming illustrating
# numpy.zeros_like method
import numpy as geek
array = geek.arange(10).reshape(5, 2)
print("Original array : \n", array)
b = geek.zeros_like(array, float)
print("\nMatrix b : \n", b)
array = geek.arange(8)
c = geek.zeros_like(array)
print("\nMatrix c : \n", c)
输出:
Original array :
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
Matrix b :
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
Matrix c :
[0 0 0 0 0 0 0 0]
示例2
# Python Programming illustrating
# numpy.zeros_like method
import numpy as geek
array = geek.arange(10).reshape(5, 2)
print("Original array : \n", array)
array = geek.arange(4).reshape(2, 2)
c = geek.zeros_like(array, dtype = 'float')
print("\nMatrix : \n", c)
array = geek.arange(8)
c = geek.zeros_like(array, dtype = 'float', order ='C')
print("\nMatrix : \n", c)
输出:
Original array :
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
Matrix :
[[ 0. 0.]
[ 0. 0.]]
Matrix :
[ 0. 0. 0. 0. 0. 0. 0. 0.]