ab khel badal gaya. Clustering me hum andhe the.
Supervised me “teacher” hai – hume pata hai answer kya hai. Model ko bas sikhana hai.
10.1 Supervised ML ke 2 Type
| Type | Predict kya karna hai | Example |
| 1.Regression | Number/Continuous | Final Marks kitne aayenge? |
| 2.Classification | Category/Yes-No | Pass hoga ya Fail? |
Hum dono karenge apne Students wale data pe.
STEP 1: Data Ready + Train-Test Split
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import r2_score, accuracy_score, classification_report
X = df[[‘Math’,’Science’,’English’]] # Features
y_reg = df[‘Average’] # Regression target: number
y_clf = (df[‘Average’] >= 60).astype(int) # Classification target: 1=Pass, 0=Fail
X_train, X_test, y_train, y_test = train_test_split(X, y_reg, test_size=0.2, random_state=42)
print(“Train:”, X_train.shape, “Test:”, X_test.shape)
Rule: 80% sikhne ke liye, 20% test karne ke liye
STEP 2A: Linear Regression – Marks Predict Karo
model_reg = LinearRegression()
model_reg.fit(X_train, y_train)
y_pred = model_reg.predict(X_test)
print(“R2 Score:”, round(r2_score(y_test, y_pred), 3))
print(“Coefficients:”, model_reg.coef_)
R2 Score: 0 se 1. 0.85+ = model badhiya hai
Coefficient: Kaunsa subject sabse zyada matter karta hai
STEP 2B: Logistic Regression – Pass/Fail Predict
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(X, y_clf, test_size=0.2, random_state=42)
model_clf = LogisticRegression()
model_clf.fit(X_train_c, y_train_c)
y_pred_c = model_clf.predict(X_test_c)
print(“Accuracy:”, round(accuracy_score(y_test_c, y_pred_c), 3))
print(classification_report(y_test_c, y_pred_c))
Accuracy: Kitne % sahi predict kiya
STEP 3: Decision Tree – Sabse easy to understand
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X_train_c, y_train_c)
plt.figure(figsize=(8,5))
plot_tree(tree, feature_names=X.columns, class_names=[‘Fail’,’Pass’], filled=True)
plt.show()
Tree dekh ke principal bhi samajh jayega “Math<50 ho to Fail”
10.2 Important Metrics CheatSheet
| Metric | Kab use | Best Value |
| R2 Score | Regression | 1 ke paas |
| RMSE | Regression error | Kam se kam |
| Accuracy | Classification | 1 ke paas |
| Precision/Recall | Imbalanced data | Dono high |
Key Formula – Linear Regression:
y=m∗x+c
Goal: Error sabse kam ho. MSE = Mean Squared Error
CODE – REGRESSION –
Project1 : Salary Prediction
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
#Data
data = {‘Experience’:[1,2,3,4,5], ‘Salary’:[30000,35000,40000,45000,50000]}
df = pd.DataFrame(data)
X = df[[‘Experience’]]
y = df[‘Salary’]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)
print(“6 years exp salary:”, model.predict([[6]]))
Output: 55000.0
CODE – CLASSIFICATION –
Project 2: Student Pass/Fail
from sklearn.tree import DecisionTreeClassifier
#Hours Studied, Pass=1 Fail=0
X = [[2,0], [3,0], [5,1], [6,1], [8,1]]
y = [0, 0, 1, 1, 1]
model = DecisionTreeClassifier()
model.fit(X, y)
print(“4 hours wala:”, model.predict([[4]])) # 0 = Fail
Output: 0
Metrics: accuracy_score, precision, recall, f1
Interview + GATE ke 4 Sawal
Q1. Overfitting kya hai?
Train me 99% score, Test me 60%. Model ratta maar liya.
Q2. Linear vs Logistic ka diff?
Linear = number predict, Logistic = category predict
Q3. Train-Test split kyu?
Nahi karoge to model cheating karega. Copy maar ke paper dega.
Q4. Feature importance kaise nikale?
model.feature_importances_ ya coef_
Aaj ka 15 min Practice Project
Q1: Random Forest try karo. from sklearn.ensemble import RandomForestRegressor. R2 better hua?
Q2: Predict karo: Math=80, Science=70, English=90 wale ka Average kitna?
model_reg.predict([[80,70,90]])
Q3: Classification me kis subject se Pass/Fail sabse zyada decide hota hai?
15 MIN MINI PROJECT
Project3: House Price Prediction App
Step 1: Data Area, Bedroom, Age → Price
Step 2: Code
import pandas as pd
from sklearn.linear_model import LinearRegression
df = pd.read_csv(‘house.csv’)
X = df[[‘Area’,’Bedroom’,’Age’]]
y = df[‘Price’]
model = LinearRegression().fit(X,y)
#User se input lo
area = int(input(“Area: “))
bed = int(input(“Bedroom: “))
age = int(input(“Age: “))
price = model.predict([[area,bed,age]])
print(f”Ghar ki price: ₹{price[0]}”)
Step 3: Ab isko Test karke dekh sakte hai…..
PYQ + INTERVIEW Q
Q1. Regression aur Classification me farak?
Ans: Regression = Continuous number, Classification = Category
Q2. Overfitting kya hai?
Ans: Model training data rat leta hai, naye data pe fail
Q3. GATE PYQ: MSE ka formula likho
Ans: MSE = 1/n * sum(y – y_pred)^2
