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) खींचकर सटीक नंबर बता देता है!
Words and symbols in this chapter
from sklearn.linear_model import LinearRegression- Imports the famous real-world
LinearRegressiontool from Python'ssklearnAI library! - Python की मशहूर
sklearnAI लाइब्रेरी से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.0marks for4.5hours 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
mand interceptcautomatically. - आँकड़ों से सबसे सटीक सीधी लाइन खींचता है।
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) प्रिंट करो — फिर ग्राफ़ के स्लाइडर को चलाकर देखो!
- Import
LinearRegressionfromsklearn.linear_model.from sklearn.linear_model import LinearRegressionलिखो। - Create
model = LinearRegression()and callmodel.fit(hours, marks).model = LinearRegression()बनाकरmodel.fit(hours, marks)चलाओ। - 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
- Linear Regression predicts a number by drawing the best straight line
y = m*x + c. - The
Xandylists must have the same length. - Train with
fit(hours, marks); thenpredict(4.5)gives the expected marks.
- Linear Regression सबसे सही सीधी रेखा
y = m*x + cखींचकर एक संख्या बताता है। Xऔरylists की लंबाई बराबर होनी चाहिए।fit(hours, marks)से train करो; फिरpredict(4.5)अनुमानित marks देता है।
Quick quiz
Which ML algorithm predicts continuous numbers like Marks or House Price using a straight line?
कौन-सा ML मॉडल सीधी रेखा खींचकर मार्क्स या घर की कीमत जैसी संख्याएँ बताता है?
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
- Fix the Marks Predictor (Linear Regression)
मार्क्स प्रेडिक्टर ठीक करो (Linear Regression)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.