Machine Learning Cơ Bản Cho Data Analyst: Regression, Classification Và Clustering
Chia sẻ
"Hướng dẫn 3 thuật toán Machine Learning cơ bản mà Data Analyst nên biết: Linear Regression, Logistic Classification và K-Means Clustering — áp dụng thực tế với Python scikit-learn."
1. Machine Learning Cho Data Analyst: Bắt Đầu Từ Đâu?
Machine Learning (ML) không chỉ dành cho Data Scientist. Data Analyst cần hiểu ML để: 1) Dự báo doanh thu, churn rate 2) Phân loại khách hàng tự động 3) Phát hiện bất thường (fraud detection) 4) Đánh giá mô hình do DS team tạo.
Bài viết hướng dẫn 3 loại ML cốt lõi: Regression (dự đoán số), Classification (phân loại), và Clustering (phân nhóm) — với code Python sklearn chạy được ngay.
2. Regression — Dự Đoán Giá Trị Liên Tục
Linear Regression dự đoán giá trị số: doanh thu tháng tới, giá nhà, lượng hàng tồn cần order. Input: features (biến đầu vào). Output: giá trị số liên tục. Đánh giá: R² score (0-1, càng gần 1 càng tốt), MAE, RMSE.
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_absolute_error
import pandas as pd
df = pd.read_csv("sales.csv")
X = df[["month", "marketing_budget", "num_employees"]]
y = df["revenue"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"R² = {r2_score(y_test, y_pred):.3f}")
print(f"MAE = {mean_absolute_error(y_test, y_pred):,.0f}")3. Classification — Phân Loại
Classification dự đoán category: email spam/not spam, khách hàng churn/không churn, giao dịch fraud/legit. Algorithms phổ biến: Logistic Regression, Decision Tree, Random Forest. Đánh giá: Accuracy, Precision, Recall, F1-Score.
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
X = df[["total_orders", "avg_order_value", "days_since_last_order"]]
y = df["churned"] # 0 or 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))4. Clustering — Phân Nhóm Không Giám Sát
Clustering tìm nhóm tự nhiên trong data MÀ KHÔNG CÓ label trước. Ứng dụng: phân khúc khách hàng (VIP/Regular/Dormant), nhóm sản phẩm tương tự, phát hiện anomaly. K-Means là algorithm đơn giản nhất.
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
features = df[["total_spent", "frequency", "recency"]]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(features)
kmeans = KMeans(n_clusters=4, random_state=42)
df["segment"] = kmeans.fit_predict(X_scaled)
print(df.groupby("segment").agg({"total_spent": "mean", "frequency": "mean"}))5. Model Evaluation — Đánh Giá Chính Xác
KHÔNG BAO GIỜ chỉ nhìn accuracy. Confusion Matrix cho biết True Positive, False Positive, True Negative, False Negative. Precision = TP/(TP+FP) — quan trọng khi false positive costly (spam filter). Recall = TP/(TP+FN) — quan trọng khi false negative costly (fraud detection).
Cross-validation: chia data thành k folds, train k lần, lấy trung bình. Tránh overfitting — model quá fit training data nhưng predict tệ data mới. Dùng cross_val_score thay vì single train/test split.
6. Feature Engineering — Nghệ Thuật Tạo Features
Model chỉ tốt bằng features input. Tips: 1) Tạo features từ dates (day_of_week, month, is_weekend). 2) Log transform cho skewed data. 3) One-hot encoding cho categorical. 4) Feature importance để loại feature không hữu ích. Feature engineering chiếm 70% effort trong ML project.
7. ML Workflow Cho Data Analyst
1) Define problem → 2) Collect & clean data → 3) EDA → 4) Feature engineering → 5) Train model → 6) Evaluate → 7) Iterate. Bắt đầu simple (Linear/Logistic Regression) → thêm complexity nếu cần. 80% business problems giải quyết được bằng simple models.
8. Kết Luận
Data Analyst không cần deep learning. Nắm Linear Regression, Random Forest Classification, K-Means Clustering + sklearn pipeline là đủ giải 80% bài toán prediction/segmentation. Focus vào feature engineering và model evaluation hơn là thử nhiều algorithms.
Bình luận
Đăng nhập để tham gia bình luận
Đăng nhậpNhận bài viết mới nhất
Đăng ký để nhận thông báo khi có bài viết mới. Không spam, chỉ kiến thức chất lượng.
Bài viết liên quan
Khám phá thêm các bài viết cùng chủ đề

