簡単 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

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