1. 程式人生 > >TensorFlow常用庫NumPy

TensorFlow常用庫NumPy

官網教程:https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

首先要注意一句話:NumPy’s main object is the homogeneous multidimensional array

翻譯過來就是,numpy庫的主要物件是多維陣列,0、1、2、3.....

特徵:

ndarray.ndim:該物件的維度

ndarray.shape:形狀,主要面對的是矩陣之類的,例如有一個矩陣 2行3列,則shape就是(2,)

ndarray.size:所有元素的數目

ndarray.dtype:元素的資料型別,例如int32,int64這種

ndarray.itemsize:元素位的大小,假如是64位的float,則itemsize=64/8=8

ndarray.data:緩衝池中包含的陣列元素(不常用)

使用:

import numpy as np
vector = np.array([1,2,3])
vertor.shape
#輸出 (3,)
vector.size
#輸出 3
vector.ndim
#輸出 1
type(vector)
#輸出 <type 'numpy.ndarray'>

#建立多維陣列
matrix = np.array([1,2],[3,4])
matrix.shape
#輸出:(2,2)
matrix.ndim
#輸出:2
matrix.size
#輸出:4
type(matrix)
#<type 'numpy.ndarray'>

#改變矩陣的形態
one = np.arange(12)
two = one.reshape((3,4))
#首先建立一個1行12列的矩陣(1維陣列)
#通過reshape可以把他改為 3行4列的矩陣