Skip to main content

Train-Test Split & Linear Regression

Now let's build your first real ML model. We will predict house prices based on house size using Linear Regression with Scikit-Learn.


1. Why Split Data into Train and Test?​

Imagine studying for an exam. If you practice with 100 questions and then the exam has the same 100 questions, of course you will score 100%. But that does not mean you actually learned the subject.

ML works the same way. If you train a model on all your data and test it on the same data, it will look perfect — but it has just memorized, not learned.

The Solution: Train-Test Split​

Split your data into two parts:

  • Training Data (80%): Used to teach the model
  • Testing Data (20%): Used to check if the model actually learned
from sklearn.model_selection import train_test_split

# X = features, y = target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  • test_size=0.2 → 20% for testing, 80% for training
  • random_state=42 → Makes the split the same every time you run it (for consistency)

2. Installing Scikit-Learn​

pip install scikit-learn

Already installed in Google Colab.


3. Building Your First Model — Step by Step​

Let's predict house prices based on house size (in square feet):

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Step 1: PREPARE DATA
# House sizes in sq ft (feature)
X = np.array([[600], [800], [1000], [1200], [1500], [1800], [2000], [2200], [2500], [3000]])
# House prices in lakhs (target)
y = np.array([30, 40, 50, 60, 75, 90, 100, 110, 125, 150])

# Split: 80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print(f"Training samples: {len(X_train)}") # Output: 8
print(f"Testing samples: {len(X_test)}") # Output: 2

# Step 2: CHOOSE MODEL
model = LinearRegression()

# Step 3: TRAIN MODEL
model.fit(X_train, y_train)
print("Model trained!")

# Step 4: PREDICT
predictions = model.predict(X_test)
print("Predictions:", predictions)
print("Actual values:", y_test)

4. What is Linear Regression?​

Linear Regression finds the best straight line that fits your data.

Price = (slope × size) + intercept
# See the learned values
print("Slope:", model.coef_[0]) # How much price increases per sq ft
print("Intercept:", model.intercept_) # Base price when size is 0

If slope = 0.05 and intercept = 0:

  • 1000 sq ft house → Price = 0.05 × 1000 + 0 = ₹50 Lakhs
  • 2000 sq ft house → Price = 0.05 × 2000 + 0 = ₹100 Lakhs

5. Checking Your Model — Is It Good?​

R² Score (0 to 1)​

  • 1.0 = Perfect predictions
  • 0.5 = Okay predictions
  • 0.0 = Terrible (model is useless)
from sklearn.metrics import r2_score

# Score on test data
score = model.score(X_test, y_test)
print(f"R² Score: {score:.2f}")

# Or calculate manually
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
print(f"R² Score: {r2:.2f}")

Rule of thumb: R² above 0.7 is good. Above 0.9 is great.


6. Predicting New Values​

# Predict the price of a 1100 sq ft house
new_house = np.array([[1100]])
predicted_price = model.predict(new_house)
print(f"Predicted price for 1100 sq ft: ₹{predicted_price[0]:.1f} Lakhs")

# Predict multiple houses at once
new_houses = np.array([[1100], [1500], [2500]])
predictions = model.predict(new_houses)
for size, price in zip([1100, 1500, 2500], predictions):
print(f"{size} sq ft → ₹{price:.1f} Lakhs")

Summary​

ConceptWhat it Means
Train-Test SplitDivide data into training (80%) and testing (20%) to check if model actually learned
Linear RegressionFinds the best straight line through your data to make predictions
.fit(X, y)Train the model — it learns the pattern
.predict(X_new)Use the trained model to predict new values
R² ScoreHow good the model is (0 = bad, 1 = perfect)
  • Always split your data before training. Never test on training data!
  • Linear Regression is the simplest ML algorithm — but it is powerful and widely used.
  • Next, we will learn Classification — predicting categories (pass/fail, spam/not spam) instead of numbers.
⏳

Coming Soon

This AI & ML module is currently under development. Stay tuned for the advanced premium curriculum!