FOUNDATIONS

What Is Machine Learning?

Machine Learning is a way of teaching a computer to learn useful patterns from data and use those patterns to make predictions or decisions.

THE SIMPLE IDEA
Examples Data
Learning Find patterns
Prediction New data

Let's Start With a Simple Example

Imagine you want a computer to identify whether an email is spam.

You could give the computer many emails that have already been classified:

After seeing many examples, a Machine Learning algorithm can learn patterns that are useful for distinguishing spam from normal emails.

Then Give It a New Email

NEW EMAIL "You have been selected for a free reward."
What should the computer predict?

The computer can use what it learned from previous examples to make a prediction about this new email.

POSSIBLE PREDICTION Spam

Notice What Happened

1 Give examples

The computer receives data from the past.

2 Learn patterns

An algorithm learns useful relationships in that data.

3 Use the learning

The trained model can make predictions on new data.

Does the Computer "Understand" the Email?

Not in the way a human understands an email.

This is an important distinction. The model doesn't need human-like understanding to make a useful prediction.

It learns patterns from the data according to the algorithm and model being used.

Don't think of Machine Learning as magic.

The computer is not suddenly becoming intelligent. It is using data, an algorithm, and a model to produce useful predictions or decisions.

Machine Learning Is Used Everywhere

📧 Spam Detection
🎬 Movie Recommendations
🏠 House Price Prediction
💳 Fraud Detection
REMEMBER Machine Learning uses data to learn useful patterns that can help make predictions or decisions on new data.
But how is this different from normal programming?

To understand why Machine Learning is useful, we first need to compare it with traditional programming.

FOUNDATIONS

Traditional Programming vs Machine Learning

The biggest difference is not that one uses code and the other doesn't. Both use code.

The important difference is where the rules come from.

Traditional Programming

In traditional programming, a developer writes the rules that tell the computer what to do.

INPUT Data
+
RULES Programmed by Developer
OUTPUT Answer

A Simple Example

Suppose you want to determine whether someone is eligible for a discount.

You could explicitly write the rule:

If age ≥ 60 → Give discount

The computer doesn't need to discover this rule. You gave it the rule.

if age >= 60:
    discount = True
else:
    discount = False

This works very well when we already know the rules.

But Some Problems Don't Have Simple Rules

Now consider spam detection.

Could we write a few simple rules that correctly identify every spam email?

Rule 1 If email contains "free" → spam
Rule 2 If email contains "winner" → spam
Rule 3 If email contains "prize" → spam

These rules might catch some spam.

But they will also make mistakes.

A legitimate email might contain the word "free". A spam email might never contain any of those words.

This Is Where Machine Learning Changes the Approach

Instead of manually writing every rule, we can provide examples and let an ML algorithm learn useful patterns from those examples.

INPUT Training Data
+
LEARNING Algorithm
OUTPUT Learned Model

The Direction Has Changed

TRADITIONAL PROGRAMMING
Rules + Data Output

Humans define the rules.

MACHINE LEARNING
Data + Answers Learned Model

The algorithm learns useful rules or relationships from the examples.

Another Example: House Prices

Imagine trying to manually write every rule that determines a house's price.

Location Good location → higher price
Size Larger house → usually higher price
Age Older house → may reduce price
Bedrooms More bedrooms → may increase price

Now combine those with hundreds of other factors: neighborhood, parking, floor, construction quality, nearby schools, market conditions, and so on.

The number of possible combinations becomes enormous.

This is exactly the kind of situation where manually writing every rule becomes impractical.

THE BIG DIFFERENCE Traditional programming: humans explicitly write the rules. Machine Learning: algorithms learn useful relationships from data.

Does Machine Learning Mean We Don't Write Rules?

No.

This is a common beginner misunderstanding.

Developers still write the software, choose the algorithm, prepare the data, define the training process, and decide how the model will be used.

What changes is that we don't have to manually specify every relationship that the model needs to use.

So why do we need Machine Learning?

