Code / AI & ML Basics / Unit 1: Data: what models learn from

Summaries and scaling सारांश और स्केलिंग

Lesson 2 of 13 · about 14 minutes · Mean, min, max and normalising to 0–1.

⚡ 0 XP

Step 1 of 2

Mean, min and max

78[0]91[1]65[2]88[3]95[4]marks = [78, 91, 65, 88, 95]

Before training anything, look at your data. Three quick summaries of a column:

  • mean (average): add everything, divide by how many
  • min: the smallest value
  • max: the largest value

They catch mistakes early. If the max weight of a mango is 9000 g, someone typed an extra zero!

To find the max, start with the first value and walk through the rest, keeping the biggest you have seen.

Example
const marks = [62, 75, 48, 90];
let total = 0;
for (const m of marks) total += m;
console.log("mean", total / marks.length);

Prints: mean 68.75

🎯 Challenge: Report-card summary +10 XP

Write three functions that take an array of numbers:

  • mean(xs) returns the average
  • minOf(xs) returns the smallest value
  • maxOf(xs) returns the largest value

You may use a loop or Math.min(...xs). Arrays will never be empty.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • mean([62, 75, 48, 90]) is 68.75
  • minOf finds the smallest, even negative
  • maxOf finds the largest
Lesson notes (text version)

1. Mean, min and max

Before training anything, look at your data. Three quick summaries of a column:

  • mean (average): add everything, divide by how many
  • min: the smallest value
  • max: the largest value

They catch mistakes early. If the max weight of a mango is 9000 g, someone typed an extra zero!

To find the max, start with the first value and walk through the rest, keeping the biggest you have seen.

                const marks = [62, 75, 48, 90];
let total = 0;
for (const m of marks) total += m;
console.log("mean", total / marks.length);
              

Challenge: Report-card summary

Write three functions that take an array of numbers:

  • mean(xs) returns the average
  • minOf(xs) returns the smallest value
  • maxOf(xs) returns the largest value

You may use a loop or Math.min(...xs). Arrays will never be empty.

2. Normalising: squeeze into 0–1

Weight goes from 50 to 300 grams, sweetness from 1 to 10. If we compare fruits with these raw numbers, weight shouts and sweetness whispers — a 10-gram difference looks bigger than the whole sweetness scale!

Normalising puts every feature on the same 0–1 scale:

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

  • the smallest value becomes 0
  • the largest becomes 1
  • everything else lands in between

Careful: if all values are equal, max - min is 0 and you would divide by zero. In that case return 0 for every value.

                const w = [50, 175, 300];
const lo = Math.min(...w), hi = Math.max(...w);
console.log(w.map((x) => (x - lo) / (hi - lo)));
              

Challenge: Scale it to 0–1

Write normalise(xs) that returns a new array where each value is (x - min) / (max - min). If all values are the same, return an array of zeros (same length).

Search by task, format or tool name.