0% found this document useful (0 votes)
11 views10 pages

MITS AI Machine Learning Course

The MITS Academy AI & Machine Learning Course covers advanced data preprocessing, model evaluation, hyperparameter tuning, and advanced algorithms including SVMs and CNNs. It emphasizes feature engineering, handling imbalanced datasets, cross-validation, and ensemble methods, providing practical examples using Python libraries. The course aims to equip learners with intermediate-level skills in AI and machine learning techniques for real-world applications.

Uploaded by

bhumikatalwar19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

MITS AI Machine Learning Course

The MITS Academy AI & Machine Learning Course covers advanced data preprocessing, model evaluation, hyperparameter tuning, and advanced algorithms including SVMs and CNNs. It emphasizes feature engineering, handling imbalanced datasets, cross-validation, and ensemble methods, providing practical examples using Python libraries. The course aims to equip learners with intermediate-level skills in AI and machine learning techniques for real-world applications.

Uploaded by

bhumikatalwar19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MITS Academy — AI & Machine Learning Course

MITS ACADEMY
AI & Machine Learning Course

Intermediate Level | Feature Engineering, Tuning, SVM, CNN, Deployment


[Link]

Page 1 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

Module 1: Advanced Data Preprocessing


1.1 Feature Engineering and Selection
Feature engineering is the process of using domain knowledge to create new features from raw
data. Good features can dramatically improve model performance. Common techniques:
polynomial features (capturing non-linear relationships), interaction features (product of two
features), log transformation (normalizing skewed distributions), and binning (converting
continuous to categorical).
Feature selection reduces dimensionality by keeping only the most informative features. This
prevents overfitting, reduces training time, and improves model interpretability. Methods:
univariate selection (chi-squared, ANOVA), feature importance from tree-based models, and
Recursive Feature Elimination (RFE).
import pandas as pd
import numpy as np
from [Link] import PolynomialFeatures
from sklearn.feature_selection import SelectKBest, chi2, RFE
from [Link] import RandomForestClassifier

# Sample dataset
[Link](42)
df = [Link]({
"study_hours": [Link](1, 10, 500),
"prev_marks": [Link](40, 95, 500),
"attendance": [Link](50, 100, 500),
"assignments": [Link](0, 10, 500),
"noise_feature": [Link](500) # Useless feature
})
df["pass"] = ((0.4*df["study_hours"] + 0.3*df["prev_marks"]/10 +
0.3*df["attendance"]/10) > 6).astype(int)

X = [Link]("pass", axis=1)
y = df["pass"]

# Polynomial features — capture non-linear interactions


poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)
X_poly = poly.fit_transform(X[["study_hours","prev_marks","attendance"]])
print("Original features:", 3, "Poly features:", X_poly.shape[1])

# Feature importance from Random Forest


rf = RandomForestClassifier(n_estimators=100, random_state=42)
[Link](X, y)
importances = [Link](rf.feature_importances_,
index=[Link]).sort_values(ascending=False)
print("Feature Importances:")
print(importances)

# Recursive Feature Elimination


rfe = RFE(estimator=RandomForestClassifier(n_estimators=50),
n_features_to_select=3)
[Link](X, y)
selected = [Link][rfe.support_].tolist()
print("Selected features:", selected)

Page 2 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

1.2 Handling Imbalanced Datasets


In real-world classification problems, class imbalance is common — one class has far more
samples than others (e.g., 95% legitimate transactions, 5% fraud). Training on imbalanced data
causes models to predict the majority class always and still get high accuracy. This is
misleading.
Solutions: oversampling minority class (SMOTE — creates synthetic samples), undersampling
majority class, class_weight parameter in sklearn, and using appropriate metrics (precision,
recall, F1-score, AUC-ROC instead of accuracy).
from [Link] import make_classification
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import classification_report, roc_auc_score
from imblearn.over_sampling import SMOTE # pip install imbalanced-learn
from collections import Counter

# Create imbalanced dataset (10:1 ratio)


X, y = make_classification(n_samples=5000, weights=[0.9, 0.1],
n_features=10, random_state=42)
print("Before SMOTE:", Counter(y)) # {0: 4500, 1: 500}

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

# Without handling imbalance


rf1 = RandomForestClassifier(random_state=42)
[Link](X_train, y_train)
print("Without SMOTE:")
print(classification_report(y_test, [Link](X_test)))

# With SMOTE — oversample minority class


smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X_train, y_train)
print("After SMOTE:", Counter(y_resampled)) # {0: 3600, 1: 3600}

rf2 = RandomForestClassifier(random_state=42)
[Link](X_resampled, y_resampled)
print("With SMOTE:")
print(classification_report(y_test, [Link](X_test)))
print("AUC-ROC:", roc_auc_score(y_test, rf2.predict_proba(X_test)
[:,1]).round(4))