Traditional programming is excellent when the rules are clear and manageable.

But some real-world problems contain patterns that are too complicated to describe manually.

That's the problem Machine Learning is designed to help with.
FOUNDATIONS

Why Do We Need Machine Learning?

Traditional programming is excellent when we can clearly describe the rules of a problem.

The problem starts when the rules become too complicated, too numerous, or difficult to write manually.

Problem 1: Some Rules Are Easy to Write

Consider a simple banking rule.

EXAMPLE If account balance is below ₹0 → Account is overdrawn.

That's easy to write as a normal program because we know exactly what the rule is.

Problem 2: Some Problems Have Too Many Rules

Now consider spam email detection.

A spam email could contain words such as:

"FREE" "WIN" "PRIZE" "CLICK NOW"

But legitimate emails can contain these words too.

So we would need many more rules:

Sender address
Subject
Email content
Links
Number of images
Writing patterns

And the combinations between these signals can become extremely complicated.

Problem 3: Some Patterns Are Difficult to Describe

Consider recognizing a cat in a photograph.

How would you write a complete set of rules for a computer?

Rule? Has two eyes
Rule? Has two ears
Rule? Has fur
Rule? Has a certain shape

These rules are not enough.

A cat can appear from different angles, under different lighting conditions, with different colors, and in different positions.

Writing rules for every possible image would be extremely difficult.

THE PROBLEM We can write rules for simple problems. But many real-world problems contain patterns that are too complicated to describe manually.

This Is Where Machine Learning Helps

Instead of trying to manually write every possible rule, we can provide the computer with many examples.

EXAMPLES Data

Give the system many examples.

LEARNING Find Patterns

An ML algorithm learns useful relationships.

RESULT Model

Use what was learned on new data.

Another Important Reason: The World Changes

Real-world patterns don't stay exactly the same forever.

For example, the characteristics of spam emails can change. New words, new scams, and new techniques appear over time.

A system that depends entirely on manually written rules may require constant updates.

A Machine Learning system can be retrained using newer data so that the model can adapt to changing patterns.

Where Machine Learning Becomes Useful

📧

Spam Detection

Learn patterns in emails.

👁️

Image Recognition

Learn patterns in images.

💳

Fraud Detection

Learn unusual transaction patterns.

🎬

Recommendations

Learn from user behavior.

But ML Is Not Always the Best Choice

This is important.

You should not use Machine Learning simply because it is available.

If a simple rule solves the problem reliably, a normal program is often easier to build, test, understand, and maintain.

SIMPLE, CLEAR RULES Traditional Programming

Usually the simpler solution.

COMPLEX PATTERNS Machine Learning

Useful when rules are difficult to define manually and sufficient data is available.

Machine Learning is not a replacement for programming.

It is another approach for solving problems where learning patterns from data can be more practical than manually writing every rule.

Now let's solve an actual problem.

Instead of talking about ML abstractly, we're going to take one concrete problem and work through it from beginning to end.

Our first problem: predicting house prices.
OUR FIRST PROBLEM

Finding the Pattern With Simple Math

We have four houses with known sizes and prices. Before using Machine Learning, let's see if we can find the relationship ourselves.

Look at the Data

House Size Price
800 sq ft ₹30 lakh
1000 sq ft ₹40 lakh
1200 sq ft ₹50 lakh
1500 sq ft ₹65 lakh

Notice something interesting.

When the house size increases, the price also increases.

But let's go one step further. Can we measure exactly how the price changes?

Step 1: Compare Two Houses

Take the first two houses:

HOUSE 1 800 sq ft ₹30 lakh
HOUSE 2 1000 sq ft ₹40 lakh

The size increased by:

1000 − 800 = 200 sq ft

The price increased by:

₹40 lakh − ₹30 lakh = ₹10 lakh

Step 2: How Much Price Changes for Each Square Foot?

We know that an additional 200 sq ft corresponds to an additional ₹10 lakh in this simplified dataset.

