ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก