Machine Learning & AI · Chapter 4.2 · Basic level

Linear Regression (Study Hours vs Exam Marks Predictor!)

Linear Regression (पढ़ाई के घंटे vs एग्ज़ाम मार्क्स प्रेडिक्टर!)

When AI needs to predict a number — like 'If I study 4.5 hours daily, how many marks will I score?' or 'What will be this house's price?' — data scientists use Linear Regression! The AI plots your past data dots on a graph and draws the Best Fit Straight Line (y = m * x + c, where x = hours, m = marks gained per hour, y = predicted marks)!

जब AI को कोई संख्या (Number) बतानी हो — जैसे 'अगर मैं रोज़ 4.5 घंटे पढ़ूँ तो मेरे कितने मार्क्स आएँगे?' या 'इस घर की कीमत क्या होगी?' — तब Linear Regression इस्तेमाल होता है! AI पुराने डेटा की नीली बिंदियों के बीच से सबसे सही सीधी रेखा (Best Fit Line: y = m*x + c) खींचकर सटीक नंबर बता देता है!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

from sklearn.linear_model import LinearRegression
Imports the famous real-world LinearRegression tool from Python's sklearn AI library!
Python की मशहूर sklearn AI लाइब्रेरी से LinearRegression टूल मँगाता है!
hours = [1, 2, 3, 4, 5]
Input clue X — daily study hours.
इनपुट डेटा X — रोज़ पढ़ाई के घंटे।
marks = [20, 40, 60, 80, 100]
Target numbers y — exam marks scored.
जवाब डेटा y — परीक्षा में आए अंक।
model = LinearRegression()
Creates a fresh Linear Regression AI brain.
नया Linear Regression AI मॉडल बनाता है।
model.fit(hours, marks)
Finds the best-fit straight line (marks = 20 * hours).
डेटा से सीधी रेखा का गणितीय नियम (y = 20x + 0) ढूँढता है।
model.predict(4.5)
Predicts 90.0 marks for 4.5 hours of study!
4.5 घंटे पढ़ने पर 90.0 मार्क्स की भविष्यवाणी करता है!

Ready example

from sklearn.linear_model import LinearRegression

hours = [1, 2, 3, 4, 5]
marks = [20, 40, 60, 80, 100]

model = LinearRegression()
model.fit(hours, marks)

predicted_marks = model.predict(4.5)
print("Predicted Marks:", predicted_marks)

What you will see: Live Scatter Plot (Blue Dots + Glowing Green Best-Fit Line) + an interactive Study Hours Slider moving the Red Prediction Dot!

असली ग्राफ़ पर नीली बिंदियाँ + चमकती हरी Best-Fit लाइन बनेगी, और नीचे स्लाइडर हिलाकर आप लाल प्रेडिक्शन बिंदु को ऊपर-नीचे चला सकेंगे!

Line by line

from sklearn.linear_model import LinearRegression
Loads the industry-standard scikit-learn syntax!
असली डेटा साइंटिस्ट्स वाला sklearn इम्पोर्ट!
hours = [1, 2, 3, 4, 5] / marks = [20, 40, 60, 80, 100]
5 past student records (try changing these numbers and watch the graph line tilt live!).
5 छात्रों का पुराना रिकॉर्ड (इन्हें बदलोगे तो ग्राफ़ की हरी लाइन भी तुरंत बदलेगी!)।
model.fit(hours, marks)
Computes slope m and intercept c automatically.
आँकड़ों से सबसे सटीक सीधी लाइन खींचता है।
predicted_marks = model.predict(4.5)
Outputs 90.0!
4.5 घंटे के लिए 90.0 मार्क्स निकालता है!

Common mistake

✗ Wrong

hours = [1, 2, 3]
marks = [20, 40]  # unequal lengths!

✓ Right

hours = [1, 2, 3]
marks = [20, 40, 60]

Every question in X must have a matching answer in y — both lists must have the exact same length!

X के हर सवाल के लिए y में जवाब होना चाहिए — दोनों लिस्ट में बराबर संख्या में डेटा होना ज़रूरी है!

Your task

Train LinearRegression() with hours and marks and print model.predict(4.5) — then drag the Hours Slider in the Live Graph!

LinearRegression() को hours और marks से ट्रेन करो और model.predict(4.5) प्रिंट करो — फिर ग्राफ़ के स्लाइडर को चलाकर देखो!

  1. Import LinearRegression from sklearn.linear_model.
    from sklearn.linear_model import LinearRegression लिखो।
  2. Create model = LinearRegression() and call model.fit(hours, marks).
    model = LinearRegression() बनाकर model.fit(hours, marks) चलाओ।
  3. Print model.predict(4.5) and click ▶ Run & Preview!
    model.predict(4.5) प्रिंट करके ▶ Run & Preview दबाओ!
Starter code for the task
from sklearn.linear_model import LinearRegression

hours = [1, 2, 3, 4, 5]
marks = [20, 40, 60, 80, 100]

# TODO 1: model = LinearRegression() banao
# TODO 2: model.fit(hours, marks) se train karo
# TODO 3: print("Predicted Marks:", model.predict(4.5))

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

Which ML algorithm predicts continuous numbers like Marks or House Price using a straight line?

कौन-सा ML मॉडल सीधी रेखा खींचकर मार्क्स या घर की कीमत जैसी संख्याएँ बताता है?

  1. Linear Regression
  2. HTML Parser
  3. K-Means
  4. Bubble Sort
Show the answer

Answer: A (Linear Regression)

Linear Regression fits y = mx + c to predict continuous numerical values!

Linear Regression डेटा पर बेस्ट-फ़िट सीधी लाइन खींचकर संख्या की भविष्यवाणी करता है!

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.