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”

# 這就是一個必要參數和四個可選參數的介紹。    

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