문제
로지스틱 회귀는 예/아니오 문제를 위한 선형 모델입니다. 각 샘플에 대해 확률을 계산한 뒤 클래스로 바꿉니다.
- z = x₁×w₁ + … + xₙ×wₙ + bias
- p = 1 / (1 + e^(-z))
- p ≥ threshold이면 클래스 1, 아니면 0
logistic_predict(samples, weights, bias, threshold)를 구현하세요.
입력
samples: 샘플마다 n개의 특성으로 된 행.
weights: n개의 가중치.
bias: 편향.
threshold: 클래스 1로 예측하는 데 필요한 확률 (0과 1 사이, 양 끝 제외).
출력
각 샘플의 예측 클래스(0 또는 1)를 순서대로 반환하세요.
제약 조건
1 ≤ 샘플 수 ≤ 1,000, 1 ≤ n ≤ 50
-100 ≤ 각 특성, 가중치, 편향 ≤ 100
0 < threshold < 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};
설명
샘플 1: z = 3 - 0.5 - 1 = 1.5, p ≈ 0.818 → 1. 샘플 2: z = -1, p ≈ 0.269 → 0. 샘플 3: z = -1.5 + 1.5 - 1 = -1, p ≈ 0.269 → 0. 결과: [1, 0, 0].
입력:
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};
설명
확률은 σ(-1) ≈ 0.269, σ(0) = 0.5, σ(1) ≈ 0.731입니다. 임곗값 0.7에 도달하는 것은 마지막뿐이므로 결과는 [0, 0, 1]입니다.
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.