(本專欄是我的慕課學習筆記,後續發現不足之處會更新)
定義list資料結構:
>>> L1 = [[1, 3, 5], [2, 4, 6]]
>>> print('L1類型:', type(L1))
>>> print('L1:', L1)
L1類型: <class 'list'>
L1: [[1, 3, 5], [2, 4, 6]]
運算速度更快的資料結構:(ndarray隻允許存放一種資料類型)
>>> import numpy as np
>>> L1 = [[1, 3, 5], [2, 4, 6]]
>>> L2 = np.array(L1) # ndarray隻允許存放一種資料類型
>>> print('L2類型:', type(L2))
L2類型: <class 'numpy.ndarray'>
>>> print('L2:\n', L2)
L2:
[[1 3 5]
[2 4 6]]
>>> print('L2規模:', L2.shape) # L2的規模(幾行幾列)
L2規模: (2, 3)
>>> print('L2次元:', L2.ndim) # L2的次元
L2次元: 2
>>> print('L2資料類型:', L2.dtype) # L2的資料類型
L2資料類型: int32
>>> print('L2每個元素占位元組數:', L2.itemsize) # L2每個元素的大小
L2每個元素占位元組數: 4
>>> print('L2大小:', L2.size) # L2大小
L2大小: 6
>>> L3 = np.array(L1, dtype=np.float) # 規定存放資料為float型
>>> print(L3.dtype)
float64
numpy常用Array:
import numpy as np
>>> print(np.zeros([2, 4])) # 2行4列的0
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
>>> print(np.ones([3, 5])) # 3行5列的1
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
>>> print(np.random.rand()) # 單個随機數
0.5346756487556099
>>> print(np.random.rand(2, 4)) # 2行4列的随機數
[[0.18197163 0.07243006 0.07054381 0.14984757]
[0.44981785 0.99545415 0.53630171 0.84512616]]
>>> print(np.random.randint(1, 20)) # 單個1到20之間的随機整數
10
>>> print(np.random.randint(1, 20, 3)) # 連續生成3個1到20之間的随機整數
[16 1 15]
>>> print(np.random.randn(2, 4)) # 生成2行4列均值為0,方差為1的正态分布随機數
[[-1.43757873 -1.64088587 -0.2095437 0.93030802]
[ 1.93776804 -0.06551866 -0.81996059 1.25876278]]
>>> print(np.random.choice([1, 2, 3, 4, 5])) # 從這5個數之中随機取數
5
numpy常用操作:(對array對象的每個資料進行操作)
>>> import numpy as np
# 生成等差數列1~10,從1開始,步長為2
# arange()傳回一個array()對象,range()傳回一個list對象
>>> array = np.arange(1, 11, 2)
>>> print(array) # [1,3,5,7,9]
[1 3 5 7 9]
>>> print(np.exp(array)) # [e^1,e^3,e^5,e^7,e^9]
[2.71828183e+00 2.00855369e+01 1.48413159e+02 1.09663316e+03
8.10308393e+03]
sum():
>>> import numpy as np
>>> L = np.array([[[1, 2, 3, 4],
>>> [5, 6, 7, 8]],
>>> [[9, 10, 11, 12],
>>> [13, 14, 15, 16]]
>>> ])
>>> print(L.sum()) # 計算各數之和
136
>>> print(L.sum(axis=0)) # 第1個元素為1+9=10,最後1個為8+16=24
[[10 12 14 16]
[18 20 22 24]]
>>> print(L.sum(axis=1)) # 第1個元素為1+5=6,最後1個為12+16=28
[[ 6 8 10 12]
[22 24 26 28]]
>>> print(L.sum(axis=2)) # 第1個元素為1+2+3+4=10,最後1個為13+14+15+16=58
[[10 26]
[42 58]]
注意list和array的差別:
>>> import numpy as np
>>> L1 = [10, 20, 30, 40]
>>> L2 = [5, 6, 7, 8]
>>> L3 = L1 + L2 # 追加,将L2接在L1後面
>>> print(L3)
[10, 20, 30, 40, 5, 6, 7, 8]
>>> array1 = np.array([10, 20, 30, 40])
>>> array2 = np.array([5, 6, 7, 8])
>>> array3 = array1 + array2 # +,-,*,/,**(後跟多少就是多少次方)同理
>>> print(array3)
[15 26 37 48]
>>> array4 = np.concatenate((array1, array2), axis=0) # array的追加
>>> print(array4)
[10 20 30 40 5 6 7 8]
>>> print(np.split(array1, 2)) # 将array1分成2份
[array([10, 20]), array([30, 40])]
矩陣相乘:
>>> import numpy as np
>>> array1 = np.array([10, 20, 30, 40])
>>> array2 = np.array([5, 6, 7, 8])
>>> array11 = array1.reshape([2, 2])
>>> array12 = array2.reshape([2, 2])
>>> print('array11:\n', array11, '\n', 'array12:\n', array12)
array11:
[[10 20]
[30 40]]
array12:
[[5 6]
[7 8]]
>>> array13 = np.dot(array11, array12) # 矩陣相乘
>>> print('array13:\n', array13)
array13:
[[190 220]
[430 500]]
矩陣相關的操作:
>>> import numpy as np
>>> from numpy.linalg import *
>>> print(np.eye(3)) # 3階機關矩陣
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
>>> L = np.array([[1, 2],
>>> [3, 4]])
>>> print('逆矩陣:\n', inv(L)) # 逆矩陣
逆矩陣:
[[-2. 1. ]
[ 1.5 -0.5]]
>>> print('轉置矩陣:\n', L.transpose()) # 轉置矩陣
轉置矩陣:
[[1 3]
[2 4]]
>>> print('行列式:\n', format(det(L), '.2f')) # 行列式(保留2位小數)
行列式:
-2.00
>>> print('特征值和特征向量:\n', eig(L)) # 特征值和特征向量
特征值和特征向量:
(array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))
線性方程組:
>>> import numpy as np
>>> from numpy.linalg import *
>>> A = np.array([[1, 2],
>>> [3, 4]]) # A為2*2矩陣
>>> B = np.array([[17],
>>> [41]]) # B為2*1矩陣
>>> print(solve(A, B)) # AX=B,求解X
[[7.]
[5.]]
# [[1, 2], [[7], = [[17],
# [3, 4]] [5]] [41]]
相關系數:
>>> import numpy as np
>>> print(np.corrcoef([1, 0, 1], [0, 2, 1])) # r(X,X),r(X,Y),r(Y,X),r(Y,Y)
[[ 1. -0.8660254]
[-0.8660254 1. ]]
生成多項式:
>>> import numpy as np
>>> print(np.poly1d([5, 8, 4, 3]))
3 2
5 x + 8 x + 4 x + 3
氷鸢鸢鸢
2020.8.4