Ndaray對象
1、內(nèi)部組成:一個指向數(shù)據(jù)的指針;存放數(shù)據(jù)的格子;表示數(shù)據(jù)各維度大小元組;一個跨度元組。
2、創(chuàng)建:numpy.array(numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
object:數(shù)組或嵌套的數(shù)列
dtype:數(shù)組元素的數(shù)據(jù)類型,可選
copy:對象是否需要復(fù)制,可選
order:創(chuàng)建數(shù)組的樣式,C為行方向,F(xiàn)為列方向,A為任意方向(默認)
subok:默認返回一個與基類類型一致的數(shù)組
ndmin:指定生成數(shù)組的最小維度
創(chuàng)建數(shù)組
1、創(chuàng)建一個指定形狀(shape)、數(shù)據(jù)類型(dtype)且未初始化的數(shù)組:
numpy.empty(shape, dtype = float, order = 'C')
注:數(shù)組元素為隨機值
2、創(chuàng)建指定大小的數(shù)組,數(shù)組元素以0填充:
numpy.zeros(shape, dtype = float, order = 'C')
3、創(chuàng)建指定形狀的數(shù)組,數(shù)組元素以 1 來填充:
numpy.ones(shape, dtype = None, order = 'C')
從已有的數(shù)組創(chuàng)建數(shù)組
1、numpy.asarray
numpy.asarray(a, dtype = None, order = None)
2、numpy.frombuffer
numpy.frombuffer 用于實現(xiàn)動態(tài)數(shù)組。
numpy.frombuffer 接受 buffer 輸入?yún)?shù),以流的形式讀入轉(zhuǎn)化成 ndarray 對象。
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
注意:buffer 是字符串的時候,Python3 默認 str 是 Unicode 類型,所以要轉(zhuǎn)成 bytestring 在原 str 前加上 b。
buffer:可以是任意對象,會以流的形式讀入。
dtype:返回數(shù)組的數(shù)據(jù)類型,可選
count:讀取的數(shù)據(jù)數(shù)量,默認為-1,讀取所有數(shù)據(jù)。
offset:讀取的起始位置,默認為0。
3、numpy.fromiter:方法從可迭代對象中建立 ndarray 對象,返回一維數(shù)組。
numpy.fromiter(iterable, dtype, count=-1)
iterable可迭代對象
dtype返回數(shù)組的數(shù)據(jù)類型
count讀取的數(shù)據(jù)數(shù)量,默認為-1,讀取所有數(shù)據(jù)
廣播
numpy.tile()
所有輸入的元素都向其中長度最長的元素看齊,不足的部分通常加1補充
輸出數(shù)組形狀是輸入數(shù)組形狀維度的最大值