簡単 1件解決済み 正答率100.0%

Min-Max Normalization

Rescale a feature so its smallest value becomes 0 and its largest becomes 1.

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

  • Rescale values to the 0-1 range
  • Handle a feature where all values are equal
  • Prepare data for training

問題

翻訳を表示

Features measured on different scales (age in years, income in dollars) confuse many models. Min-max normalization rescales a feature to the range 0 to 1:

x' = (x - min) / (max - min)

If all values are equal (max = min), return 0 for every value.

Implement min_max_normalize(values).

Input

values: The values of one feature.

Output

Return the normalized values in the same order.

制約

1 ≤ length ≤ 10,000

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

例

例 1
入力:
values = [10.0, 20.0, 15.0, 30.0]

期待される出力:
expected = [0.0, 0.5, 0.25, 1.0]
入力:
std::vector<double> values = {10.0, 20.0, 15.0, 30.0};

期待される出力:
std::vector<double> expected = {0.0, 0.5, 0.25, 1.0};
入力:
double[] values = new double[] {10.0, 20.0, 15.0, 30.0};

期待される出力:
double[] expected = new double[] {0.0, 0.5, 0.25, 1.0};

解説

min = 10 and max = 30, so the range is 20: (10 - 10) / 20 = 0, (20 - 10) / 20 = 0.5, (15 - 10) / 20 = 0.25 and (30 - 10) / 20 = 1.

例 2
入力:
values = [5.0, 5.0, 5.0]

期待される出力:
expected = [0.0, 0.0, 0.0]
入力:
std::vector<double> values = {5.0, 5.0, 5.0};

期待される出力:
std::vector<double> expected = {0.0, 0.0, 0.0};
入力:
double[] values = new double[] {5.0, 5.0, 5.0};

期待される出力:
double[] expected = new double[] {0.0, 0.0, 0.0};

解説

All values are equal, so the range is 0 and every value becomes 0.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

First find the smallest and largest values, since every normalized result depends on the feature’s spread.

ヒント 2

Compute each output from its position relative to the minimum, using one pass to build the results after scanning extremes.

ヒント 3

If every value is identical, avoid dividing by zero and return zeros for all positions.

目安の計算量

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

参考資料

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

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 RCRiccardo Corti Python 838 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が1件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。