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

What is machine learning? मशीन लर्निंग क्या है?

Lesson 1 of 13 · about 12 minutes · Examples instead of rules; features and labels.

⚡ 0 XP

Step 1 of 2

Rules vs examples

weight gsweet 1–10label1808🥭 mango602🍋 lemon2109🥭 mango

Normal programming: you write the rules. if (sweetness > 6) return "mango";

Machine learning: you give the computer many examples with answers, and it finds the rule itself. That “found rule” is called a model.

In this course you will build tiny models in plain JavaScript — no libraries, no magic. You need a little JavaScript: arrays, objects, loops and functions. Rusty? Revise with our JavaScript course. Never coded at all? Start with AI Shiksha, our no-code AI literacy course, and come back later.

Example
// A hand-written rule (no learning yet)
function guess(fruit) {
  if (fruit.sweetness > 6) return "mango";
  return "lemon";
}
console.log(guess({ weight: 220, sweetness: 9 }));
console.log(guess({ weight: 60, sweetness: 2 }));

Soon the computer will find rules like this from examples.

script.jsCtrl ⏎
Console

Output from console.log appears here.

Lesson notes (text version)

1. Rules vs examples

Normal programming: you write the rules. if (sweetness > 6) return "mango";

Machine learning: you give the computer many examples with answers, and it finds the rule itself. That “found rule” is called a model.

In this course you will build tiny models in plain JavaScript — no libraries, no magic. You need a little JavaScript: arrays, objects, loops and functions. Rusty? Revise with our JavaScript course. Never coded at all? Start with AI Shiksha, our no-code AI literacy course, and come back later.

                // A hand-written rule (no learning yet)
function guess(fruit) {
  if (fruit.sweetness > 6) return "mango";
  return "lemon";
}
console.log(guess({ weight: 220, sweetness: 9 }));
console.log(guess({ weight: 60, sweetness: 2 }));
              

2. Features and labels

Data is usually a table. Each row is one example; each column is a fact about it.

  • Features are the inputs we can measure: weight in grams, sweetness out of 10
  • The label is the answer we want to predict: "mango" or "lemon"

In JavaScript a table is an array of objects — one object per row:

{ weight: 210, sweetness: 9, label: "mango" }

The model only ever sees features while predicting; labels are the answers it learns from.

Challenge: Your first dataset

Make a made-up dataset fruits: an array of at least 6 objects, each with a number weight, a number sweetness and a label that is "mango" or "lemon". Include at least 2 of each label.

Then write labelsOf(rows) that returns an array of just the labels, in order.

Search by task, format or tool name.