numpy
import numpy as np
shop_price = [30, 20, 15, 40]
shop_num = [2, 3, 1, 4]
快捷鍵
向下添加一個單元格, 按esc進入命令行模式, 再按b 向下添加
向下添加一個單元格, 按esc進入命令行模式, 再按a 向上添加
向下添加一個單元格, 按esc進入命令行模式, 再按dd 刪除單元格
代碼模式轉成markdown模式, 按esc進入命令行模式, 按m,轉換成markdown
shift + enter: 運行當前代碼並選中下一個單元格
ctrl + enter: 運行當前代碼
一維數組
>>> np_shop_price = np.array(shop_price)
>>> np_shop_price
array([30, 20, 15, 40])
>>> np_shop_num = np.array(shop_num)
>>> np_shop_num
array([2, 3, 1, 4])
數組之間可以進行向量運算
>>> np_shop_price = np.array(shop_price)
>>> np_shop_num = np.array(shop_num)
>>> np_shop_price * np_shop_num
array([ 60, 60, 15, 160])
>>> np.sum(np_shop_price * np_shop_num)
295
向量運算必須一一運算
python中, 數組就是列表, 列表之間的運算叫向量運算, 2個列表的元素數量必須是一樣的,否則就會報錯
>>> a1 = np.array([1,2,3,4])
>>> a2 = np.array([1,2,3])
>>> a1*a2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-d8f7a8f74ee5> in <module>
----> 1 a1*a2
ValueError: operands could not be broadcast together with shapes (4,) (3,)
numpy的基本屬性
>>> a1 = np.array([1,2,3,4])
>>> a1
array([1, 2, 3, 4])
>>> a2 = np.array([[1,2,3,4],[5,6,7,8]])
>>> a2
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
- 數組元素的數據類型
>>> a1.dtype
dtype('int32')
- 數組元素的個數
>>> a1.size
4
>>> a2.size
8
- 數組的維數, 是一維數組還是二維數組
>>> a1.ndim
1
>>> a2.ndim
2
- 數組的維度大小, 以元組的形式表示
>>> a1.shape
(4,)
>>> a2.shape (行數,列數)
(2, 4)
- 數組的轉置(將二維數組以上的數組的數據結構的行和列對調)
>>> a2
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> a2.shape (行數,列數)
(2, 4)
>>> a2.T (列數, 行數)
array([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
創建numpy的幾種方式
array
將列表轉換為數組
- 創建整數類型的數組
>>> a3 = np.array([1,2,3,4])
>>> a3
array([1, 2, 3, 4])
- 創建浮點類型的數組
>>> a3 = np.array([1,2,3,4], dtype='float')
>>> a3
array([1., 2., 3., 4.])
- 將浮點類型的數組轉換為整數類型的數組
>>> a3.astype('int') 轉換數據類型為int
array([1, 2, 3, 4])
arrange
和range函數用法一樣, 在numpy中使用arrange
- 創建0, 1, 2 組成的數組
>>> np.arange(3)
array([0, 1, 2])
- 創建3到7之間的數組成的數組, 步長為2
>>> np.arange(3,7,2)
array([3, 5])
linspace
將一定范圍內的數根據數組設置的長度平均做除法得到數組內的每個元素, 默認分成50份
- 創建2到10之間的數組成的數組, 數組長度為50
>>> np.linspace(2,10)
array([ 2. , 2.16326531, 2.32653061, 2.48979592, 2.65306122,
2.81632653, 2.97959184, 3.14285714, 3.30612245, 3.46938776,
3.63265306, 3.79591837, 3.95918367, 4.12244898, 4.28571429,
4.44897959, 4.6122449 , 4.7755102 , 4.93877551, 5.10204082,
5.26530612, 5.42857143, 5.59183673, 5.75510204, 5.91836735,
6.08163265, 6.24489796, 6.40816327, 6.57142857, 6.73469388,
6.89795918, 7.06122449, 7.2244898 , 7.3877551 , 7.55102041,
7.71428571, 7.87755102, 8.04081633, 8.20408163, 8.36734694,
8.53061224, 8.69387755, 8.85714286, 9.02040816, 9.18367347,
9.34693878, 9.51020408, 9.67346939, 9.83673469, 10. ])
>>> np.linspace(2,10,num=5)
array([ 2., 4., 6., 8., 10.])
- 創建2到10之間的數, 數組長度為5, 不能包括10
>>> np.linspace(2,10,num=5,endpoint=False)
array([2. , 3.6, 5.2, 6.8, 8.4])
zeros
根據數組的長度創建全部都是0的浮點數的數組
>>> np.zeros(5)
array([0., 0., 0., 0., 0.])
ones
根據數組的長度創建全部都是1的浮點數的數組
>>> np.ones(5)
array([1., 1., 1., 1., 1.])
reshape
將一維數組轉換成多維數組
>>> a4 = np.arange(10)
>>> a4
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a4.reshape(2,5)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> a4.reshape(5,2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> a4.reshape(3,3) 報錯
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-57-88c9cc4a32ac> in <module>
----> 1 a4.reshape(3,3)
ValueError: cannot reshape array of size 10 into shape (3,3)
索引和切片
數組和常量之間的運算
>>> a4
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a5 = a4*3
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
>>> li = [1, 2, 3, 4]
>>> li*3
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> a5+3
array([ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30])
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
同等大小數組之間的運算
>>> a7 = np.array([1,2,3,4])
>>> a8 = a5 = np.array([1,2,3,4])
>>> a7*a8
array([ 5, 12, 21, 32])
數組的索引
>>> a5[2]
6
>>> a6 = a5.reshape(2,5)
>>> a6
array([[ 0, 3, 6, 9, 12],
[15, 18, 21, 24, 27]])
>>> a6[0,2]
6
>>> a6[0][2]
6
數組的切片
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
>>> a5[3:6]
array([ 9, 12, 15])
>>> a6
array([[ 0, 3, 6, 9, 12],
[15, 18, 21, 24, 27]])
>>> a6[:,2:4]
array([[ 6, 9],
[21, 24]])
布爾型索引
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
獲取數組中大於5的數
>>> a5>5
array([False, False, True, True, True, True, True, True, True,
True])
取出所有大於5的偶數
>>> a5[(a5>5) & (a5%2==0)]
>>> array([ 6, 12, 18, 24])
花式索引
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
獲取索引是2, 4, 8所對應的具體的值
>>> a5[[2,4,8]]
array([ 6, 12, 24])
通用函數
取絕對值
>>> np.abs(-2)
2
開根號
>>> np.sqrt(4)
2.0
向上取值
>>> np.ceil(3.2)
4.0
np.ceil(3.8)
4.0
四舍五入
>>> np.rint(3.2)
3.0
>>> np.rint(3.7)
4.0
小數位歸0
>>> np.trunc(1.5)
1.0
將整數和小數分開
>>> np.modf(12.22)
(0.22000000000000064, 12.0)
>>> np.modf([0,3.5])
(array([0. , 0.5]), array([0., 3.]))
判斷數據是否不是一個數字
>>> np.isnan(3)
False
取2個數組中的最大值組成一個新的數組
>>> np.maximum([2,3,4],[1,5,2])
array([2, 5, 4])
取平均值
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
>>> np.mean(a5)
13.5
取方差
((0-13.5)^2+(3-13.5^2)+...+(27-13.5)^2)/10 ===> 方差公式
>>> a5
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
>>> np.var(a5)
74.25
排序
>>> np.sort(a5)
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
隨機數生成
- 生成0到1之間的隨機數
>>> np.random.rand()
0.41127690452609
- 生成一個三行2列的0到1的隨機數的數組
>>> np.random.rand(3,2)
array([[0.25495748, 0.4505048 ],
[0.70361618, 0.37524951],
[0.19785526, 0.3860836 ]])
- 生成0到2以內的10個整數的數組
>>> np.random.randint(2,size=10)
array([1, 1, 0, 1, 1, 0, 0, 0, 0, 1])
- 生成2到5以內的10個整數的數組
>>> np.random.randint(2,5,size=10)
array([2, 4, 4, 2, 4, 4, 3, 4, 2, 3])
- 生成5以內的3個整數組成的數組
>>> np.random.choice(5,3)
array([4, 2, 4])
- 給定形狀產生隨機數組
>>> np.random.uniform(-1,0,10)
array([-1.86027339e-01, -5.65761107e-01, -7.38784743e-01, -9.64964081e-01,
-3.88713618e-01, -5.17054693e-01, -5.46899721e-01, -9.90719601e-01,
-7.09073463e-04, -5.16624999e-01])