So the price increase per square foot is:

₹10 lakh ÷ 200 sq ft = ₹0.05 lakh per sq ft

Since ₹0.05 lakh is ₹5,000:

Every additional 1 sq ft corresponds to about ₹5,000 in this simplified example.

Step 3: Can We Write This as an Equation?

Yes.

We can describe the relationship using:

Price = 0.05 × Size − 10

In Machine Learning and mathematics, we often write this more generally as:

y = mx + b

Here:

y Predicted price
x House size
m How quickly price changes
b Starting offset of the line

Let's Check Our Equation

We shouldn't just assume the equation is correct. Let's test it against the houses we already know.

For an 800 sq ft house

Price = 0.05 × 800 − 10 = 40 − 10 = ₹30 lakh

For a 1200 sq ft house

Price = 0.05 × 1200 − 10 = 60 − 10 = ₹50 lakh

Both predictions match our original data.

So What Did We Actually Find?

We found a mathematical relationship between house size and house price.

INPUT House Size
RELATIONSHIP Price = 0.05 × Size − 10
OUTPUT Price

Now We Can Predict a New House

Remember our new house from the previous section?

NEW HOUSE 1400 sq ft

We can put 1400 into our equation.

Price = 0.05 × 1400 − 10 = 70 − 10 = ₹60 lakh
OUR ESTIMATE ₹60 lakh

According to the simple relationship we found, a 1400 sq ft house would be predicted at approximately ₹60 lakh.

We Just Did Something Important

We started with examples.

Then we found a relationship in those examples.

Finally, we used that relationship to predict something we didn't know.

1. Examples Known house sizes and prices
2. Relationship Price = 0.05 × Size − 10
3. Prediction 1400 sq ft → ₹60 lakh
This is the basic idea behind what Machine Learning does.

We used mathematics manually here. Later, we will give the data to a Machine Learning algorithm and let it learn the relationship for us.

One Important Warning

Our example is deliberately simple.

Real house prices do not depend only on size. Location, number of bedrooms, age, condition, parking, neighborhood, and many other factors can affect the price.

That's why real Machine Learning problems are more complicated than this example.

But our simple example gives us the foundation.

We found a relationship that fits the data perfectly. What happens when real-world data doesn't fit a simple relationship so neatly?

Next: Why Predictions Aren't Perfect
OUR FIRST PROBLEM

Why Predictions Aren't Perfect

In our previous example, the numbers were unusually clean. Every house followed almost exactly the same relationship.

Real-world data is rarely that simple.

Let's Make the Data More Realistic

Imagine that we collect information about houses from a real city.

House Size Actual Price
800 sq ft ₹32 lakh
1000 sq ft ₹41 lakh
1200 sq ft ₹47 lakh
1400 sq ft ₹58 lakh
1500 sq ft ₹68 lakh

The pattern is still there: larger houses generally cost more.

But the numbers don't follow one perfectly straight rule.

Why Are the Prices Different?

House size is only one factor that affects price.

Two houses with the same size can have very different prices.

HOUSE A 1200 sq ft

Good location
New construction
Parking available

₹55 lakh
HOUSE B 1200 sq ft

Less convenient location
Older construction
No parking

₹45 lakh

Both houses have the same size. But their prices are different.

This tells us something important:

House size alone cannot explain the entire price.

Other information also matters.

What Other Information Could Matter?

📍 Location Where the house is located
🛏️ Bedrooms Number of bedrooms
🏗️ Age How old the property is
🚗 Parking Available parking space
🏫 Nearby Schools Access to schools and services
🔧 Condition Overall property condition

So What Happens to Our Prediction?

Suppose we use our simple equation from the previous section:

Price = 0.05 × Size − 10

For a 1400 sq ft house:

Price = 0.05 × 1400 − 10 = ₹60 lakh

But imagine the actual selling price was ₹58 lakh.

PREDICTED ₹60 lakh
vs
ACTUAL ₹58 lakh

