numpy重新学习系列(5)---如何用np.zeros_like创建一个新的和原来array形状一样的,但是元素为0的新的array

'''
numpy.zeros_like

numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)[source]
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.
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.
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.
       New in version 1.17.0.
Returns
out   ndarray
      Array of zeros with the same shape and type as a.
'''

 

# np.zeros_like的作用
# 很明显,使用np.zeros_like是想要创造一个和目标的array一样的形状结构,但是元素是0的新的array
# 五个参数,一个必须的参数,四个可选的参数
# 第一个参数:a
# 这是一个array数据类型,想要返回和这个array一样的形状、类型的array,即目标array

# 第二个参数:dtype
# 数据类型:重新改变目标array的元素里面的数据类型

# 第三个参数:order
#  这个参数可以改变结果的内存分布。C代表着C语言类型的,F代表Fortan语言类型,
# A代表着原来如果是F连接类型,就还是F,否则就是C类型;K代表着返回和a尽可能相似的结果

# 第四个参数:subok
# 默认是真的。如果是真的,返回的新的array是目标array的一个子类;否则就是基础类。

# 第五个参数:shape
#  改写结果的形状。如果order="K",并且维数没有改变,就保持原来的;否则,order就假定为“C”

# 这就是一个必要参数和四个可选参数的介绍。    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章