文章目錄
- 資料分析.numpy.數組的建立、形狀、計算基礎
- 一、數組的建立
- 二、數組的形狀
- 2.1 shape --- 檢視
- 2.2 reshape()--- 更改
- 2.3 數組展平與恢複
- 三、數組的計算(以二維數組為例)
- 3.1 數組與數字計算
- 3.2 數組與數組計算
- 四、說明
資料分析.numpy.數組的建立、形狀、計算基礎
一、數組的建立
在python中,numpy(Numerical Python)提供了python對多元數組對象的支援:ndarray,具有矢量運算能力,快速、節省空間。numpy支援進階大量的次元數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫。
建立ndarray數組函數:
1.ndarray:N維數組對象(矩陣),所有元素必須是相同類型。
2.ndarray屬性:
ndim 次元個數;
shape 各次元大小(數組的形狀);
dtype 資料類型;
size 數組的大小,數組内元素總個數;
itemsize 數組内的元素占計算機的記憶體大小;
data 數組在記憶體中的位址,一般通過索引操作元素。
3.常用方法:
t102.astype() # 調整資料類型
np.round() # 修改數組小數位
array與arange的差別
import numpy as np
import random
t1 = np.array([1,2,3,4])
print(t1) #[1 2 3 4]
print(type(t1)) #該數組的類型 <class 'numpy.ndarray'>
print(t1.dtype) #該數組内元素的資料類型 int32 (32/64位)
t101 = np.array([1,2,3,4],dtype=float) #添加dtype屬性,手動賦資料類型
print(t101) #[1. 2. 3. 4.]
print(t101.dtype) #float64
t102 = np.array([1,0,1,1,0],dtype=bool)
print(t102) #[ True False True True False]
print(t102.dtype) #bool
t103 = t102.astype("int8") #調整資料類型
print(t103) #[1 0 1 1 0]
print(t103.dtype) #int8
#arange生成的效果與array相似,可以快速的生成數組
t2 = np.array(range(10))
print(t2) #[0 1 2 3 4 5 6 7 8 9]
t3 = np.arange(10)
print(t3) #[0 1 2 3 4 5 6 7 8 9]
t4 = np.arange(1,10,2) #從1到10,步長為2
print(t4) #[1 3 5 7 9]
#numpy中取小數
t5 = np.array([random.random() for i in range(10)])
print(t5) #[0.43902813 0.3838124 0.57498559 0.83616761 0.71580552 0.3064713 0.47795043 0.17326671 0.38841184 0.7184412 ]
print(t5.dtype) #float64
#修改小數位
t6 = np.round(t5,2)
print(t6) #[0.64 0.72 0.73 0.63 0.52 0.53 0.75 0.84 0.48 0.11]
t7 = np.round([random.random()],3)
print(t7) #[0.825]
傳回頂部
二、數組的形狀
t1.reshape() # 修改數組t1的形狀(次元) 元素數與原次元的元素數必須一緻!!!
2.1 shape — 檢視
#一維數組
t1 = np.arange(12)
print(t1) #[ 0 1 2 3 4 5 6 7 8 9 10 11]
print(t1.shape) #(12,) 表示該一維數組中的元素個數
#二維數組
t2 = np.array([[1,2,3],[4,5,6]])
print(t2) #[[1 2 3]
# [4 5 6]]
print(t2.shape) #(2, 3) 表示2行3列
#三維數組
t3 = t1.reshape((2,2,3))
print(t3)
# [[[ 0 1 2]
# [ 3 4 5]]
#
# [[ 6 7 8]
# [ 9 10 11]]]
print(t3.shape) #(2,2,3)
傳回頂部
2.2 reshape()— 更改
import numpy as np
#修改數組形狀
#低維到高維
#二維
t4 = t1.reshape((3,4))
print(t4) #[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(t4.shape) #(3, 4)
#三維 假設定義24個資料
t5 = np.arange(24).reshape((2,3,4))
print(t5)
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
print(t4.shape) #(2, 3, 4) 2塊,每塊3行4列.
傳回頂部
2.3 數組展平與恢複
#高維到低維
t7 = np.arange(24).reshape((2,4,3)) #t7是一個三維數組
t8 = t7.reshape((24,))
print(t8) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t9 = t7.reshape((t7.size,))
print(t9) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t10 = t7.flatten() #直接展開
print(t10) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t100 = t7.ravel()
print(t100)# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
#如果是二維轉一維,還可以用(行數*列數)計算元素個數
#對t5修改形狀
t6 = t5.reshape((4,6))
print(t6)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
print(t5) #盡管通過t6對t5進行修改,但是t5本身不會變,它還是一個三維數組
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
t5 = t5.reshape((4,6)) #但是如果對t5本身進行修改,那麼将改變t5原有的樣式
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
傳回頂部
三、數組的計算(以二維數組為例)
廣播機制:可以應用到每一個元素上
3.1 數組與數字計算
import numpy as np
t1 = np.arange(24)
#二維數組計算
t2 = t1.reshape((4,6))
print(t2)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t3 = t2+2 #加
print(t2)
# [[ 2 3 4 5 6 7]
# [ 8 9 10 11 12 13]
# [14 15 16 17 18 19]
# [20 21 22 23 24 25]]
t4 = t2-2 #減
print(t4)
# [[-2 -1 0 1 2 3]
# [ 4 5 6 7 8 9]
# [10 11 12 13 14 15]
# [16 17 18 19 20 21]]
t5 = t2*2 #乘
print(t5)
# [[ 0 2 4 6 8 10]
# [12 14 16 18 20 22]
# [24 26 28 30 32 34]
# [36 38 40 42 44 46]]
t6 = t2/2 #除
print(t6)
# [[ 0. 0.5 1. 1.5 2. 2.5]
# [ 3. 3.5 4. 4.5 5. 5.5]
# [ 6. 6.5 7. 7.5 8. 8.5]
# [ 9. 9.5 10. 10.5 11. 11.5]]
說明:
t6 = t2/0 #除
print(t6)
#[[nan inf inf inf inf inf]
# [inf inf inf inf inf inf]
# [inf inf inf inf inf inf]
# [inf inf inf inf inf inf]]
當被除數為0時,按照正常的是不可以的,系統也會出現警告:
同時會出現以上注釋裡的結果,這裡強調一下下:
nan ---> not an number
inf --->infinity(無窮大)
傳回頂部
3.2 數組與數組計算
相同形狀數組與數組計算,對應位置互相計算!!!
t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t6 = np.arange(100,124).reshape(4,6)
print(t6)
# [[100 101 102 103 104 105]
# [106 107 108 109 110 111]
# [112 113 114 115 116 117]
# [118 119 120 121 122 123]]
print(t5+t6) #加
# [[100 102 104 106 108 110]
# [112 114 116 118 120 122]
# [124 126 128 130 132 134]
# [136 138 140 142 144 146]]
print(t5-t6) #減
# [[-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]]
print(t5*t6) #乘
# [[ 0 101 204 309 416 525]
# [ 636 749 864 981 1100 1221]
# [1344 1469 1596 1725 1856 1989]
# [2124 2261 2400 2541 2684 2829]]
print(t5/t6) #除
# [[0. 0.00990099 0.01960784 0.02912621 0.03846154 0.04761905]
# [0.05660377 0.06542056 0.07407407 0.08256881 0.09090909 0.0990991 ]
# [0.10714286 0.11504425 0.12280702 0.13043478 0.13793103 0.14529915]
# [0.15254237 0.15966387 0.16666667 0.17355372 0.18032787 0.18699187]]
相同列數,不同行數,進行每一行的計算
t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t7 = np.arange(6)
print(t7)
#[0 1 2 3 4 5]
print(t5+t7) #加
# [[ 0 2 4 6 8 10]
# [ 6 8 10 12 14 16]
# [12 14 16 18 20 22]
# [18 20 22 24 26 28]]
print(t5-t7) #減
# [[ 0 0 0 0 0 0]
# [ 6 6 6 6 6 6]
# [12 12 12 12 12 12]
# [18 18 18 18 18 18]]
print(t5*t7) #乘
# [[ 0 1 4 9 16 25]
# [ 0 7 16 27 40 55]
# [ 0 13 28 45 64 85]
# [ 0 19 40 63 88 115]]
print(t5/t7) #除
# [[ nan 1. 1. 1. 1. 1. ]
# [ inf 7. 4. 3. 2.5 2.2]
# [ inf 13. 7. 5. 4. 3.4]
# [ inf 19. 10. 7. 5.5 4.6]]
相同行數,不同列數,進行每一列的計算
t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t8 = np.arange(4).reshape(4,1)
print(t8)
# [[0]
# [1]
# [2]
# [3]]
print(t5+t8) #加
# [[ 0 1 2 3 4 5]
# [ 7 8 9 10 11 12]
# [14 15 16 17 18 19]
# [21 22 23 24 25 26]]
print(t5-t8) #減
# [[ 0 1 2 3 4 5]
# [ 5 6 7 8 9 10]
# [10 11 12 13 14 15]
# [15 16 17 18 19 20]]
print(t5*t8) #乘
# [[ 0 0 0 0 0 0]
# [ 6 7 8 9 10 11]
# [24 26 28 30 32 34]
# [54 57 60 63 66 69]]
print(t5/t8) #除
# [[ nan inf inf inf inf inf]
# [ 6. 7. 8. 9. 10. 11. ]
# [ 6. 6.5 7. 7.5 8. 8.5 ]
# [ 6. 6.33333333 6.66666667 7. 7.33333333 7.66666667]]
行數、列數都不一樣時,不可計算!!!
t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t9 = np.arange(6).reshape(2,3)
print(t9)
# [[0 1 2]
# [3 4 5]]
print(t5+t9) #加
print(t5-t9) #減
print(t5*t9) #乘
print(t5/t9) #除
報錯:
ValueError: operands could not be broadcast together with shapes (4,6) (2,3)
傳回頂部
四、說明
1.廣播原則:
如果兩個數組的後緣次元(從末尾開始算起的次元)的軸長度相符或其中一方的長度為1,則認為它們是廣播相容的。廣播會在缺失和(或)長度為1的次元上進行。
2.軸:
軸在numpy中可以了解為方向,使用數字0,1,2…表示,對于一個一維數組隻有一個0軸,對于二維數組shape(2,2),有0軸和1軸,對于三維數組shape(2,2,3),有0,1,2軸。