狠狠撸

狠狠撸Share a Scribd company logo
NumPy(1)
Benson
NumPy(1)
★高效率且方便的科學計算的套件.
★提供大量多維矩陣和編輯函式.
★方便處理向量、矩陣等運算
★包含了數學、邏輯、排序、I/O 離散傅立葉變換、基本線性代數、基本統計運
算及隨機模擬等功能
NumPy(1)
(一)安裝 NumPy
(二)匯入(import) NumPy
(三)建立陣列與矩陣
(四)陣列與矩陣的屬性
(五)矩陣加減乘除及基本運算
(六)索引、切片、迭代、合併和分割
(七)view, shadow copy 和 deep copy 運算
(八)位元運算
(九)字串運算
(十)IO 運算
(十一)線性代數與統計
NumPy(1)
(一)安裝 NumPy
★使用 pip3 安裝 NumPy 套件
(base) C:Usersbenson>pip3 install numpy
Collecting numpy
Downloading
https://files.pythonhosted.org/packages/b5/11/82916e23836a
37c0d76babf74a7ca6f7b4fedd0814eaa166aacc2318b87c/numpy-1.1
6.1-cp36-cp36m-win_amd64.whl (11.9MB)
100% |████████████████████████████████| 11.9MB 2.2MB/s
Installing collected packages: numpy
Successfully installed numpy-1.16.1
NumPy(1)
(二)匯入(import) NumPy
In [1]: import numpy as np
NumPy(1)
(三)建立陣列與矩陣
★在NumPy的世界裡有許多的函數可以直接建立陣列與矩陣,以下介紹常用的建
立的函數.
============= ===============================
character description
============= ===============================
array 返回一個自行輸入的陣列
matrix 返回一個自行輸入的矩陣
empty 返回一個隨機未初始化的陣列
matlib.empty 返回一個隨機未初始化的矩陣
arrange 返回一個給定範圍內(start~stop-1)且均勻間隔
(step)的陣列
NumPy(1)
linspace 返回一個給定範圍內(start~stop)且欲產生幾個
元素的(num)
random.random 返回一個隨機介於 0~1 的陣列
matlib.rand 返回一個隨機介於 0~1 的矩陣
fromfunction 返回由執行 lambda 運算式所產生的函式物件(陣
列)
tile 返回一個重複次數的陣列
repeat 返回一個重複次數的陣列
take 返回一個指定位置元素的陣列
zeros 返回一個全部元素為 0 的陣列
matlib.zeros 返回一個全部元素為 0 的矩陣
ones 返回一個全部元素為 1 的陣列
matlib.ones 返回一個全部元素為 1 的矩陣
full 返回一個全部元素為某值的陣列
eye 返回一個對角線元素為 1 且其他為 0 的陣列
NumPy(1)
matlib.eye 返回一個對角線元素為 1 且其他為 0 的矩陣
zeros_like 返回一個與輸入陣列一樣的 shape 和 type 且全部
元素為 0 的陣列
ones_like 返回一個與輸入陣列一樣的 shape 和 type 且全部
元素為 1 的陣列
full_like 返回一個與輸入陣列一樣的 shape 和 type 且全部
元素為某值的陣列
===================================================
NumPy(1)
array: 返回一個自行輸入的陣列
[方法]: np.array([元素]),或 np.array([元素], [元素]).其中,元素與
元素之間以逗號作區隔.
[範例 1]: 輸入一維的元素
In [1]: a_np_array = np.array([1,2,3])
In [2]: print(a_np_array)
[1 2 3]
NumPy(1)
[範例 2]: 輸入二維的元素
In [1]: a_np_array = np.array([[1,2,3],[4,5,6]])
In [2]: print(a_np_array)
[[1 2 3]
[4 5 6]]
NumPy(1)
matrix: 返回一個自行輸入的矩陣
[方法]: np.matrix(([元素], [元素])), np.matrix([[元素], [元
素]]),和 np.matrix('元素; 元素').其中,元素與元素之間以逗號作區隔.
[範例 1]: 輸入二維矩陣
In [1]: a_np_matrix = np.matrix(([1,2],[3,4]))
In [2]: print(a_np_matrix)
[[1 2]
[3 4]]
NumPy(1)
In [3]: a_np_matrix = np.matrix([[1,2],[3,4]])
In [4]: print(a_np_matrix)
[[1 2]
[3 4]]
In [5]: a_np_matrix = np.matrix('1,2;3,4')
In [6]: print(a_np_matrix)
[[1 2]
[3 4]]
NumPy(1)
[範例 2]: 輸入三維矩陣
In [1]: a_np_matrix = np.matrix(([1,2,3],[3,4,5],[5,6,7]))
In [2]: print(a_np_matrix)
[[1 2 3]
[3 4 5]
[5 6 7]]
In [3]: a_np_matrix = np.matrix([[1,2,3],[3,4,5],[5,6,7]])
In [4]: print(a_np_matrix)
[[1 2 3]
[3 4 5]
[5 6 7]]
NumPy(1)
In [3]: a_np_matrix = np.matrix('1,2,3;3,4,5;5,6,7')
In [4]: print(a_np_matrix)
[[1 2 3]
[3 4 5]
[5 6 7]]
NumPy(1)
empty: 返回一個隨機未初始化的陣列
[方法]: np.empty([row, column])
[範例 1]: 返回 2 行(row)2 列(column)隨機未初始化的陣列.
In [1]: a_np_empty = np.empty([2,2])
In [2]: print(a_np_empty)
[[6.16971893e-313 4.94065646e-324]
[0.00000000e+000 0.00000000e+000]]
NumPy(1)
matlib.empty: 返回一個隨機未初始化的矩陣
[方法]: np.matlib.empty([row, column])
[範例 1]: 返回 2 行(row)2 列(column)隨機未初始化的矩陣.
In [1]: a_np_empty = np.matlib.empty([2,2])
In [2]: print(a_np_empty)
[[4.49724942e-312 8.01304531e+262]
[2.60801927e-310 2.33419537e-312]]
NumPy(1)
arrange: 返回一個給定範圍內(start~stop-1)且均勻間隔(step)的陣列
[方法]: np.arange(stop), np.arange(start,stop),或
np.arange(start,stop,step)
[範例 1]: 給定最終值 step=10(start 預設為 0, step 預設為 1)
In [1]: a_np_arange1 = np.arange(10)
In [2]: print(a_np_arange1)
[0 1 2 3 4 5 6 7 8 9]
NumPy(1)
[範例 2]:給定起始值 start=2 和最終值 stop=10(step 預設為 1)
In [1]: a_np_arange1 = np.arange(2,10)
In [2]: print(a_np_arange1)
[2 3 4 5 6 7 8 9]
[範例 3]:給定起始值 start=2,最終值 stop=1 和均勻間隔 step=3
In [1]: a_np_arange1 = np.arange(2,10,3)
In [2]: print(a_np_arange1)
[2 5 8]
NumPy(1)
linspace: 返回一個給定範圍內(start~stop)且欲產生幾個元素的(num)
的陣列
[方法]: np.linspace(start,stop,num)
[範例 1]: 給定起始值 start=1,最終值 stop=10 且均勻產生 5 個元素.
In [1]: a_np_linspace = np.linspace(1,10,5)
In [2]: print(a_np_linspace)
[ 1. 3.25 5.5 7.75 10. ]
NumPy(1)
random.random: 返回一個隨機介於 0~1 的陣列
[方法]:
np.random.random(row),或 np.random.random((row,column))
[範例 1]: 返回 1 行(row)3 列(column)的陣列
In [1]: a_np_random = np.random.random(3)
In [2]: print(a_np_random)
[0.56705442 0.99448158 0.17873977]
NumPy(1)
[範例 2]: 返回 3 行(row)4 列(column)的陣列
In [1]: a_np_random = np.random.random((3,4))
In [2]: print(a_np_random)
[[0.01220009 0.45699848 0.93175194 0.84602469]
[0.47332988 0.90255503 0.22599553 0.30415374]
[0.71499388 0.72409148 0.01867644 0.2858131 ]]
NumPy(1)
matlib.rand: 返回一個隨機介於 0~1 的矩陣
[方法]:
np.matlib.rand(row),或 np.matlib.rand((row,column))
[範例 1]: 返回 1 行(row)3 列(column)的矩陣
In [1]: a_np_rand = np.matlib.rand(3)
In [2]: print(a_np_rand)
[[0.66690317 0.34592346 0.11711543]]
NumPy(1)
[範例 2]: 返回 3 行(row)4 列(column)的矩陣
In [1]: a_np_rand = np.matlib.rand((3,4))
In [2]: print(a_np_rand)
[[0.11313814 0.65769475 0.66595726 0.13753123]
[0.41555964 0.83218225 0.92174339 0.01412265]
[0.93898292 0.50744586 0.43109374 0.9164317 ]]
NumPy(1)
fromfunction: 返回由執行 lambda 運算式所產生的函式物件(陣列)
[方法]: np.fromfunction(function,shape).
[範例 1]: function 為 i+j, shape 為(2,2)
In [1]: np.fromfunction(lambda i, j: i + j, (2, 2), dtype=int)
Out[1]:
array([[0, 1],
[1, 2]])
NumPy(1)
如同以下程式:
In [1]: def add_array(i,j):
return i+j
In [2]: i=np.array([[0, 0],[1, 1]])
In [3]: j=np.array([[0, 1],[0, 1]])
In [4]: add_array(i,j)
Out[4]:
array([[0, 1],
[1, 2]])
NumPy(1)
tile: 返回一個重複次數的陣列
[方法]: np.tile(A, reps)
[範例 1]: A 為要重複的陣列(a_array), reps 為重複的次數(reps=2).
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.tile(a_array, 2)
Out[2]:
array([[1, 2, 1, 2],
[3, 4, 3, 4]])
NumPy(1)
[範例 2]: A 為要重複的陣列(a_array), reps 為重複的次數
(reps=(2,2)).
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.tile(a_array, (2,2))
Out[2]:
array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
NumPy(1)
repeat: 返回一個重複次數的陣列
[方法]: np.repeat(A, reps, axis=None)
[範例 1]: A 為要重複的陣列(a_array), reps 為重複的次數(reps=2).
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.repeat(x, 2)
Out[2]:
array([1, 1, 2, 2, 3, 3, 4, 4])
NumPy(1)
[範例 2]: A 為要重複的陣列(a_array), reps 為重複的次數(reps=[1,2]),
axis 等於 0.
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.repeat(x, [1, 2], axis=0)
Out[2]:
array([[1, 2],
[3, 4],
[3, 4]])
NumPy(1)
[範例 3]: A 為要重複的陣列(a_array), reps 為重複的次數
(reps=[1,2]), axis 等於 1.
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.repeat(x, [1, 2], axis=1)
Out[2]:
array([[1, 2, 2],
[3, 4, 4]])
NumPy(1)
take: 返回一個指定位置元素的陣列
[方法]: np.take(a, indices).
[範例1]: A為要重複的陣列(a_array), indices為指定位置元素(indices
_array).
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: indices_array = np.array([0,3])
In [3]: np.take(a_array, indices_array)
Out[3]:
array([1, 4])
NumPy(1)
[範例2]: A為要重複的陣列(a_array), indices為指定位置元素(indices
_array).
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: indices_array = np.array([[3,2],[1,0]])
In [3]: np.take(a_array, indices_array)
Out[3]:
array([[4, 3],
[2, 1]])
NumPy(1)
zeros: 返回一個全部元素為 0 的陣列
[方法]: np.zeros([row, column])
[範例 1]:返回 3 行(row)4 列(column)為 0 的陣列.
In [1]: np.zeros([3,4])
Out[1]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
NumPy(1)
matlib.zeros: 返回一個全部元素為 0 的矩陣
[方法]: np.matlib.zeros([row, column])
[範例 1]:返回 3 行(row)4 列(column)為 0 的矩陣.
In [1]: np.matlib.zeros([3,4])
Out[1]:
matrix([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
NumPy(1)
ones: 返回一個全部元素為 1 的陣列
[方法]: np.ones([row, column])
[範例 1]: 返回 3 行(row)4 列(column)為 1 的陣列.
In [1]: np.ones([3,4])
Out[1]:
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
NumPy(1)
matlib.ones: 返回一個全部元素為 1 的矩陣
[方法]: np.matlib.ones([row, column])
[範例 1]: 返回 3 行(row)4 列(column)為 1 的矩陣.
In [1]: np.matlib.ones([3,4])
Out[1]:
matrix([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
NumPy(1)
full: 返回一個全部元素為某值的陣列
[方法]: np.full([row, column], fill_value)
[範例 1]: 返回 3 行(row)4 列(column)為 5 的陣列.
In [1]: np.full([3,4],5)
Out[1]:
array([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]])
NumPy(1)
eye: 返回一個對角線元素為 1 且其他為 0 的陣列
[方法]: np.eys(N)
[範例 1]: 返回 3×3 且對角線元素為 1 的陣列.
In [1]: np.eye(3)
Out[1]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
NumPy(1)
matlib.eye: 返回一個對角線元素為 1 且其他為 0 的矩陣
[方法]: np.matlib.eys(N)
[範例 1]: 返回 3×3 且對角線元素為 1 的矩陣.
In [1]: np.matlib.eye(3)
Out[1]:
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
NumPy(1)
zeros_like: 返回一個與輸入陣列一樣的 shape 和 type 且全部元素為 0 的
陣列
[方法]: np.zeros_like(a)
[範例 1]: 返回一個與輸入陣列(a_array)一樣的 shape 和 type 且全部元
素為 0 的陣列.
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.zeros_like(a_array)
Out[2]:
array([[0, 0],
[0, 0]])
NumPy(1)
ones_like: 返回一個與輸入陣列一樣的 shape 和 type 且全部元素為 1 的
陣列
[方法]: np.ones_like(a)
[範例 1]: 返回一個與輸入陣列(a_array)一樣的 shape 和 type 且全部元
素為 1 的陣列.
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.ones_like(a_array)
Out[2]:
array([[1, 1],
[1, 1]])
NumPy(1)
full_like: 返回一個與輸入陣列一樣的 shape 和 type 且全部元素為某值
的陣列
[方法]:np.full_like(a)
[範例 1]: 返回一個與輸入陣列(a_array)一樣的 shape 和 type 且全部元
素為 5 的陣列.
In [1]: a_array = np.array([[1, 2],[3, 4]])
In [2]: np.full_like(a_array,5)
Out[2]:
array([[5, 5],
[5, 5]])
NumPy(1)
(四)陣列與矩陣的屬性
★可以透過以下函數來確認 Numpy 陣列與矩陣的屬性.
============= ===============================
character description
============= ===============================
dtype 返回陣列的類形
size 返回陣列的元素總數量
shape 返回陣列的維度大小
ndim 返回陣列的元素總數量
itemsize 返回陣列中元素的大小(Bytes)
===================================================
NumPy(1)
dtype: 返回陣列與矩陣的類形
[方法]: x.dtype
[範例 1.1]: 確認陣列 one_mtx = [1,2,3]的屬性
In [1]: import numpy as np
In [2]: one_mtx = np.array([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data dtype:",one_mtx.dtype,"n")
one_mtx-Data dtype: int32
NumPy(1)
[範例 1.2]: 確認矩陣 one_mtx = [1,2,3]的屬性
In [1]: import numpy as np
In [2]: one_mtx = np.matrix([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data dtype:",one_mtx.dtype,"n")
one_mtx-Data dtype: int32
NumPy(1)
[範例 2.1]: 確認陣列 two_mtx = [1,2,3][4,5,6]的屬性
In [1]: import numpy as np
In [2]: two_mtx = np.array([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data dtype:",two_mtx.dtype,"n")
two_mtx-Data dtype: int32
NumPy(1)
[範例 2.2]: 確認矩陣 two_mtx = [1,2,3][4,5,6]的屬性
In [1]: import numpy as np
In [2]: two_mtx = np.matrix([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data dtype:",two_mtx.dtype,"n")
two_mtx-Data dtype: int32
NumPy(1)
[範例 3.1]: 確認陣列 three_mtx = [1,2,3][4,5,6][7,8,9]的屬性
In [1]: import numpy as np
In [2]: three_mtx = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data dtype:",three_mtx.dtype,"n")
three_mtx-Data dtype: int32
NumPy(1)
[範例 3.2]: 確認矩陣 three_mtx = [1,2,3][4,5,6][7,8,9]的屬性
In [1]: import numpy as np
In [2]: three_mtx = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
In [4]: print("three_mtx-Data dtype:",three_mtx.dtype,"n")
three_mtx-Data dtype: int32
NumPy(1)
size: 返回陣列與矩陣的元素總數量
[方法]: x.size
[範例 1.1]: 確認陣列 one_mtx = [1,2,3]的元素總數量
In [1]: import numpy as np
In [2]: one_mtx = np.array([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data size:",one_mtx.size,"n")
one_mtx-Data size: 3
NumPy(1)
[範例 1.2]: 確認矩陣 one_mtx = [1,2,3]的元素總數量
In [1]: import numpy as np
In [2]: one_mtx = np.matrix([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data size:",one_mtx.size,"n")
one_mtx-Data size: 3
NumPy(1)
[範例 2.1]: 確認陣列 two_mtx = [1,2,3][4,5,6]的元素總數量
In [1]: import numpy as np
In [2]: two_mtx = np.array([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data size:",two_mtx.size,"n")
two_mtx-Data size: 6
NumPy(1)
[範例 2.2]: 確認矩陣 two_mtx = [1,2,3][4,5,6]的元素總數量
In [1]: import numpy as np
In [2]: two_mtx = np.matrix([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data size:",two_mtx.size,"n")
two_mtx-Data size: 6
NumPy(1)
[範例 3.1]: 確認陣列 three_mtx = [1,2,3][4,5,6][7,8,9]的元素總
數量
In [1]: import numpy as np
In [2]: three_mtx = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data size:",three_mtx.size,"n")
three_mtx-Data size: 9
NumPy(1)
[範例 3.2]: 確認矩陣 three_mtx = [1,2,3][4,5,6][7,8,9]的元素總
數量
In [1]: import numpy as np
In [2]: three_mtx = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data size:",three_mtx.size,"n")
three_mtx-Data size: 9
NumPy(1)
shape:返回陣列與矩陣的維度大小
[方法]: x.shape
[範例 1.1]: 確認陣列 one_mtx = [1,2,3]的維度大小
In [1]: import numpy as np
In [2]: one_mtx = np.array([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data shape:",one_mtx.shape,"n")
one_mtx-Data shape: (3,)
NumPy(1)
[範例 1.2]: 確認矩陣 one_mtx = [1,2,3]的維度大小
In [1]: import numpy as np
In [2]: one_mtx = np.matrix([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data shape:",one_mtx.shape,"n")
one_mtx-Data shape: (1, 3)
NumPy(1)
[範例 2.1]: 確認陣列 two_mtx = [1,2,3][4,5,6]的維度大小
In [1]: import numpy as np
In [2]: two_mtx = np.array([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data shape:",two_mtx.shape,"n")
two_mtx-Data shape: (2, 3)
NumPy(1)
[範例 2.2]: 確認矩陣 two_mtx = [1,2,3][4,5,6]的維度大小
In [1]: import numpy as np
In [2]: two_mtx = np.matrix([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data shape:",two_mtx.shape,"n")
two_mtx-Data shape: (2, 3)
NumPy(1)
[範例 3.1]: 確認陣列 three_mtx = [1,2,3][4,5,6][7,8,9]的維度大
小
In [1]: import numpy as np
In [2]: three_mtx = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data shape:",three_mtx.shape,"n")
three_mtx-Data shape: (1, 3, 3)
NumPy(1)
[範例 3.2]: 確認矩陣 three_mtx = [1,2,3][4,5,6][7,8,9]的維度大
小
In [1]: import numpy as np
In [2]: three_mtx = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data shape:",three_mtx.shape,"n")
three_mtx-Data shape: (3, 3)
NumPy(1)
ndim: 返回陣列與矩陣的元素總數量
[方法]: x.ndim
[範例 1.1]: 確認陣列 one_mtx = [1,2,3]的元素總數量
In [1]: import numpy as np
In [2]: one_mtx = np.array([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data ndim:",one_mtx.ndim,"n")
one_mtx-Data ndim: 1
NumPy(1)
[範例 1.2]: 確認矩陣 one_mtx = [1,2,3]的元素總數量
In [1]: import numpy as np
In [2]: one_mtx = np.matrix([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data ndim:",one_mtx.ndim,"n")
one_mtx-Data ndim: 2
NumPy(1)
[範例 2.1]: 確認陣列 two_mtx = [1,2,3][4,5,6]的元素總數量
In [1]: import numpy as np
In [2]: two_mtx = np.array([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data ndim:",two_mtx.ndim,"n")
two_mtx-Data ndim: 2
NumPy(1)
[範例 2.2]: 確認矩陣 two_mtx = [1,2,3][4,5,6]的元素總數量
In [1]: import numpy as np
In [2]: two_mtx = np.matrix([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data ndim:",two_mtx.ndim,"n")
two_mtx-Data ndim: 2
NumPy(1)
[範例 3.1]: 確認陣列 three_mtx = [1,2,3][4,5,6][7,8,9]的元素總
數量
In [1]: import numpy as np
In [2]: three_mtx = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data ndim:",three_mtx.ndim,"n")
three_mtx-Data ndim: 3
NumPy(1)
[範例 3.2]: 確認矩陣 three_mtx = [1,2,3][4,5,6][7,8,9]的元素總
數量
In [1]: import numpy as np
In [2]: three_mtx = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data ndim:",three_mtx.ndim,"n")
three_mtx-Data ndim: 2
NumPy(1)
itemsize: 返回陣列與矩陣中元素的大小(Bytes)
[方法]: x.itemsize
[範例 1.1]: 確認陣列 one_mtx = [1,2,3]的元素的大小
In [1]: import numpy as np
In [2]: one_mtx = np.array([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data
itemsize:",one_mtx.itemsize,"n")
one_mtx-Data itemsize: 4
NumPy(1)
[範例 1.2]: 確認矩陣 one_mtx = [1,2,3]的元素的大小
In [1]: import numpy as np
In [2]: one_mtx = np.matrix([1,2,3])
In [3]: print("one_mtx-Data:",one_mtx,"n")
one_mtx-Data: [1 2 3]
In [4]: print("one_mtx-Data
itemsize:",one_mtx.itemsize,"n")
one_mtx-Data itemsize: 4
NumPy(1)
[範例 2.1]: 確認陣列 two_mtx = [1,2,3][4,5,6]的元素的大小
In [1]: import numpy as np
In [2]: two_mtx = np.array([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data
itemsize:",two_mtx.itemsize,"n")
two_mtx-Data itemsize: 4
NumPy(1)
[範例 2.2]: 確認矩陣 two_mtx = [1,2,3][4,5,6]的元素的大小
In [1]: import numpy as np
In [2]: two_mtx = np.matrix([[1,2,3],[4,5,6]])
In [3]: print("two_mtx-Data :",two_mtx,"n")
two_mtx-Data : [[1 2 3]
[4 5 6]]
In [4]: print("two_mtx-Data
itemsize:",two_mtx.itemsize,"n")
two_mtx-Data itemsize: 4
NumPy(1)
[範例 3.1]: 確認陣列 three_mtx = [1,2,3][4,5,6][7,8,9]的元素的
大小
In [1]: import numpy as np
In [2]: three_mtx = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data
itemsize:",three_mtx.itemsize,"n")
three_mtx-Data itemsize: 4
NumPy(1)
[範例 3.2]: 確認矩陣 three_mtx = [1,2,3][4,5,6][7,8,9]的元素的
大小
In [1]: import numpy as np
In [2]: three_mtx = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
In [3]: print("three_mtx-Data :",three_mtx,"n")
three_mtx-Data: [[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
In [4]: print("three_mtx-Data
itemsize:",three_mtx.itemsize,"n")
three_mtx-Data itemsize: 4
NumPy(1)
(五)矩陣加減乘除及基本運算
★可以透過以下符號或函數來進行 Numpy 矩陣的基本運算.
============= ===============================
character description
============= ===============================
+, np.add(a,b) 矩陣相加
-, np.subtract(a,b) 矩陣相減
*, np.multiply(a,b) 矩陣相乘
/, np.divide(a,b) 矩陣相除
NumPy(1)
a.sum() 矩陣 a 的元素總和
a.min() 矩陣 a 的最小值
a.max() 矩陣 a 的最大值
median(a) 矩陣 a 的中位數
mean(a) 矩陣 a 的平均數
average(a) 矩陣 a 的平均數
std(a) 矩陣 a 的標準差
sqrt(a) 矩陣 a 的所有元素開平方根
exp(a) 矩陣 a 的所有元素進行 Exponential
function 運算
NumPy(1)
a.T, np.transpose(a) 矩陣 a 的轉置
sort(a) 矩陣 a 元素的排序
clip(a, minimum, maximum)
轉換矩陣 a 的指定元素為最小值或最大值
diff(a) 矩陣 a 的累差運算,後一列與前一列相減等於前一列
的元素數值
cumsum(a) 矩陣的累加運算,後一位與前一位元素相加等於後一
位的元素數值
===================================================
NumPy(1)
+, np.add(a,b): 陣列或矩陣相加
[方法]: a+b, or np.add(a,b)
[範例 1]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用+來計
算相加.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: a + b
Out[3]:
array([[ 3, 6, 9],
NumPy(1)
[12, 15, 18]])
[範例 2]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用 np.add
來計算相加.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: np.add(a , b)
Out[3]:
array([[ 3, 6, 9],
[12, 15, 18]])
NumPy(1)
-, np.subtract(a,b): 陣列或矩陣相減
[方法]: a-b, or np.subtract(a,b)
[範例 1]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用-來計
算相減.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: a - b
Out[3]:
array([[1, 2, 3],
NumPy(1)
[4, 5, 6]])
[範例 2]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用
np.subtract 來計算相減.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: np.subtract (a , b)
Out[3]:
array([[1, 2, 3],
[4, 5, 6]])
NumPy(1)
*, np.multiply(a,b): 陣列或矩陣相乘
[方法]: a*b, or np.multiply(a,b)
[範例 1]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用*來計
算加法.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: a * b
Out[3]:
array([[ 2, 8, 18],
NumPy(1)
[32, 50, 72]])
[範例 2]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用
np.multiply 來計算加法.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: np.multiply (a , b)
Out[3]:
array([[ 2, 8, 18],
[32, 50, 72]])
NumPy(1)
/, np.divide(a,b): 陣列或矩陣相除
[方法]: a/b, or np.divide(a,b)
[範例 1]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用/來計
算加法.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: a / b
Out[3]:
array([[2., 2., 2.],
NumPy(1)
[2., 2., 2.]])
[範例 2]: a = [2,4,6][8,10,12], b = [1,2,3][4,5,6],運用
np.divide 來計算加法.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: b = np.array([[1,2,3],[4,5,6]])
In [3]: np.divide (a , b)
Out[3]:
array([[2., 2., 2.],
[2., 2., 2.]])
NumPy(1)
sum(): 陣列或矩陣的元素總和
[方法]: a.sum()
[範例 1]: a = [2,4,6][8,10,12],運用 sum()來計算元素的總和.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: a.sum()
Out[2]: 42
NumPy(1)
min(): 陣列或矩陣的最小值
[方法]: a.min()
[範例 1]: a = [2,4,6][8,10,12],運用 min()來計算最小的元素值.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: a.min()
Out[2]: 2
NumPy(1)
max(): 陣列或矩陣的最大值
[方法]: a.max()
[範例 1]: a = [2,4,6][8,10,12],運用 max()來計算最大的元素值.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: a.max()
Out[2]: 12
NumPy(1)
median(): 陣列或矩陣的中位數
[方法]: np.median(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.median()來計算中位數.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.median(a)
Out[2]: 7.0
NumPy(1)
mean(): 陣列或矩陣的平均數
[方法]: np.mean(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.mean()來計算平均數.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.mean(a)
Out[2]: 7.0
NumPy(1)
average(): 陣列或矩陣的平均數
[方法]: np.average(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.average()來計算平均數.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.average(a)
Out[2]: 7.0
NumPy(1)
std(): 陣列或矩陣的標準差
[方法]: np.std(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.std()來計算標準差.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.std(a)
Out[2]: 3.415650255319866
NumPy(1)
sqrt(): 陣列或矩陣的所有元素開平方根
[方法]: np.sqrt(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.sqrt()來計算所有元素開平
方根.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.sqrt(a)
Out[2]:
array([[1.41421356, 2. , 2.44948974],
[2.82842712, 3.16227766, 3.46410162]])
NumPy(1)
exp(): 陣列或矩陣所有元素進行 Exponential function 運算
[方法]: np.exp(a)
[範例 1]: a = [2,4,6][8,10,12],運用 np.exp()來計算所有元素進行
Exponential function 運算.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.exp(a)
Out[2]:
array([[7.38905610e+00, 5.45981500e+01, 4.03428793e+02],
[2.98095799e+03, 2.20264658e+04, 1.62754791e+05]])
NumPy(1)
a.T, transpose(): 矩陣的轉置
[方法]: a.T, or np.transpose(a)
[範例 1]: a = [2,4,6][8,10,12],運用 a.T 來進行轉置.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: a.T
Out[2]:
array([[ 2, 8],
[ 4, 10],
[ 6, 12]])
NumPy(1)
[範例 2]: a = [2,4,6][8,10,12],運用 np.transpose()來進行轉置.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.transpose(a)
Out[2]:
array([[ 2, 8],
[ 4, 10],
[ 6, 12]])
NumPy(1)
sort(): 矩陣元素的排序
[方法]: np.sort(a)
[範例 1]: a = [12,10,8][6,4,2],運用 np.sort()來進行排序.
In [1]: a = np.array([[12,10,8],[6,4,2]])
In [2]: np.sort(a)
Out[2]:
array([[ 8, 10, 12],
[ 2, 4, 6]])
NumPy(1)
clip(a, minimum, maximum): 矩陣指定的元素轉換成最小值或最大值
[方法]: np.clip(a, minimum, maximum)
[範例 1]: a = [2,4,6][8,10,12],運用 np.clip()來轉換成最小值與最
大值.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.clip(a, 4, 10)
Out[2]:
array([[ 4, 4, 6],
[ 8, 10, 10]])
NumPy(1)
diff(): 矩陣累差運算
[方法]: np.diff()
[範例 1]: a = [2,4,6][8,10,12],運用 np.diff()來進行累差運算,後一
列與前一列相減等於前一列.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.diff(a)
Out[2]:
array([[2, 2],
[2, 2]])
NumPy(1)
cumsum(): 矩陣累加運算
[方法]: np.cumsum()
[範例 1]: a = [2,4,6][8,10,12],運用 np.cumsum()來進行累加運算, 後
一位與前一位元素相加等於後一位的元素數值.
In [1]: a = np.array([[2,4,6],[8,10,12]])
In [2]: np.cumsum(a)
Out[2]:
array([ 2, 6, 12, 20, 30, 42], dtype=int32)

More Related Content

Num py basic(1) - v01