すべてのチャレンジ
簡単 0件解決済み 正答率0%

Logistic Regression Prediction

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

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

-100 ≤ each feature, weight and bias ≤ 100

0 < threshold < 1

例

例 1
入力:
bias = -1.0
samples = [
    [2.0, 1.0],
    [0.0, 0.0],
    [-1.0, -3.0]
]
weights = [1.5, -0.5]
threshold = 0.5

期待される出力:
expected = [1, 0, 0]
入力:
double bias = -1.0;
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 threshold = 0.5;

期待される出力:
std::vector<long long> expected = {1, 0, 0};
入力:
double bias = -1.0;
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 threshold = 0.5;

期待される出力:
long[] expected = new long[] {1, 0, 0};

解説

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].

例 2
入力:
bias = -2.0
samples = [
    [1.0],
    [2.0],
    [3.0]
]
weights = [1.0]
threshold = 0.7

期待される出力:
expected = [0, 0, 1]
入力:
double bias = -2.0;
std::vector<std::vector<double>> samples = {
    {1.0},
    {2.0},
    {3.0}
};
std::vector<double> weights = {1.0};
double threshold = 0.7;

期待される出力:
std::vector<long long> expected = {0, 0, 1};
入力:
double bias = -2.0;
double[][] samples = new double[][] {
    new double[] {1.0},
    new double[] {2.0},
    new double[] {3.0}
};
double[] weights = new double[] {1.0};
double threshold = 0.7;

期待される出力:
long[] expected = new long[] {0, 0, 1};

解説

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].

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。