NumPy是Python語言的一個擴充程序庫。支持高級大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫。Numpy內部解除了Python的PIL(全局解釋器鎖),運算效率極好,是大量機器學習框架的基礎庫!
簡單理解:
2維是EXCEL表格里面的多行多列
3維是EXCEL表格里面的多行多列+下面的sheet1、2、3
4維是包括了同一個文件夾下不同名稱的EXCEL表格
5維是同一分區不同文件夾下不同名稱的EXCEL表格
6維是不同分區不同文件夾下不同名稱的EXCEL表格
多維數組非常像列表,但通常它的元素類型是相同的,且都是數字,下面是一個簡單的例子。
聲明數組:
import numpy as np a = np.array([1, 2, 3, 4, 5, 6]) print(f'一維數組: {a}')
結果:
a = np.array([[1, 2, 3], [4, 5, 6]]) print(f'二維數組: {a}')
結果:
# 使用reshape方法反向生成多維數組 三維數組 nlist_3 = np.array(range(24)).reshape((3,2,4)) print(nlist_3)
結果:
#反向聲明一個size為20個元素的四維數組 nlist_4 = np.array(range(20)).reshape((1,2,5,2)) print(nlist_4)
結果:
數組操作:
print(f'維度(axes or dimensions)): {a.ndim}')
結果:
print(f'每個維度長度: {a.shape}')
結果:
print(f'數組長度: {a.size}')
結果:
print(f'數組類型: {type(a)}')
結果:
print(f'數組元素類型: {a.dtype}')
結果:
print(f'數組元素大小(bytes): {a.itemsize}')
結果:
print(f'數組元素: {a.data}')
結果:
#data屬性,用來打印數據緩沖區 buffer print(a.data)
結果:

#使用浮點作為元素類型 nlist_flaot = np.array([1.0,2.0,3.0]) print(nlist_flaot.dtype)
結果:
#使用字符串作為元素類型
nlist_string = np.array(['a','b','c']) print(nlist_string.dtype)
結果:
#自動生成使用ones方法,自動生成元素為1的多維數組 nlist_ones = np.ones((4,4)) print(nlist_ones) print(nlist_ones.dtype)
結果:
#使用zeros來生成元素為0的多維數組 nlist_zeros = np.zeros((4,4)) print(nlist_zeros) print(nlist_zeros.dtype)
結果:
#使用empty方法來生成隨機多維數組 使用第二個參數指定數據類型 不指定為float64 nlist_empty = np.empty([2,2],dtype=np.int) print(nlist_empty) print(nlist_empty.dtype)
結果:
#把普通list轉換成數組 x = [1,2,3] x = [[1,2,3],[4,5]] print(type(x)) nlist = np.asarray(x) print(nlist) print(nlist.ndim) print(nlist.shape) print(type(nlist))
結果:
#把普通list轉換成數組 二維數組 x = [1,2,3] x = [[1,2,3],[4,5,6]] print(type(x)) nlist = np.asarray(x) print(nlist) print(nlist.ndim) print(nlist.shape) print(type(nlist))
結果:
#frombuffer 通過字符串(buffer內存地址)切片來生成多維數組 b指定字節 my_str = b'Hello Word' nlist_str = np.frombuffer(my_str,dtype='S1') print(nlist_str)
結果:
x = np.array([[1,2],[3,4]]) print(x) #指定axis屬性可以指定當前多維數組的維度 keepdims=True讓其維度不變 sum0 = np.sum(x,axis=0,keepdims=True) print(sum0) print('--------------') sum1 = np.sum(x,axis=1,keepdims=True) print(sum1)
結果:
#多維數組賦值 x = np.array([1,2]) y = x.copy() y[0] = 3 print(x)
結果:
#維度級的運算 a = np.array([[1,2],[3,4],[5,6]]) b = np.array([[10,20],[30,40],[50,60]]) #vstack方法 suma = np.vstack((a,b)) print(suma) #hstack方法 sumb = np.hstack((a,b)) print(sumb)
結果:
#多維數組調用 nlist = np.array([[1,2],[3,4],[5,6]]) print(nlist[0]) #取元素4 print(nlist[1][1]) #第二種寫法 print(nlist[1,1]) nlist[2,1] = 7 print(nlist)
結果:
#刪除方法 delete #刪除nlist第二行 print(np.delete(nlist,1,axis=0)) print(np.delete(nlist,0,axis=1))
結果:
簡單的計算及數組值交換操作
import numpy as np
q1 = np.zeros(shape=10)
#給第五個元素賦值
q1[4] = 1
# print(q1)
# 創建一個每一行都是0到4的5*5矩陣
q2_list = [0,1,2,3,4]
# 使用list乘法反推矩陣
q2 = np.array(q2_list * 5).reshape(5,5)
# print(q2)
q3 = np.array([[1,2,3],[4,5,6],[7,8,9]])
#使用所引交換元素
# 換行
q3 = q3[[2,1,0]]
q3 = q3[[2][0]]
print(q3)
#原數組為一維數組,內容為從 0 到 100,抽取出所有偶數。
q4 = np.array(range(101))
#判斷偶數
q4 = q4[q4 % 2 == 0]
print(q4)
import numpy as np q1 = np.zeros(shape=10) #給第五個元素賦值 q1[4] = 1 # print(q1) # 創建一個每一行都是0到4的5*5矩陣 q2_list = [0,1,2,3,4] # 使用list乘法反推矩陣 q2 = np.array(q2_list * 5).reshape(5,5) # print(q2) q3 = np.array([[1,2,3],[4,5,6],[7,8,9]]) #使用所引交換元素 # 換行 q3 = q3[[2,1,0]]print(q3) #原數組為一維數組,內容為從 0 到 100,抽取出所有偶數。 q4 = np.array(range(101)) #判斷偶數 q4 = q4[q4 % 2 == 0] print(q4)