R
Rishtaara
Knowledge Hub
Technology & IT

Machine Learning Fundamentals: Complete Guide

By Rishtaara Editorial Team38 min read
#ML#scikit-learn#Classification#Regression

Data preprocessing, supervised learning, regression, classification, and model evaluation.

What machine learning does

Machine learning builds models that learn patterns from data and make predictions or decisions without hand-written rules for every case.

  • Supervised learning: labeled examples.
  • Unsupervised learning: discover hidden structure.
  • Reinforcement learning: learn via rewards.

Typical ML workflow

  • Frame problem and metric.
  • Collect and clean data.
  • Train baseline model.
  • Evaluate, iterate, and deploy.

Data preparation essentials

  • Handle missing values with drop/impute strategy.
  • Encode categorical variables.
  • Scale numeric features for distance-based models.
  • Split train/validation/test before peeking at outcomes.

Pipeline example

Scikit-learn preprocessing pipeline
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import SimpleImputer

numeric_features = ["age", "salary"]
categorical_features = ["city", "education"]

preprocess = ColumnTransformer([
    ("num", Pipeline([
        ("imputer", SimpleImputer(strategy="median")),
        ("scaler", StandardScaler())
    ]), numeric_features),
    ("cat", Pipeline([
        ("imputer", SimpleImputer(strategy="most_frequent")),
        ("onehot", OneHotEncoder(handle_unknown="ignore"))
    ]), categorical_features),
])

Core supervised setup

Supervised learning uses input-output pairs to learn a mapping from features to labels.

  • Regression predicts continuous values.
  • Classification predicts discrete classes.
  • Generalization means good performance on unseen data.

Train/test split example

Simple model training loop
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

model = DecisionTreeClassifier(max_depth=5, random_state=42)
model.fit(X_train, y_train)

When to use regression

  • Forecasting sales or demand.
  • Estimating prices, risk, or scores.
  • Modeling relationships between numeric variables.

Linear regression baseline

Fit and evaluate linear regression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score

reg = LinearRegression()
reg.fit(X_train, y_train)
pred = reg.predict(X_test)

print("MAE:", mean_absolute_error(y_test, pred))
print("R2:", r2_score(y_test, pred))

Classification tasks

  • Spam detection, fraud screening, churn prediction.
  • Binary, multiclass, and multilabel formulations.
  • Threshold choice affects precision/recall trade-offs.

Logistic regression classifier

Train and probability thresholding
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

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

proba = clf.predict_proba(X_test)[:, 1]
pred = (proba >= 0.6).astype(int)  # custom threshold
print(classification_report(y_test, pred))

Evaluation metrics by problem type

  • Regression: MAE, RMSE, R2.
  • Classification: accuracy, precision, recall, F1, ROC-AUC.
  • Imbalanced classes require precision-recall focus.

Cross-validation example

K-fold score estimate
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=200, random_state=42)
scores = cross_val_score(model, X, y, cv=5, scoring="f1")
print("Fold scores:", scores)
print("Mean F1:", scores.mean())

Unsupervised learning objectives

  • Clustering similar observations.
  • Reducing dimensions for visualization and denoising.
  • Detecting anomalies without labels.

K-means clustering

Cluster data into groups
from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
labels = kmeans.fit_predict(X)
centroids = kmeans.cluster_centers_
print("Centroids shape:", centroids.shape)

Project brief

Build a complete ML pipeline from preprocessing to evaluation. Pick a real dataset, define a metric, compare at least two model families, and produce a concise model card.

  • Data quality checks and feature report.
  • Baseline + improved model comparison.
  • Error analysis for top failure cases.
  • Deployment idea (batch API or dashboard).

Minimal training script structure

Train/evaluate function signatures
def load_data(path: str):
    ...

def build_pipeline():
    ...

def train_and_evaluate(X_train, X_test, y_train, y_test):
    ...

if __name__ == "__main__":
    X_train, X_test, y_train, y_test = load_data("data.csv")
    metrics = train_and_evaluate(X_train, X_test, y_train, y_test)
    print(metrics)

Key Takeaways

  • Good ML starts with clear problem framing and clean data.
  • Preprocessing and validation choices can outweigh model complexity.
  • Use the right metric for business impact, not just convenience.
  • Baselines and cross-validation prevent overconfidence.
  • End-to-end projects convert theory into deployable skill.