Our prediction was not exactly correct.

But that does not automatically mean the prediction was useless.

Prediction vs Reality

Machine Learning usually works with patterns rather than perfect rules.

The goal is often to find a relationship that makes useful predictions, even though the prediction may not exactly match reality every time.

REAL WORLD Actual Price
MODEL Estimated Price
DIFFERENCE Prediction Error

Does More Data Solve Everything?

Not necessarily.

More examples can help a model learn a better pattern, but the quality of the data also matters.

If the data is incomplete, incorrect, biased, or missing important information, the model can still make poor predictions.

Good Machine Learning needs more than lots of data.

We also need useful information and reliable examples.

This Gives Us an Important Insight

In our first example, we only used house size.

But real house prices depend on many things.

So a real Machine Learning system could use several pieces of information together.

A MORE REALISTIC INPUT Size + Location + Bedrooms + Age + Parking + Condition

→ Predict House Price

The simple example was not wrong.

It was intentionally simplified so we could understand the basic idea first.

Real Machine Learning becomes more interesting when we have messy data, multiple factors, and imperfect predictions.

Now we have reached an important question.

If we give a Machine Learning system all these examples, what exactly does the machine learn from them?

Next: What Does the Machine Actually Learn?
HOW ML LEARNS

What Does the Machine Actually Learn?

We keep saying that a Machine Learning system "learns patterns from data."

But what does that actually mean?

Let's go back to our house-price example and look at it more closely.

Remember Our Data

House Size Actual Price
800 sq ft ₹30 lakh
1000 sq ft ₹40 lakh
1200 sq ft ₹50 lakh
1500 sq ft ₹65 lakh

We noticed that larger houses generally have higher prices.

Instead of remembering every house individually, we want to describe the relationship between size and price.

We Can Describe the Relationship With an Equation

In our simplified example, we found:

Price = 0.05 × Size − 10

There are two important numbers in this equation:

0.05 How strongly price changes as house size changes.
−10 The starting offset of the relationship.

These Numbers Describe What Was Learned

This is the important idea.

The machine does not need to memorize:

  • "800 sq ft house costs ₹30 lakh"
  • "1000 sq ft house costs ₹40 lakh"
  • "1200 sq ft house costs ₹50 lakh"
  • "1500 sq ft house costs ₹65 lakh"

Instead, it can learn a relationship that helps explain how house size is related to price.

THE IMPORTANT PART The machine learns values that describe useful relationships in the data.

In this simple example, those values are the numbers used in our equation.

Let's See What Those Numbers Do

Suppose the learned relationship is:

Price = 0.05 × Size − 10

Now give the relationship a new input:

NEW INPUT 1400 sq ft

The learned relationship is used to calculate the prediction:

Price = 0.05 × 1400 − 10 Price = 70 − 10 Price = ₹60 lakh

Notice what happened.

We gave the system a house size it had not seen before, and the learned relationship produced an estimated price.

So What Is the Machine Learning Part?

Up to now, we calculated the relationship ourselves.

We manually decided that:

0.05 × Size − 10

That's not really Machine Learning yet.

The interesting part is when we give the examples to a Machine Learning algorithm and ask it to find useful values for the relationship.

GIVEN Examples

House sizes and their known prices.

LEARN Relationship

Find values that describe the data.

USE Prediction

Apply the learned relationship to new houses.

What If We Had More Information?

Real house prices depend on much more than size.

Suppose we give the system:

Size 1400 sq ft
Bedrooms 3
Age 5 years
Location City center

Now the relationship becomes more complicated.

Instead of one input, the system has several inputs that can influence the prediction.

House Size + Bedrooms + Age + Location Predicted Price

The Machine Doesn't Learn "House Prices"

This is another important distinction.

The machine does not learn a universal rule such as:

"A 1400 sq ft house always costs ₹60 lakh."

That would be wrong.

Instead, it learns a relationship from the particular data it was given.

If the data changes, the learned relationship can also change.