Module 2: Model Evaluation and Hyperparameter


Tuning
2.1 Cross-Validation and Learning Curves
K-Fold Cross-Validation splits data into K folds. The model trains on K-1 folds and validates on
the remaining fold, repeating K times. This gives a more reliable estimate of model performance

Page 3 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

than a single train-test split. Stratified K-Fold preserves class ratios in each fold — essential for
imbalanced datasets.
Learning curves plot training and validation performance as training data size increases. If
training accuracy is high but validation accuracy is low — overfitting (add more data,
regularization, dropout). If both are low — underfitting (more complex model, more features).
from sklearn.model_selection import cross_val_score, StratifiedKFold,
learning_curve
from [Link] import load_breast_cancer
from [Link] import GradientBoostingClassifier
from [Link] import StandardScaler
from [Link] import Pipeline
import numpy as np
import [Link] as plt

data = load_breast_cancer()
X, y = [Link], [Link]

# Pipeline: scale + model


pipeline = Pipeline([
("scaler", StandardScaler()),
("model", GradientBoostingClassifier(n_estimators=100, random_state=42))
])

# Stratified K-Fold CV
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(pipeline, X, y, cv=skf, scoring="f1")
print(f"F1 scores: {[Link](4)}")
print(f"Mean F1: {[Link]():.4f} +/- {[Link]():.4f}")

# Learning curve
train_sizes, train_scores, val_scores = learning_curve(
pipeline, X, y, cv=skf, n_jobs=-1,
train_sizes=[Link](0.1, 1.0, 10),
scoring="f1"
)

train_mean = train_scores.mean(axis=1)
val_mean = val_scores.mean(axis=1)

[Link](figsize=(10, 5))
[Link](train_sizes, train_mean, "o-", label="Training score")
[Link](train_sizes, val_mean, "o-", label="Validation score")
plt.fill_between(train_sizes, train_mean-train_scores.std(axis=1),
train_mean+train_scores.std(axis=1), alpha=0.1)
[Link]("Training Size"); [Link]("F1 Score")
[Link]("Learning Curve"); [Link](); [Link](True)
[Link]("learning_curve.png", dpi=100)

2.2 Hyperparameter Tuning


Every ML model has hyperparameters — settings that must be chosen before training (not
learned from data). Choosing the right values is critical for model performance. Grid Search tries
all combinations. Random Search samples random combinations — often finds equally good
results in less time. Bayesian Optimization is smarter — it learns from previous evaluations to
choose promising next configurations.

Page 4 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV


from [Link] import GradientBoostingClassifier
from [Link] import load_breast_cancer
from [Link] import StandardScaler
from [Link] import Pipeline
from [Link] import randint, uniform
import numpy as np

X, y = load_breast_cancer(return_X_y=True)

pipeline = Pipeline([
("scaler", StandardScaler()),
("model", GradientBoostingClassifier(random_state=42))
])

# Grid Search — exhaustive (slow)


param_grid = {
"model__n_estimators": [50, 100, 200],
"model__max_depth": [2, 3, 4],
"model__learning_rate": [0.05, 0.1, 0.2],
"model__subsample": [0.8, 1.0]
}
grid_search = GridSearchCV(pipeline, param_grid, cv=5,
scoring="f1", n_jobs=-1, verbose=1)
grid_search.fit(X, y)
print("Grid Best Params:", grid_search.best_params_)
print("Grid Best F1:", grid_search.best_score_)

# Random Search — faster


param_dist = {
"model__n_estimators": randint(50, 300),
"model__max_depth": randint(2, 6),
"model__learning_rate": uniform(0.01, 0.3),
"model__subsample": uniform(0.6, 0.4)
}
rand_search = RandomizedSearchCV(pipeline, param_dist, n_iter=50,
cv=5, scoring="f1", n_jobs=-1,
random_state=42)
rand_search.fit(X, y)
print("Random Best Params:", rand_search.best_params_)
print("Random Best F1:", rand_search.best_score_)

Module 3: Advanced Algorithms


3.1 Support Vector Machines
SVMs find the optimal hyperplane that maximizes the margin between classes. The support
vectors are the training points closest to the decision boundary. SVMs work well in high-
dimensional spaces and are effective when the number of features > number of samples. The
kernel trick maps data to higher dimensions to find non-linear boundaries.
from [Link] import SVC
from [Link] import make_moons
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import classification_report
import numpy as np

Page 5 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

# Non-linearly separable data


