Tổng ket khoa hoc ML Fundamentals
Tổng quan kien thuc
Cấu trúc khoa hoc
| Chủ đề | Nội dung chính |
|---|---|
| Supervised Learning | |
| - Regression | Linear Regression, Polynomial Regression |
| - Classification | Logistic Regression, Decision Tree, Random Forest, XGBoost |
| Unsupervised Learning | |
| - Clustering | K-Means |
| - Dimensionality Reduction | PCA |
| - Anomaly Detection | Isolation Forest |
| Evaluation | |
| - Metrics | Accuracy, Precision, Recall, F1-Score, ROC-AUC |
| - Validation | Cross-Validation, GridSearchCV |
| Data Processing | |
| - Preprocessing | Feature Scaling, Missing Values, Encoding |
Hinh: Scikit-learn Algorithm Cheat Sheet
Checklist kiến thức
1. Preprocessing
- Xử lý missing values (imputation)
- Feature scaling (StandardScaler, MinMaxScaler)
- Encoding categorical (OneHot, Label)
- Train/Test split đúng cách
2. Algorithms
- Linear Regression:
- Logistic Regression:
- Decision Tree: Gini Impurity, Information Gain
- K-Means: Inertia, Elbow Method
- Random Forest, XGBoost
3. Evaluation
- Confusion Matrix: TP, TN, FP, FN
- Precision =
- Recall =
- F1 =
- ROC-AUC, PR Curve
4. Model Selection
- Cross-Validation (K-Fold, Stratified)
- GridSearchCV, RandomizedSearchCV
- Bias-Variance Tradeoff
Bài kiểm tra cuối khóa
Phần 1: Trắc nghiệm (40 điểm)
Câu 1: Trong bài toán classification với 100 samples, model dự đoán dung 80 positive va 5 negative, sai 10 positive va 5 negative. Precision là:
A. 0.80
B. 0.89
C. 0.94
D. 0.85
Câu 2: Khi nào nên dùng F1-Score thay vì Accuracy?
A. Dataset cân bằng
B. Dataset imbalanced
C. Chỉ quan tâm positive class
D. Cả B và C
Câu 3: Công thức Gini Impurity là:
A.
B.
C.
D.
Câu 4: Trong K-Means, Elbow Method dùng để:
A. Tính Silhouette Score
B. Chọn so clusters K
C. Khởi tạo centroids
D. Đánh giá convergence
Câu 5: Sigmoid function có output trong khoảng:
A.
B.
C.
D.
Câu 6: Random Forest khac Bagging o điểm nao?
A. Sử dụng bootstrap sampling
B. Kết hợp nhiều trees
C. Random feature selection tại mỗi split
D. Voting để predict
Câu 7: Overfitting xảy ra khi:
A. Train error cao, test error cao
B. Train error thấp, test error cao
C. Train error cao, test error thấp
D. Train error thấp, test error thấp
Câu 8: PCA chọn principal components dựa trên:
A. Minimize variance
B. Maximize variance
C. Random selection
D. User input
Câu 9: XGBoost cải tiến so với Gradient Boosting bằng cách:
A. Loại bỏ regularization
B. Thêm L1, L2 regularization
C. Chi dùng đểcision tree
D. Không dùng learning rate
Câu 10: Stratified K-Fold nên dùng khi:
A. Regression problem
B. Classification voi imbalanced classes
C. Time series data
D. Small dataset
Phần 2: Tu luan (60 điểm)
Câu 11 (15 điểm): Cho confusion matrix:
| Predicted 0 | Predicted 1 | |
|---|---|---|
| Actual 0 | 85 | 15 |
| Actual 1 | 10 | 90 |
Tính:
a) Accuracy
b) Precision (class 1)
c) Recall (class 1)
d) F1-Score (class 1)
Câu 12 (15 điểm): Cho data points:
| Point | X | Y |
|---|---|---|
| A | 1 | 2 |
| B | 2 | 1 |
| C | 4 | 5 |
| D | 5 | 4 |
Voi K-Means (K=2), khởi tạo:
- Centroid 1 = A = (1, 2)
- Centroid 2 = D = (5, 4)
a) Gan moi điểm vao cluster (Iteration 1)
b) Cap nhat centroids
c) Tính Inertia sau iteration 1
Câu 13 (15 điểm): Giải thích su khac biet giữa:
a) Bagging vs Boosting
b) Precision vs Recall
c) ROC-AUC vs PR-AUC
Câu 14 (15 điểm): Viet code Python hoan chinh de:
- Load dataset (co the dung sklearn datasets)
- Preprocess data (scale, split)
- Train Random Forest
- Tune hyperparameters voi GridSearchCV
- Evaluate voi cross-validation
- In confusion matrix va classification report
Dap an tham khao
Trắc nghiệm
| Câu | Dap an | Giải thích |
|---|---|---|
| 1 | B | TP=80, FP=5. Precision = 80/(80+5) = 0.89 |
| 2 | D | F1 huu ich khi imbalanced va focus positive |
| 3 | A | Công thức Gini chuan |
| 4 | B | Elbow tìm K tối ưu |
| 5 | B | Sigmoid output (0,1) |
| 6 | C | RF them feature randomness |
| 7 | B | High variance = overfitting |
| 8 | B | PCA maximize variance |
| 9 | B | XGBoost co regularization |
| 10 | B | Stratified cho classification imbalanced |
Câu 11
a) Accuracy =
b) Precision =
c) Recall =
d) F1 =
Câu 12
a) Khoang cach:
| Point | d(C1) | d(C2) | Cluster |
|---|---|---|---|
| A | 0 | 4.47 | C1 |
| B | 1.41 | 4.24 | C1 |
| C | 4.24 | 1.41 | C2 |
| D | 4.47 | 0 | C2 |
b) New centroids:
- C1 = ((1+2)/2, (2+1)/2) = (1.5, 1.5)
- C2 = ((4+5)/2, (5+4)/2) = (4.5, 4.5)
c) Inertia:
Câu 14 - Code mau
1from sklearn.datasets import load_breast_cancer2from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score3from sklearn.preprocessing import StandardScaler4from sklearn.ensemble import RandomForestClassifier5from sklearn.metrics import classification_report, confusion_matrix6import numpy as np78# 1. Load data9data = load_breast_cancer()10X, y = data.data, data.target1112# 2. Preprocess13scaler = StandardScaler()14X_scaled = scaler.fit_transform(X)1516X_train, X_test, y_train, y_test = train_test_split(17 X_scaled, y, test_size=0.2, random_state=42, stratify=y18)1920# 3. Train Random Forest21rf = RandomForestClassifier(random_state=42)2223# 4. GridSearchCV24param_grid = {25 'n_estìmators': [50, 100, 200],26 'max_depth': [5, 10, None],27 'min_samples_split': [2, 5]28}2930grid_search = GridSearchCV(rf, param_grid, cv=5, scoring='f1', n_jobs=-1)31grid_search.fit(X_train, y_train)3233print(f"Best params: {grid_search.best_params_}")34print(f"Best CV F1: {grid_search.best_score_:.4f}")3536# 5. Cross-validation37best_model = grid_search.best_estìmator_38cv_scores = cross_val_score(best_model, X_train, y_train, cv=5, scoring='f1')39print(f"\nCV Scores: {cv_scores}")40print(f"Mean: {cv_scores.mean():.4f} (+/- {cv_scores.std()*2:.4f})")4142# 6. Evaluate on test set43y_pred = best_model.predict(X_test)4445print("\n=== Confusion Matrix ===")46print(confusion_matrix(y_test, y_pred))4748print("\n=== Classification Report ===")49print(classification_report(y_test, y_pred, target_names=data.target_names))Chuc mung hoan thanh khoa hoc!
Ban da nam vung cac kien thuc nen tang ve Machine Learning. Tiep theo:
- Thuc hanh: Lam cac projects thuc te
- Kaggle: Tham gia competitions
- Deep Learning: Hoc tiep Neural Networks
- Chuyen sau: NLP, Computer Vision, Time Series
Hen gap lai o cac khoa hoc tiep theo!