A Simple Way to Think About It

BEFORE LEARNING Examples

We have data, but don't yet know the useful relationship.

AFTER LEARNING Learned Parameters

Values that describe the relationship found in the training data.

NEW DATA Prediction

Use the learned relationship to estimate an output.

REMEMBER Machine Learning learns useful parameters or relationships from examples, rather than simply memorizing the examples.
But where do we keep this learned relationship?

And what do we actually mean when we call something a "Machine Learning model"?

Next: What Is a Model?
HOW ML LEARNS

What Is a Model?

We just saw that Machine Learning can learn a relationship from examples. That learned relationship is what we call a model.

The word "model" can sound complicated, but the basic idea is actually simple.

Let's Go Back to Our House Example

We had examples of house sizes and their prices:

House Size Price
800 sq ft ₹30 lakh
1000 sq ft ₹40 lakh
1200 sq ft ₹50 lakh
1500 sq ft ₹65 lakh

From this data, we found a simplified relationship:

Price = 0.05 × Size − 10

That Relationship Is Our Model

The equation gives us a way to turn a house size into a predicted price.

That's why we can treat it as our simple Machine Learning model.

SIMPLE MODEL Price = 0.05 × Size − 10

Input a house size → get a predicted price.

Let's Use the Model

Suppose a new house is:

NEW INPUT 1400 sq ft

We give that input to our model:

Price = 0.05 × 1400 − 10 Price = 70 − 10 Predicted Price = ₹60 lakh

The model didn't need us to manually create a new rule for the 1400 sq ft house.

It used the relationship it already had to calculate the prediction.

A Model Is Not the Same as the Data

This distinction is important.

The houses we used for learning are the data.

The learned relationship is the model.

DATA Examples

800 sq ft → ₹30L
1000 sq ft → ₹40L
1200 sq ft → ₹50L

LEARN Relationship

Find values that describe the pattern.

MODEL Learned Relationship

Price = 0.05 × Size − 10

Why Do We Need a Model?

Because we want to use what was learned later.

Imagine that our system learned from 100,000 houses. We don't want to go through all 100,000 houses every time someone asks for a prediction.

We want to use the learned result to make predictions quickly.

Training data teaches the system. The model stores the useful result of that learning.

Is a Model Always Just an Equation?

No.

Our equation is only a very simple example. Different Machine Learning problems use different types of models.

A model could represent a mathematical equation, a set of decision rules, a tree of decisions, or a much more complex mathematical structure.

House Price A mathematical relationship
Spam Detection A learned decision boundary or classification model
Image Recognition A complex learned representation of visual patterns

Model vs Prediction

These two terms are often confused by beginners.

The model is what was learned.

The prediction is the answer produced when we give new data to that model.

MODEL Price = 0.05 × Size − 10

The learned relationship.

NEW DATA 1400 sq ft

Input the model has not seen before.

PREDICTION ₹60 lakh

The model's estimated output.

The model is not the prediction.

The model is the learned relationship that produces predictions when new data is provided.

One More Important Point

A model is not automatically correct just because it has learned something.

A model can learn a poor relationship if the training data is poor, insufficient, or misleading.

That's why Machine Learning doesn't stop after creating a model. We also need to evaluate how well the model performs.

REMEMBER A Machine Learning model is the learned representation of patterns or relationships that can be used to make predictions on new data.
We now have the pieces we need.

We have data, we have learning, and we have a model. But there are two different moments in this process: learning from data and using what was learned.

Next: Training vs Prediction
HOW ML LEARNS

Training vs Prediction

We now know what data, learning, and a model are. The next important distinction is understanding what happens during training and what happens after training when we make a prediction.

First: What Is Training?

Training is the process where a Machine Learning algorithm looks at examples and learns a useful relationship from them.

Let's use our house-price example again.

Our Training Data

House Size Known Price
800 sq ft ₹30 lakh
1000 sq ft ₹40 lakh
1200 sq ft ₹50 lakh
1500 sq ft ₹65 lakh

