|5. Scikit-learn — Classical ML
Chapter 5Artificial Intelligence~1 min read

Scikit-learn — Classical ML

पहिले ML Model बनवा

Scikit-learn हे Python मधले most popular classical ML library आहे. Linear Regression, Decision Trees, SVM, K-Means — सगळे algorithms consistent API सोबत available आहेत. पहिला real ML model scikit-learn नेच बनवतात.

Complete ML example — House Price Prediction

python
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Data load करा
df = pd.read_csv('houses.csv')
X = df[['area', 'bedrooms', 'age']].values  # features
y = df['price'].values                        # target

# Train/Test 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
)

# Feature Scaling — important for many algorithms
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)   # Note: transform only, not fit!

# Model train करा
model = LinearRegression()
model.fit(X_train, y_train)

# Predictions
y_pred = model.predict(X_test)

# Evaluate
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print(f"RMSE: {rmse:.2f}")
print(f"R² Score: {r2:.3f}")  # 1.0 = perfect

Classification Example — Spam Detection

Logistic Regression classifier

python
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# Same split/scale steps...

clf = LogisticRegression()
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, y_pred):.2%}")
print(classification_report(y_test, y_pred))
# Precision, Recall, F1 score दाखवतो

Common Algorithms

  • Linear Regression — continuous value predict (house prices)
  • Logistic Regression — binary classification (spam/not spam)
  • Decision Tree — interpretable, tree-based decisions
  • Random Forest — multiple decision trees, more accurate
  • SVM (Support Vector Machine) — powerful classifier
  • K-Means — clustering, customer segmentation
  • KNN (K-Nearest Neighbors) — simple, instance-based

Key Points — लक्षात ठेवा

  • train_test_split — model evaluate करायला
  • StandardScaler — features same scale वर आणा
  • fit() — model train, predict() — predictions
  • Accuracy: classification, RMSE/R²: regression
  • Random Forest: Decision Tree पेक्षा almost always better
0/11 chapters पूर्ण