Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Logistic Regression Prediction

Classify samples as 0 or 1 by passing a weighted sum through the sigmoid and comparing it to a threshold.

Bạn sẽ học được gì

  • Turn a weighted sum into a probability
  • Classify with a decision threshold
  • Understand binary classifiers

Đề bài

Xem bản dịch

Logistic regression is a linear model for yes/no questions. For each sample it computes a probability and turns it into a class:

  1. z = x₁×w₁ + … + xₙ×wₙ + bias
  2. p = 1 / (1 + e^(-z))
  3. class = 1 if p ≥ threshold, otherwise 0

Implement logistic_predict(samples, weights, bias, threshold).

Input

samples: One row of n features per sample.

weights: n weights.

bias: The bias.

threshold: The probability needed to predict class 1 (strictly between 0 and 1).

Output

Return the predicted class (0 or 1) of each sample, in order.

Ràng buộc

1 ≤ samples ≤ 1,000, 1 ≤ n ≤ 50

-100 ≤ each feature, weight and bias ≤ 100

0 < threshold < 1

Ví dụ

Ví dụ 1
Đầu vào:
samples = [
    [2.0, 1.0],
    [0.0, 0.0],
    [-1.0, -3.0]
]
weights = [1.5, -0.5]
bias = -1.0
threshold = 0.5

Kết quả mong đợi:
expected = [1, 0, 0]
Đầu vào:
std::vector<std::vector<double>> samples = {
    {2.0, 1.0},
    {0.0, 0.0},
    {-1.0, -3.0}
};
std::vector<double> weights = {1.5, -0.5};
double bias = -1.0;
double threshold = 0.5;

Kết quả mong đợi:
std::vector<long long> expected = {1, 0, 0};
Đầu vào:
double[][] samples = new double[][] {
    new double[] {2.0, 1.0},
    new double[] {0.0, 0.0},
    new double[] {-1.0, -3.0}
};
double[] weights = new double[] {1.5, -0.5};
double bias = -1.0;
double threshold = 0.5;

Kết quả mong đợi:
long[] expected = new long[] {1, 0, 0};

Giải thích

Sample 1: z = 3 - 0.5 - 1 = 1.5, p ≈ 0.818 → 1. Sample 2: z = -1, p ≈ 0.269 → 0. Sample 3: z = -1.5 + 1.5 - 1 = -1, p ≈ 0.269 → 0. Result: [1, 0, 0].

Ví dụ 2
Đầu vào:
samples = [
    [1.0],
    [2.0],
    [3.0]
]
weights = [1.0]
bias = -2.0
threshold = 0.7

Kết quả mong đợi:
expected = [0, 0, 1]
Đầu vào:
std::vector<std::vector<double>> samples = {
    {1.0},
    {2.0},
    {3.0}
};
std::vector<double> weights = {1.0};
double bias = -2.0;
double threshold = 0.7;

Kết quả mong đợi:
std::vector<long long> expected = {0, 0, 1};
Đầu vào:
double[][] samples = new double[][] {
    new double[] {1.0},
    new double[] {2.0},
    new double[] {3.0}
};
double[] weights = new double[] {1.0};
double bias = -2.0;
double threshold = 0.7;

Kết quả mong đợi:
long[] expected = new long[] {0, 0, 1};

Giải thích

The probabilities are σ(-1) ≈ 0.269, σ(0) = 0.5 and σ(1) ≈ 0.731. Only the last one reaches the 0.7 threshold, so the result is [0, 0, 1].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

For each sample, first compute the linear score from features, weights, and bias.

Gợi ý 2

Convert that score into a probability with a sigmoid, then compare it to the threshold.

Gợi ý 3

Very large positive or negative scores can saturate the sigmoid, but the class decision still comes from the threshold.

Độ phức tạp mong đợi

O(m × n) time, O(1) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.