During training, the algorithm uses these examples to find a relationship between house size and house price.

What Happens During Training?

Conceptually, the process looks like this:

STEP 1 Give Examples

House sizes and their known prices.

STEP 2 Find the Pattern

The algorithm searches for a relationship that fits the examples.

STEP 3 Learn the Parameters

Values that describe the learned relationship.

In our simplified example, suppose training produces:

Price = 0.05 × Size − 10

That learned relationship is now our model.

Training Is Learning From Known Examples

Notice that during training, we already know the correct prices.

For example, the algorithm knows that the 1000 sq ft house actually sold for ₹40 lakh.

It uses these known examples to learn the relationship.

Training = learning from examples where the useful information needed for learning is available.

Now Training Is Finished

Once the model has been trained, we can use it.

Now suppose a completely new house arrives:

NEW HOUSE 1400 sq ft

We don't know its actual selling price yet.

We give the 1400 sq ft input to the trained model.

TRAINED MODEL Price = 0.05 × Size − 10
+
NEW INPUT 1400 sq ft
PREDICTION ₹60 lakh

What Is Prediction?

Prediction is using a trained model with new data to produce an estimated output.

The model is no longer learning the basic relationship from scratch. It is applying what it already learned.

Prediction = using a trained model to estimate an output for new input data.

Training and Prediction Are Different

Here's the simplest way to separate them.

Training Prediction
Uses known examples Uses new data
Learns the relationship Uses the learned relationship
Produces a trained model Produces an estimated output

A Simple Real-Life Analogy

Think about learning to recognize different types of fruit.

Someone shows you many examples and tells you:

  • "This is an apple."
  • "This is a banana."
  • "This is an orange."

You observe their characteristics and learn how to distinguish them.

Later, someone gives you a fruit you've never seen before and asks:

"What is this?"

You use what you learned to make your answer.

The learning process is similar to training. Identifying the new fruit is similar to making a prediction.

Does Prediction Always Mean a Number?

No.

The output depends on the Machine Learning problem.

House price: ₹60 lakh

Spam detection: Spam

Image classification: Cat

Customer prediction: Likely to buy

One Common Beginner Mistake

Beginners sometimes think that the model continuously learns every time it makes a prediction.

That's not necessarily true.

In a simple prediction system, the model can already be trained and then simply be used to produce predictions.

Training and prediction are separate stages.

Training creates or updates what the model has learned. Prediction uses that learned model on new data.

Put Everything Together

TRAINING DATA Known Examples

Size + known price

TRAINING Learn

Find a useful relationship

MODEL Learned Relationship

Can be reused

NEW DATA 1400 sq ft

Unknown price

PREDICTION ₹60 lakh

Estimated output

REMEMBER Training is where the model learns from examples. Prediction is where the trained model is used on new data.
So far, we've done all of this with mathematics.

Now it's time to stop doing the calculations manually. Let's build this simple example using Python and see what an actual Machine Learning library does.

Next: Build It With Python
BUILD IT

Build It With Python

So far, we calculated the relationship between house size and price ourselves.

Now let's let Python and a Machine Learning library do the learning for us.

What Are We Going to Build?

We want a program that can look at examples like these:

House Size Price
800 sq ft ₹30 lakh
1000 sq ft ₹40 lakh
1200 sq ft ₹50 lakh
1500 sq ft ₹65 lakh

Then we want it to learn the relationship and predict the price of a new house.

OUR GOAL Give the program house sizes and prices → train a model → predict a new house price

Step 1: Import the Machine Learning Tool

We'll use scikit-learn, one of the most commonly used Python libraries for Machine Learning.

from sklearn.linear_model import LinearRegression

This imports the LinearRegression algorithm.

Linear regression is a good choice for our first example because we are trying to predict a numerical value: house price.

Step 2: Create the Training Data

Next, we give Python the house sizes and their known prices.

X = [[800],
     [1000],
     [1200],
     [1500]]

