[Python] np.ones_like(ndarray)和np.zeros_like(ndarray)

语法:

np.ones_like(ndarray)			#创建同shape的全1矩阵

np.zeros_like(ndarray)			#创建同shape的全0矩阵

示例:

import numpy as np

a = np.array([[1,2,3],[4,5,6]])
print(a.shape)
# (2, 3)
ones = np.ones_like(a)
print(ones)
# [[1 1 1]
#  [1 1 1]]
zeros = np.zeros_like(a)
print(zeros)
# [[0 0 0]
#  [0 0 0]]

你可能感兴趣的:(Python,Python)