Nền tảng Toán học cho Machine Learning
🎯 Mục tiêu bài học
Sau bài học này, học viên sẽ:
- Nắm vững các khái niệm Đại số tuyến tính cần thiết
- Hiểu các khái niệm Thống kê cơ bản
- Thực hành tính toán với NumPy
1. Đại số tuyến tính (Linear Algebra)
1.1 Vector va Ma trận
Vector la mảng 1 chiều:
Ma trận la mảng 2 chiều:
1.2 Cac phép toán cơ bản
| Phep toan | Cong thuc | Dieu kien |
|---|---|---|
| Cộng ma tran | Cung kích thước | |
| Nhân scalar | - | |
| Nhan ma tran | A: mxn, B: nxp | |
| Transpose | - | |
| Dot product | Cung độ dài |
1.3 Thực hành NumPy
Python
1import numpy as np23# Tạo vector4x = np.array([1, 2, 3])5print(f"Vector: {x}")6print(f"Shape: {x.shape}")78# Tạo ma trận9A = np.array([[1, 2, 3],10 [4, 5, 6]])11print(f"Ma trận:\n{A}")12print(f"Shape: {A.shape}")1314# Cac phép toán15B = np.array([[7, 8, 9],16 [10, 11, 12]])1718# Cong19print(f"A + B:\n{A + B}")2021# Nhân scalar22print(f"2 * A:\n{2 * A}")2324# Dot product25a = np.array([1, 2, 3])26b = np.array([4, 5, 6])27print(f"a dot b: {np.dot(a, b)}") # 1*4 + 2*5 + 3*6 = 322829# Nhan ma tran30C = np.array([[1, 2],31 [3, 4],32 [5, 6]])33print(f"A @ C:\n{A @ C}") # (2x3) @ (3x2) = (2x2)3435# Transpose36print(f"A transpose:\n{A.T}")2. Thống kê cơ bản (Statistics)
2.1 Cac do độ đo tập trung
| Do do | Cong thuc | Ý nghĩa |
|---|---|---|
| Mean | Giá trị trung bình | |
| Median | Giá trị giua | Không bị ảnh hưởng bởi outliers |
| Mode | Giá trị xuat hien nhieu nhat | Cho du lieu phân loại |
2.2 Cac độ đo phân tán
Variance (Phương sai):
Standard Deviation (Độ lệch chuẩn):
2.3 Vi du tính toán
Dữ liệu: [2, 4, 4, 4, 5, 5, 7, 9]
Tính Mean:
Tính Variance:
Tính Standard Deviation:
2.4 Thực hành voi NumPy
Python
1import numpy as np23data = np.array([2, 4, 4, 4, 5, 5, 7, 9])45print(f"Mean: {np.mean(data)}")6print(f"Median: {np.median(data)}")7print(f"Variance: {np.var(data)}")8print(f"Std: {np.std(data)}")9print(f"Min: {np.min(data)}")10print(f"Max: {np.max(data)}")11print(f"Percentile 25: {np.percentile(data, 25)}")12print(f"Percentile 75: {np.percentile(data, 75)}")3. Covariance va Correlation
3.1 Covariance (Hiệp phương sai)
- : Tương quan thuận
- : Tương quan nghịch
- : Khong tuong quan tuyến tính
3.2 Correlation (Hệ số tương quan Pearson)
| Giá trị r | Ý nghĩa |
|---|---|
| r = 1 | Tương quan thuận hoàn hảo |
| r = -1 | Tương quan nghịch hoàn hảo |
| r = 0 | Khong tuong quan |
| 0.7 < r < 1 | Tương quan manh |
| 0.3 < r < 0.7 | Tương quan vừa |
| r < 0.3 | Tương quan yếu |
3.3 Thực hành
Python
1import numpy as np23X = np.array([1, 2, 3, 4, 5])4Y = np.array([2, 4, 5, 4, 5])56# Covariance7cov_matrix = np.cov(X, Y)8print(f"Covariance Matrix:\n{cov_matrix}")910# Correlation11corr_matrix = np.corrcoef(X, Y)12print(f"Correlation Matrix:\n{corr_matrix}")13print(f"Correlation coefficient: {corr_matrix[0, 1]:.4f}")4. Probability Distributions
4.1 Normal Distribution (Phân phối chuẩn)
Trong đó:
- : Mean (trung bình)
- : Standard deviation (do lech chuan)
Hinh: Normal Distribution voi cac gia tri sigma khac nhau
4.2 Thực hành
Python
1import numpy as np2import matplotlib.pyplot as plt3from scipy import stats45# Tao data tu normal distribution6mu, sigma = 0, 17data = np.random.normal(mu, sigma, 1000)89# Visualize10plt.figure(figsize=(10, 4))11plt.hist(data, bins=30, density=True, alpha=0.7)12x = np.linspace(-4, 4, 100)13plt.plot(x, stats.norm.pdf(x, mu, sigma), 'r-', lw=2)14plt.title('Normal Distribution')15plt.xlabel('Value')16plt.ylabel('Density')17plt.show()5. Feature Scaling
5.1 Tại sao cần Scaling?
| Vấn đề | Giải pháp |
|---|---|
| Features co scale khac nhau | Standardization |
| Gradient descent hội tụ chậm | Feature scaling |
| Distance-based models bi bias | Normalization |
5.2 Cac phương pháp Scaling
Standardization (Z-score):
Min-Max Normalization:
5.3 Thực hành
Python
1from sklearn.preprocessing import StandardScaler, MinMaxScaler2import numpy as np34# Data5X = np.array([[1, 200],6 [2, 400],7 [3, 600],8 [4, 800]])910# StandardScaler11scaler_std = StandardScaler()12X_std = scaler_std.fit_transform(X)13print(f"Standardized:\n{X_std}")14print(f"Mean: {X_std.mean(axis=0)}")15print(f"Std: {X_std.std(axis=0)}")1617# MinMaxScaler18scaler_mm = MinMaxScaler()19X_mm = scaler_mm.fit_transform(X)20print(f"Min-Max Normalized:\n{X_mm}")21print(f"Min: {X_mm.min(axis=0)}")22print(f"Max: {X_mm.max(axis=0)}")Ưu và nhược điểm cac phương pháp Scaling
| Phuong phap | Uu diem | Nhuoc diem |
|---|---|---|
| StandardScaler | Không bị ảnh hưởng bởi outliers nhieu | Giá trị khong giới hạn |
| MinMaxScaler | Giá trị trong [0,1] | Rat nhay voi outliers |
| RobustScaler | Không bị ảnh hưởng bởi outliers | Ít phổ biến |
Bài tập tự luyện
- Bài tập 1: Tinh mean, variance, std cua data: [10, 20, 30, 40, 50]
- Bài tập 2: Tinh correlation giua X = [1,2,3,4,5] va Y = [5,4,3,2,1]
- Bài tập 3: Áp dụng StandardScaler va MinMaxScaler cho một dataset
