Lời mở đầu
Trong Machine Learning, thuật toán hiếm khi là vấn đề. Dữ liệu và cách biểu diễn dữ liệu mới là yếu tố quyết định. Bài viết này phân tích một mô hình Binary Classification đơn giản trong ML.NET để chỉ ra:
- Vì sao Accuracy có thể gây hiểu nhầm
- Vì sao Recall 100% có thể là dấu hiệu nguy hiểm
- Vì sao Feature Engineering mới là đòn bẩy thực sự
1. Bài toán
Ta có dữ liệu:
Duration– thời gian khách ở lại websiteIsPurchased– có mua hàng hay không
public class CustomerData
{
[LoadColumn(0)]
public float Duration { get; set; }
[LoadColumn(1)]
public bool IsPurchased { get; set; }
}
Mục tiêu: Dự đoán IsPurchased.
2. Pipeline tối giản (và vấn đề của nó)
var context = new MLContext();
var data = context.Data.LoadFromTextFile<CustomerData>(
"data.csv", hasHeader: true, separatorChar: ',');
var split = context.Data.TrainTestSplit(data, testFraction: 0.2);
var pipeline = context.Transforms
.Concatenate("Features", nameof(CustomerData.Duration))
.Append(context.BinaryClassification.Trainers.LbfgsLogisticRegression());
var model = pipeline.Fit(split.TrainSet);
Evaluate model
var predictions = model.Transform(split.TestSet);
var metrics = context.BinaryClassification.Evaluate(predictions);
Console.WriteLine($"Accuracy: {metrics.Accuracy}");
Console.WriteLine($"Precision: {metrics.PositivePrecision}");
Console.WriteLine($"Recall: {metrics.PositiveRecall}");
Console.WriteLine($"F1Score: {metrics.F1Score}");
3. Khi Recall = 100% nhưng model vẫn tệ
Giả sử kết quả:
Accuracy: 0.53
Precision: 0.41
Recall: 1.00
Recall
$$ Recall = \frac{True\ Positive}{True\ Positive + False\ Negative} $$Recall 100% nghĩa là model không bỏ sót bất kỳ người mua nào.
Precision
$$ Precision = \frac{True\ Positive}{True\ Positive + False\ Positive} $$Precision 41% nghĩa là trong 100 người model dự đoán “sẽ mua”, chỉ 41 người thực sự mua.
Điều này thường xảy ra khi model dự đoán tất cả mọi người đều mua.
- Không có False Negative → Recall = 100%
- Nhiều False Positive → Precision thấp
- Accuracy chỉ hơn 50%
Đây không phải model thông minh. Đây là model đoán bừa nhưng an toàn.
4. Vấn đề không nằm ở thuật toán
Ta dùng Logistic Regression – một thuật toán tuyến tính chuẩn.
Vấn đề là ta chỉ cung cấp đúng 1 feature: Duration.
Model không có đủ thông tin để tìm ranh giới phân loại tốt.
5. Feature Engineering – Thay đổi không gian đặc trưng
Ta thêm một feature mới: phiên truy cập dài hơn 300 giây.
public class ModelInput
{
public float Duration { get; set; }
public float IsLongSession { get; set; }
[ColumnName("Label")]
public bool IsPurchased { get; set; }
}
CustomMapping để tạo feature
var pipeline = context.Transforms.CustomMapping<CustomerData, ModelInput>(
(input, output) =>
{
output.Duration = input.Duration;
output.IsLongSession = input.Duration > 300 ? 1f : 0f;
output.IsPurchased = input.IsPurchased;
},
contractName: "CustomerMapping")
.Append(context.Transforms.Concatenate(
"Features",
nameof(ModelInput.Duration),
nameof(ModelInput.IsLongSession)))
.Append(context.BinaryClassification.Trainers.LbfgsLogisticRegression());
Train và Evaluate lại
var model = pipeline.Fit(split.TrainSet);
var predictions = model.Transform(split.TestSet);
var metrics = context.BinaryClassification.Evaluate(predictions);
Console.WriteLine($"Accuracy: {metrics.Accuracy}");
Console.WriteLine($"Precision: {metrics.PositivePrecision}");
Console.WriteLine($"Recall: {metrics.PositiveRecall}");
Console.WriteLine($"F1Score: {metrics.F1Score}");
6. Insight quan trọng
- Accuracy không đủ để đánh giá model.
- Recall cao không đồng nghĩa model tốt.
- Thuật toán thường không phải vấn đề đầu tiên.
- Feature Engineering thay đổi hình học của không gian dữ liệu.
Khi bạn thêm feature, bạn đang thay đổi không gian vector mà thuật toán nhìn thấy. Điều đó có thể quan trọng hơn việc đổi sang một thuật toán phức tạp hơn.
Kết luận
Trong ML.NET (và Machine Learning nói chung), trước khi tìm thuật toán tốt hơn, hãy tự hỏi:
Dữ liệu của tôi đã đủ thông tin để máy học phân biệt chưa?
Nhận xét
Đăng nhận xét