y = [30, 40, 50, 65]

Here we use the price in lakhs to keep the numbers simple.

X House sizes — the information we use to make a prediction.
y Known house prices — the answers from our examples.

Step 3: Create the Model

Now we create a Linear Regression model.

model = LinearRegression()

At this point, the model has not learned our house-price relationship yet.

We have simply created the object that will perform the learning.

Step 4: Train the Model

Now comes the important part.

model.fit(X, y)

The fit() method tells the algorithm:

"Look at these examples and learn the relationship between X and y."

This is the training step we discussed earlier.

Step 5: Give It a New House

Now suppose we want to estimate the price of a:

NEW HOUSE 1400 sq ft

We pass the new house size to the trained model.

prediction = model.predict([[1400]])

The model uses what it learned during training and returns a predicted price.

The Complete Program

Now let's put all the pieces together.

from sklearn.linear_model import LinearRegression

X = [[800],
     [1000],
     [1200],
     [1500]]

y = [30, 40, 50, 65]

model = LinearRegression()

model.fit(X, y)

prediction = model.predict([[1400]])

print(prediction)

What Should We Expect?

The model looks at the training examples and finds the relationship that best fits them.

For our new 1400 sq ft house, it produces a prediction close to the relationship we calculated manually earlier.

MODEL PREDICTION Approximately ₹60 lakh

The exact result comes from the relationship learned by the Linear Regression algorithm.

Compare What We Did Before and Now

BEFORE We did the math ourselves

We calculated the relationship manually and wrote the equation.

NOW The algorithm learns it

We give it examples and let Linear Regression find the relationship.

One Important Difference

Earlier, we already knew the equation:

Price = 0.05 × Size − 10

In the Python version, we don't give that equation to the algorithm.

We only give it the examples:

X = [[800],
     [1000],
     [1200],
     [1500]]

y = [30, 40, 50, 65]

The algorithm calculates the relationship itself.

This is the key transition from ordinary programming to Machine Learning.

Instead of manually writing the rule that connects the input to the output, we provide examples and let the algorithm learn the rule.

What Just Happened?

1 Training Data

House sizes + known prices

2 Linear Regression

Finds the relationship

3 Trained Model

Stores the learned relationship

4 New House

1400 sq ft

5 Prediction

Estimated price

REMEMBER We provide the examples. The Machine Learning algorithm finds the relationship. The trained model uses that relationship to make predictions.
The code is short, but every line has a purpose.

If you understand what each line is doing, you understand the basic workflow of a Machine Learning program.

Next: Understand the Python Code
BUILD IT

Try It Yourself

You have seen how the model is created, trained, and used for prediction. Now change the input yourself and see what the model does.

This is where the idea becomes easier to understand. Don't just read the code — experiment with it.

Start With the Same Model

Run this code first without changing anything.

from sklearn.linear_model import LinearRegression

X = [[800],
     [1000],
     [1200],
     [1500]]

y = [30, 40, 50, 65]

model = LinearRegression()

model.fit(X, y)

prediction = model.predict([[1400]])

print(prediction)
STARTING POINT Prediction for a 1400 sq ft house

The model produces an estimated price based on the relationship it learned from the training data.

Experiment 1: Change the House Size

Find this line:

prediction = model.predict([[1400]])

Change 1400 to 1800.

prediction = model.predict([[1800]])

Run the program again.

The model will produce a different prediction because you gave it a different input.

BEFORE 1400 sq ft

One prediction

CHANGE 1800 sq ft

Different input

RESULT New prediction

The model calculates again

Why Did the Prediction Change?

Because the model learned a relationship between house size and price.

When the size changes, the input to that relationship changes, so the predicted price changes too.

Smaller house → generally lower predicted price Larger house → generally higher predicted price

Experiment 2: Try Another House

Now try:

prediction = model.predict([[1100]])

Run the program.

Then try:

prediction = model.predict([[2000]])

Run it again.

