모든 챌린지
쉬움 0명 해결 정답률 0%

Z-Score Standardization

Center a feature at 0 and scale it to a standard deviation of 1.

배우게 될 내용

  • Compute the mean and standard deviation
  • Center and scale a feature
  • Handle zero variance

문제

번역본 보기

Standardization rescales a feature so it has mean 0 and standard deviation 1, which many models (and gradient descent) work best with:

z = (x - mean) / std

Use the population standard deviation: std = √(Σ(x - mean)² / n). If std is 0 (all values equal), return 0 for every value.

Implement z_scores(values).

Input

values: The values of one feature.

Output

Return the z-score of each value, in the same order.

제약 조건

1 ≤ length ≤ 10,000

-1,000,000 ≤ each value ≤ 1,000,000

예시

예시 1
입력:
values = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]

예상 출력:
expected = [-1.5, -0.5, -0.5, -0.5, 0.0, 0.0, 1.0, 2.0]
입력:
std::vector<double> values = {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};

예상 출력:
std::vector<double> expected = {-1.5, -0.5, -0.5, -0.5, 0.0, 0.0, 1.0, 2.0};
입력:
double[] values = new double[] {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};

예상 출력:
double[] expected = new double[] {-1.5, -0.5, -0.5, -0.5, 0.0, 0.0, 1.0, 2.0};

설명

The mean is 40 / 8 = 5 and the population standard deviation is √(32 / 8) = 2, so 2 → (2 - 5) / 2 = -1.5, 4 → -0.5, 5 → 0, 7 → 1 and 9 → 2. Result: [-1.5, -0.5, -0.5, -0.5, 0, 0, 1, 2].

예시 2
입력:
values = [3.0, 3.0]

예상 출력:
expected = [0.0, 0.0]
입력:
std::vector<double> values = {3.0, 3.0};

예상 출력:
std::vector<double> expected = {0.0, 0.0};
입력:
double[] values = new double[] {3.0, 3.0};

예상 출력:
double[] expected = new double[] {0.0, 0.0};

설명

Both values equal the mean, so the standard deviation is 0 and both z-scores are 0.

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.