0% found this document useful (0 votes)
21 views

14 NumPy

Uploaded by

saturnusfaunus54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

14 NumPy

Uploaded by

saturnusfaunus54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Numpy

import numpy as np
Numpy 基本運算 (1/2)
l NumPy 是 Python 語⾔的⼀個第三⽅函式庫,⽀援多維度陣 import numpy as np import numpy as np
列與矩陣運算,Numpy 具備以下特性: # creat matrix matrix1 = np.array([[1, 2],
matrix1 = np.array([ [3, 4],])
Ø ⽀援多維度陣列類別資料 [1, 2, 3], matrix2 = np.array([[5, 6],
[4, 5, 6], [7, 8],])
Ø 廣播運算的特性,使矩陣運算容易理解執⾏ [7, 8, 9],])
foo = [['a', 'b', 'c'], a = matrix1 + matrix2
Ø 底層由 C/C++ 和 Fortran 實作,執⾏效率佳 ['d', 'e', 'f'], b = matrix1 - matrix2
['g', 'h', 'i'],]
Ø ⼤量科學計算的實作函數,如線性代數、三⾓函數等 matrix2 = np.array(foo) # 對應位置的乘積 c1, c2
boo = matrix1[1][1] c1 = matrix1 * matrix2
l NumPy的矩陣是 ndarray 類別的物件 (N維陣列, n- zoo = matrix2[1,2] c2 = np.multiply(matrix1, matrix2)
dimensional array), 可以⽤多層list來建立, 只能儲存相同資 # create 2X2 (two-dimension) matrix
l22 = [[1, 2], d = matrix1 / matrix2
料型別的物件 [3, 4],]
m22 = np.array(l22) # 矩陣乘法 e1, e2
l ndarray 運算⼦: +, -, *, /, @ egg = m22[1,1]
# create 3X3 (two-dimension) matrix
e1 = matrix1 @ matrix2
e2 = np.matmul(matrix1, matrix2)
l ndarray 內建函式: l33 = [[1, 2, 3],
[4, 5, 6], # 內積: 2D相當於@, in 1D 計算為⼀個純量
[7, 8, 9],] f1 = np.dot(matrix1, matrix2)
• array() m33 = np.array(l33) f2 = np.dot(np.array([1, 3]), np.array([2, 4]))
spam = m33[1,1]
• dot() 內積: in 2D相當於@, in 1D 計算為⼀個純量 # create 2X3X3 (three-dimension) matrix
l233 = [[[1, 2, 3],
• multiply() 對應位置的乘積 (*) [4, 5, 6],
• matmul() 矩陣乘法 (@): A@B is equal to np.matmul(A, B) [7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]],]
m233 = np.array(l233)
hen = m233[1,1,1]
Numpy 基本運算 (2/2)
import numpy as np import numpy as np
A = np.array([[1, 2, 3], A = np.array([[1, 2, 3],
l ndarray 內建函式: [4, 5, 6],]) [4, 5, 6],
B = np.array([[7, 8, 9], [7, 8, 9],])
ü vstack(), hstack()
[10, 11, 12],])
ü reshape() a = A.shape
C = np.vstack([A, B]) b = A.diagonal()
ü arange()
D = np.hstack([A, B]) c = A.flatten()
l ndarray 型別⽅法: E = A.reshape(6, 1) d = A.transpose()
F = A.reshape(1, 6) e = A.min()
ü .shape G = A.reshape(3, 2) f = A.max()
g = A.mean()
ü .diagonal(), .flatten(), .transpose()
# create matrix by numpy h = A.sum()
ü .min(), .max(), .mean(), .sum() nums = np.arange(1, 5) foo = A[0][1]
H = nums.reshape(2, 2) boo = A[0, 1]
l ndarray indexing: matrix[0][1], matrix[0, 1] I = np.arange(1, 13).reshape(3, 2, 2)
J = np.arange(1, 24, 2).reshape(3, 2, 2)
#Lab

NumPy 運算
1. use arrange() and reshape() 產⽣右⽅的matrix
2. ⽤ np.arange() 和 np.reshape(), 建立⼀個3x3 Numpy 陣列A, 內容是數字 3 到 11
• 求陣列 A 的所有元素的最⼩值,最⼤值,平均值,總和
• 將 A 的每個元素進⾏平⽅計算,並將結果存到 B
3. 設 a = (78, 22, 65, 87, 12, 98, 63, 79) 且 x = 57, 試著在a 中找出離 x 最近的數,請寫兩個版本,⼀個⽤for, 另
⼀個⽤Numpy廣播運算

#1 #2 #3A #3B
import numpy as np import numpy as np import numpy as np import numpy as np
a = np.arange(1,10) A = np.arange(3,12).reshape(3, 3) import math a = (78, 22, 65, 87, 12, 98, 63, 79)
b = np.arange(11,20) a = A.shape a = (78, 22, 65, 87, 12, 98, 63, 79) x = 57
c = np.arange(21,30) b = A.min() diff = () A = np.array(a)
d = np.hstack([a, b]) c = A.max() x = 57 B = abs(A - 57)
e = np.hstack([d, c]) d = A.mean() min_index = 0 min_distance = min(B)
matrix_1 = e.reshape(3, 3, 3) e = A.sum() min_distance = math.inf pos = np.where(B == min_distance)[0][0]
B=A*A for i in range(0, len(a)): num = a[pos]
z = np.arange(1, 30) if min_distance > abs(x-a[i]): print(f'a[{pos}]:{a[pos]} have min value
y = np.delete(z, [9, 19]) min_index = i {min_distance}')
matrix_2 = y.reshape(3, 3, 3) min_distance = abs(x-a[i])

print(f'a[{min_index}]:{a[min_index]}
have min value {min_distance}')

You might also like