New House What Should Happen?
1100 sq ft Model produces a prediction
1400 sq ft Model produces a prediction
1800 sq ft Model produces a prediction
2000 sq ft Model produces a prediction

Experiment 3: Ask for Several Houses at Once

You don't have to make one prediction at a time.

Try:

prediction = model.predict([
    [900],
    [1300],
    [1700],
    [2000]
])

print(prediction)

Now the model receives four new house sizes.

It returns four predicted prices — one for each house.

900 sq ft Prediction
1300 sq ft Prediction
1700 sq ft Prediction
2000 sq ft Prediction

Important: We Did Not Retrain the Model

Look carefully at what we changed.

We changed:

model.predict([[1400]])

We did not change:

model.fit(X, y)

The model was already trained.

We are simply giving the trained model different inputs.

Prediction does not mean training again.

Once the model is trained, you can use it with many different inputs without retraining it every time.

Experiment 4: Change the Training Data

Now let's do something more interesting.

Change one of the known prices:

y = [30, 45, 50, 65]

We changed the price of the 1000 sq ft house from 40 to 45.

Now train the model again:

model.fit(X, y)

Then predict the price of a 1400 sq ft house:

prediction = model.predict([[1400]])

You should get a different prediction than before.

OLD DATA 1000 → ₹40L
NEW DATA 1000 → ₹45L
RESULT Learned relationship changes

Why Did the Model Change?

Because the training data changed.

The model learns from the data you give it. If the examples change, the relationship learned from those examples can change as well.

Your Turn: Make Three Changes

Try these experiments yourself.

CHALLENGE 1 Predict a 1600 sq ft house

Change only the value inside predict().

CHALLENGE 2 Predict a 2200 sq ft house

Keep the training data unchanged.

CHALLENGE 3 Change one training price

Train the model again and compare the prediction.

Ask Yourself These Questions

  • What happens when I increase the house size?
  • What happens when I decrease the house size?
  • What happens when I change the training data?
  • Do I need to call fit() every time I call predict()?
THE CORE IDEA Change the input → the prediction can change. Change the training data → the learned model can change.

What You Just Experienced

You didn't manually calculate a new equation for every house.

You trained the model once and then gave it different inputs.

That's one of the useful ideas behind Machine Learning: the learned relationship can be reused on new data.

REMEMBER A trained model can be reused to make predictions for many new inputs. If the training data changes and the model is trained again, the learned relationship can change too.
Now we have built and tested our first Machine Learning model.

But something important happened when we changed the training data. The model's behavior changed too. Let's understand exactly why.

Next: What Happens When the Data Changes?
REVIEW

Key Takeaways

If you remember only a few things from this lesson, remember these.

01
Machine Learning learns from examples.

Instead of manually writing every rule, we provide data and allow an algorithm to learn useful relationships from it.

02
Training data contains examples.

In our house-price example, the house sizes were the inputs and the known prices were the target values.

03
A model learns a relationship from the data.

The model can then use what it learned when it receives new input.

04
Training and prediction are different.

fit() trains the model. predict() uses the trained model to produce an output.

05
Predictions are estimates.

A model can make useful predictions, but a prediction is not automatically guaranteed to be correct.

06
The training data matters.

If the training data changes and the model is trained again, what the model learns can change.

07
More data is not automatically better.

Useful, accurate, and relevant data matters more than simply having a larger amount of data.

The One Flow to Remember

1 Data Examples
2 Training Learn
3 Model What was learned
4 New Data New input
5 Prediction Estimated output
REMEMBER THIS Data → Learn → Model → Predict

That is the basic Machine Learning workflow you learned in this lesson.

Before continuing, can you explain these three lines?
model = LinearRegression()

model.fit(X, y)

model.predict([[1400]])

If you can explain what each line does in your own words, you understand the core of this lesson.

You've built your first Machine Learning workflow.

Now let's look at where this foundation leads and what you'll learn next.

Next: What You'll Learn Next