X, y = make_moons(n_samples=1000, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = [Link](X_test)

# Linear SVM
svm_linear = SVC(kernel="linear", C=1.0)
svm_linear.fit(X_train_s, y_train)
print("Linear SVM:")
print(classification_report(y_test, svm_linear.predict(X_test_s)))

# RBF kernel — handles non-linear boundaries


svm_rbf = SVC(kernel="rbf", C=10, gamma="scale", probability=True)
svm_rbf.fit(X_train_s, y_train)
print("RBF SVM:")
print(classification_report(y_test, svm_rbf.predict(X_test_s)))

# Polynomial kernel
svm_poly = SVC(kernel="poly", degree=3, C=5)
svm_poly.fit(X_train_s, y_train)
print("Poly SVM:")
print(classification_report(y_test, svm_poly.predict(X_test_s)))

# Support vectors
print(f"Support vectors: {svm_rbf.n_support_} per class")

3.2 Ensemble Methods — Stacking and Boosting


Ensemble methods combine multiple models to produce better predictions than any single
model. Bagging (Bootstrap Aggregating) trains models on random subsets of data. Boosting
trains models sequentially, each focusing on errors of the previous. Stacking trains a meta-
model on the predictions of base models.
from [Link] import (RandomForestClassifier,
GradientBoostingClassifier,
VotingClassifier, StackingClassifier)
from sklearn.linear_model import LogisticRegression
from [Link] import SVC
from [Link] import KNeighborsClassifier
from [Link] import load_breast_cancer
from sklearn.model_selection import cross_val_score
from [Link] import StandardScaler
from [Link] import Pipeline

X, y = load_breast_cancer(return_X_y=True)

base_models = [
("rf", RandomForestClassifier(n_estimators=100, random_state=42)),
("gb", GradientBoostingClassifier(n_estimators=100, random_state=42)),
("svm", Pipeline([("sc", StandardScaler()), ("svm",
SVC(probability=True))]))
]

# Hard Voting — majority vote

Page 6 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

voting_hard = VotingClassifier(base_models, voting="hard")


scores = cross_val_score(voting_hard, X, y, cv=5, scoring="f1")
print(f"Hard Voting F1: {[Link]():.4f}")

# Soft Voting — average probabilities (usually better)


voting_soft = VotingClassifier(base_models, voting="soft")
scores = cross_val_score(voting_soft, X, y, cv=5, scoring="f1")
print(f"Soft Voting F1: {[Link]():.4f}")

# Stacking — meta-model learns from base model predictions


stacking = StackingClassifier(
estimators=base_models,
final_estimator=LogisticRegression(),
cv=5, # Use 5-fold CV to create meta-features
passthrough=False
)
scores = cross_val_score(stacking, X, y, cv=5, scoring="f1")
print(f"Stacking F1: {[Link]():.4f}")

Module 4: Deep Learning — Intermediate


4.1 Convolutional Neural Networks (CNN)
CNNs are specialized neural networks for processing grid-like data such as images. They use
convolutional layers that apply learnable filters across the input. Each filter detects specific
features: early layers detect edges, later layers detect complex patterns like faces or objects.
Pooling layers reduce spatial dimensions.
A typical CNN architecture: Input → Conv → ReLU → Pooling → Conv → ReLU → Pooling →
Flatten → Dense → Softmax. Batch Normalization normalizes layer inputs, accelerating training.
Dropout randomly sets neurons to zero during training, preventing overfitting.
import tensorflow as tf
from tensorflow import keras
from [Link] import layers

# Build CNN for image classification


def build_cnn(input_shape, num_classes):
model = [Link]([
# Block 1
layers.Conv2D(32, (3,3), padding="same", input_shape=input_shape),
[Link](),
[Link]("relu"),
layers.Conv2D(32, (3,3), padding="same"),
[Link](),
[Link]("relu"),
layers.MaxPooling2D(2,2),
[Link](0.25),

# Block 2
layers.Conv2D(64, (3,3), padding="same"),
[Link](),
[Link]("relu"),
layers.Conv2D(64, (3,3), padding="same"),
[Link](),
[Link]("relu"),
layers.MaxPooling2D(2,2),

Page 7 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

[Link](0.25),

# Classifier
[Link](),
[Link](512, activation="relu"),
[Link](),
[Link](0.5),
[Link](num_classes, activation="softmax")
])
return model

# CIFAR-10 dataset (10 classes, 32x32 color images)


(X_train, y_train), (X_test, y_test) = [Link].cifar10.load_data()
X_train = X_train.astype("float32") / 255.0
X_test = X_test.astype("float32") / 255.0

model = build_cnn((32,32,3), 10)


[Link](optimizer=[Link](0.001),
loss="sparse_categorical_crossentropy", metrics=["accuracy"])
[Link]()

# Data augmentation — prevents overfitting


datagen = [Link](
rotation_range=15, width_shift_range=0.1,
height_shift_range=0.1, horizontal_flip=True
)

# Train with augmentation


history = [Link]([Link](X_train, y_train, batch_size=64),
epochs=30, validation_data=(X_test, y_test),
callbacks=[[Link](patience=3)])

test_loss, test_acc = [Link](X_test, y_test, verbose=0)


print(f"Test Accuracy: {test_acc*100:.2f}%")

Module 5: Model Deployment


5.1 Saving Models and Building a Prediction API
A trained model is only useful when deployed for real-world use. Models are saved using pickle
(general Python objects), joblib (optimized for large numpy arrays), or the framework's native
format ([Link]() in Keras). A prediction API wraps the model in a Flask or FastAPI web
service.
import pickle
import joblib
from [Link] import Pipeline
from [Link] import RandomForestClassifier
from [Link] import StandardScaler
from flask import Flask, request, jsonify # pip install flask
import numpy as np

# Train and save model


pipeline = Pipeline([
("scaler", StandardScaler()),
("model", RandomForestClassifier(n_estimators=100))
])

Page 8 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

# [Link](X_train, y_train)
[Link](pipeline, "student_pass_model.pkl")
print("Model saved!")

# Load model
model = [Link]("student_pass_model.pkl")

# Flask prediction API


app = Flask(__name__)

@[Link]("/predict", methods=["POST"])
def predict():
try:
data = request.get_json()
features = [
data["study_hours"],
data["prev_marks"],
data["attendance"],
data["assignments"]
]
X = [Link](features).reshape(1, -1)
prediction = [Link](X)[0]
probability = model.predict_proba(X)[0]

return jsonify({
"prediction": "Pass" if prediction == 1 else "Fail",
"confidence": round(float(max(probability)) * 100, 2),
"pass_probability": round(float(probability[1]) * 100, 2)
})
except Exception as e:
return jsonify({"error": str(e)}), 400

if __name__ == "__main__":
[Link](debug=True, port=5001)

# Test with curl:


# curl -X POST [Link] \
# -H "Content-Type: application/json" \
# -d '{"study_hours":7,"prev_marks":80,"attendance":90,"assignments":8}'

Assignments
Assignment 1: Feature Engineering and Model Tuning
• On the Titanic dataset: create 5 new features (title from name, family size, is_alone, age
group, fare per person). Train RandomForest before and after features. Compare
accuracy.
• Apply SMOTE to a highly imbalanced credit card fraud dataset. Compare classification
reports with and without SMOTE.
• Use GridSearchCV to tune XGBoost hyperparameters (n_estimators, max_depth,
learning_rate) on a dataset of your choice.
• Plot learning curves for underfit, well-fit, and overfit models. Annotate each.
• Implement a custom cross-validation that plots the distribution of scores across folds.

Page 9 | MITS Academy | [Link]


MITS Academy — AI & Machine Learning Course

Assignment 2: Deep Learning


• Build and train a CNN on the MNIST dataset targeting 99%+ accuracy. Use
BatchNormalization and Dropout.
• Apply transfer learning: load VGG16 with ImageNet weights, freeze convolutional layers,
add custom dense layers, fine-tune on a small dataset.
• Plot training/validation accuracy and loss curves for a neural network. Identify overfitting
point.
• Build a binary text classifier (spam/not spam) using an LSTM network on the SMS Spam
dataset.
• Deploy a trained model as a Flask API and test with Postman or curl.

Projects
Project 1: Credit Card Fraud Detection System
Build a production-ready fraud detection ML pipeline:
• Dataset: Kaggle Credit Card Fraud Detection (highly imbalanced — 0.17% fraud)
• EDA: visualize class imbalance, transaction amount distribution, temporal patterns
• Handle imbalance with SMOTE + class_weight parameter comparison
• Feature engineering: transaction frequency per hour, rolling average, amount deviation
• Model comparison: Logistic Regression, Random Forest, XGBoost, LightGBM
• Optimize for recall (minimize missed fraud): tune decision threshold
• Deploy as Flask API; test with sample transactions

Project 2: Image Classification Web App


Build an end-to-end image classification system:
• Train a CNN or use transfer learning (MobileNetV2) for 5-10 class image classification
• Data augmentation pipeline for training: flips, rotations, brightness/contrast, zoom
• Training callback: ModelCheckpoint (save best model), EarlyStopping,
ReduceLROnPlateau
• Grad-CAM visualization: highlight which parts of the image the model focused on
• Flask API endpoint: accept image upload, return class + confidence
• React frontend: drag-and-drop image upload, display prediction with confidence bar

Page 10 | MITS Academy | [Link]

You might also like