Normal programming me hum rules likhte hain: agar X to Y. Machine Learning me hum computer ko examples dete hain, aur wo khud rules seekh leta hai. Jaise hazaaron emails dikha ke — ye spam hai, ye nahi — model khud seekh leta hai ki spam kaisa dikhta hai.
Machine Learning ke types
Type | Kya hota hai | Examples |
|---|---|---|
Supervised | Data me sahi jawab (label) diya hota hai | House price prediction, spam detection |
Unsupervised | Koi label nahi, model khud groups/patterns dhoondhta hai | Customer segmentation, anomaly detection |
Reinforcement | Model reward/penalty se seekhta hai | Games, robotics, self-driving |
Supervised learning ke do main type:
- Regression — number predict karna (price, salary, temperature)
- Classification — category predict karna (spam/not spam, pass/fail, flower ki kism)
Zaroori terms
Term | Matlab |
|---|---|
Features (X) | Input columns jinse prediction hoga |
Target / Label (y) | Jo predict karna hai |
Training | Model ko examples se seekhana ( |
Prediction | Naye data pe jawab dena ( |
Train / Test set | Seekhne wala data aur jaanchne wala data |
Pehla model: Iris flowers classify karna
scikit-learn ke saath aane wala Iris dataset — 150 flowers, 4 measurements, aur 3 kismein (setosa, versicolor, virginica). Kaam: measurements dekh ke kism batana.
Example
from sklearn.datasets import load_iris
iris = load_iris(as_frame=True)
X = iris.data # features
y = iris.target # target (0, 1, 2)
print(X.head())
print()
print(y.value_counts())
print("Classes:", iris.target_names.tolist())
Output
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
target
0 50
1 50
2 50
Name: count, dtype: int64
Classes: ['setosa', 'versicolor', 'virginica']
Train-test split
Model ko jo data dikhaya, usi pe test karna cheating hai — jaise exam me wahi sawaal aa jaayein jo ratte the. Isliye data ko do hisson me baantte hain:
Example
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print("Train:", X_train.shape, "| Test:", X_test.shape)
Output
Train: (120, 4) | Test: (30, 4)
stratify=y se dono hisson me har class ka anupaat same rehta hai, aur random_state se split har baar same aata hai.
Model train karna aur predict karna
K-Nearest Neighbors (KNN) — naye flower ke sabse paas ke K flowers dekho, jo kism zyada ho wahi jawab. scikit-learn me har model ka pattern same hai: fit → predict → score.
Example
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Pehle 10 predictions:", predictions[:10])
print("Asli jawab: ", y_test.values[:10])
print("Accuracy:", round(accuracy_score(y_test, predictions), 3))
Output
Pehle 10 predictions: [0 2 1 1 0 1 0 0 2 1]
Asli jawab: [0 2 1 1 0 1 0 0 2 1]
Accuracy: 1.0
Model ne test data ke 100.0% flowers ki kism sahi batayi — un flowers pe jo usne kabhi dekhe hi nahi the.
Naye flower pe prediction
Example
import pandas as pd
new_flower = pd.DataFrame([[5.1, 3.4, 1.5, 0.2]], columns=X.columns)
print("Kism:", iris.target_names[model.predict(new_flower)[0]])
Output
Kism: setosa
Overfitting vs Underfitting
Underfitting | Good fit | Overfitting | |
|---|---|---|---|
Matlab | Model ne kuch khaas nahi seekha | Pattern seekha | Ratta maar liya, noise bhi yaad kar liya |
Train score | Kam | Accha | Bahut accha |
Test score | Kam | Accha | Kam |
Ilaaj | Complex model, zyada features | — | Simple model, zyada data, regularization |
Machine Learning ke concepts aur detail me: What is Machine Learning aur Scikit-learn Complete Guide.