0% found this document useful (0 votes)
3 views33 pages

Python 10day Notes

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

Python 10day Notes

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

■ Python for

Data Science & AI


Complete Notes + Practice Problems

15 Days · 45 Topics · 75 Practice Problems

Day 1 ■ Day 2 ■ Day 3 ■ Day 4 ■■ Day 5 ■

Day 6 ■■ Day 7 ■ Day 8 ■ Day 9 ■ Day 10 ■

Day 11 ■ Day 12 ■ Day 13 ■ Day 14 ■ Day 15 ■


Table of Contents

Day 1 ■ Python Fundamentals I Basics

Day 2 ■ Control Flow Basics

Day 3 ■ Data Structures Core

Day 4 ■■ Functions & Modules Core

Day 5 ■ File I/O & Error Handling Core

Intermediat
Day 6 ■■ OOP — Classes & Objects e

Data
Day 7 ■ NumPy Mastery Science

Data
Day 8 ■ Pandas for Data Analysis Science

Data
Day 9 ■ Data Visualization Science

Machine
Day 10 ■ Scikit-Learn & ML Basics Learning

Machine
Day 11 ■ Advanced ML & Tuning Learning

Deep
Day 12 ■ Deep Learning with PyTorch Learning

Day 13 ■ NLP & Transformers AI / LLMs

Day 14 ■ MLOps & Best Practices Production

Day 15 ■ End-to-End Project & Next Steps Capstone

Python for Data Science & AI — 15-Day Study Notes Page 2


Day 1 — Python Fundamentals I
■ Basics · 3 topics · 5 problems

Python is a dynamically typed, interpreted language. Variables need no type declaration — Python infers the type
at runtime. These building blocks are the foundation of every DS/ML script you will ever write.

■ Variables & Data Types


Python supports four primary scalar types: int (whole numbers), float (decimals), str (text), and bool (True/False).
Use type() to inspect any variable.

name = 'Alice' # str


age = 25 # int
gpa = 3.9 # float
is_student = True # bool

print(type(name)) # <class 'str'>


print(type(age)) # <class 'int'>

■ F-Strings & String Methods


F-strings (Python 3.6+) are the preferred way to embed expressions inside strings. Essential string methods
include strip(), lower(), upper(), split(), join(), replace(), and startswith().

score = 0.9427
print(f'Accuracy: {score:.2%}') # Accuracy: 94.27%
print(f'Pi ~ {3.14159:.2f}') # Pi ~ 3.14

text = ' Hello World '


print([Link]()) # 'Hello World'
print([Link]()) # ' hello world '
print([Link]()) # ['Hello', 'World']
print(','.join(['a','b','c'])) # 'a,b,c'

■ Type Casting & Operators


Type casting converts one type to another. Arithmetic operators: +, -, *, /, //, %, **. Comparison operators: ==, !=, <,
>, <=, >=. Logical: and, or, not.

x = int('42') # '42' -> 42


y = float('3.14') # '3.14' -> 3.14
z = str(100) # 100 -> '100'
b = bool(0) # 0 -> False

print(10 // 3) # 3 (floor division)


print(10 % 3) # 1 (modulo)
print(2 ** 8) # 256 (exponentiation)

Python for Data Science & AI — 15-Day Study Notes Page 3


DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Write a program that asks the user for their name and age, then prints: 'Hello Alice! In 10 years you will
be 35.'

Easy 2. Given a float 0.8763, format and print it as a percentage with 1 decimal place.

Mediu 3. Write a temperature converter: ask the user for Celsius, convert to Fahrenheit (F = C * 9/5 + 32) and
m
Kelvin (K = C + 273.15), print all three.

Mediu 4. Given the string ' Python is AMAZING ', produce: 'python is amazing' (stripped and lowercased) and
m
count how many words it contains.

Hard 5. Write a simple BMI calculator. Input: weight (kg) and height (m). Output: BMI value rounded to 2
decimal places and the category (Underweight <18.5, Normal 18.5-25, Overweight 25-30, Obese >30).

Python for Data Science & AI — 15-Day Study Notes Page 4


Day 2 — Control Flow
■ Basics · 3 topics · 5 problems

Control flow statements direct the order in which code executes. Mastering loops is critical for data iteration, model
training, and batch processing.

■ Conditionals
if/elif/else evaluates boolean expressions. Python uses indentation (4 spaces) to define code blocks — there are
no curly braces.

score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
else:
grade = 'F'
print(f'Grade: {grade}') # Grade: B

■ For Loops & enumerate


for loops iterate over any iterable. enumerate() adds an index counter — use it instead of range(len(lst)). List
comprehensions are a Pythonic shorthand.

labels = ['cat', 'dog', 'bird']


for i, label in enumerate(labels):
print(f'{i}: {label}')

# List comprehension
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]
upper = [[Link]() for w in labels]

Python for Data Science & AI — 15-Day Study Notes Page 5


■ While Loops & break/continue
while repeats as long as a condition is True. break exits the loop early; continue skips to the next iteration. Both are
common in training loops and data processing pipelines.

loss, epoch = 1.0, 0


while loss > 0.01:
loss *= 0.85
epoch += 1
print(f'Converged at epoch {epoch}')

for i in range(10):
if i == 3: continue # skip 3
if i == 7: break # stop at 7
print(i, end=' ') # 0 1 2 4 5 6

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Print all numbers from 1 to 50 that are divisible by 3 or 5 using a for loop.

Easy 2. Use a while loop to keep asking the user for a password until they type 'python123'. Print 'Access
granted' when correct.

Mediu 3. Write a program to find all prime numbers between 1 and 100 using nested loops.
m

Mediu 4. Given a list of exam scores [78,92,55,88,73,95,60,41,85,67], use list comprehension to create: (a)
m
scores above 70, (b) letter grades for each score.

Hard 5. Implement FizzBuzz for 1-100: print 'Fizz' for multiples of 3, 'Buzz' for multiples of 5, 'FizzBuzz' for
both, and the number otherwise. Then count how many of each category appeared.

Python for Data Science & AI — 15-Day Study Notes Page 6


Day 3 — Data Structures
■ Core · 3 topics · 5 problems

Python's built-in data structures — list, tuple, dict, set — are the workhorses of data science. Understanding their
properties, time complexities, and use-cases is essential.

■ Lists — Indexing, Slicing & Methods


Lists are ordered, mutable sequences. Indexing: O(1). Append: O(1). Insert/Delete at index: O(n). Slicing creates a
new list. Negative indices count from the end.

data = [3, 1, 4, 1, 5, 9, 2, 6]
data[0] # 3 (first)
data[-1] # 6 (last)
data[1:4] # [1, 4, 1]
data[::2] # every 2nd: [3, 4, 5, 2]
data[::-1] # reversed: [6, 2, 9, 5, 1, 4, 1, 3]

[Link](7) # add to end


[Link](0, 99) # insert at index 0
[Link](1) # remove first occurrence
[Link]() # in-place sort
sorted_copy = sorted(data, reverse=True)

■ Dictionaries
Dicts store key-value pairs. Keys must be hashable (str, int, tuple). Lookup: O(1). Used everywhere for
hyperparameters, configs, and word frequency counts.

params = {'lr': 0.001, 'epochs': 50, 'batch': 32}


params['lr'] # 0.001
[Link]('dropout', 0.5) # 0.5 (default)
params['optimizer'] = 'adam' # add key
del params['epochs'] # remove key

for key, val in [Link]():


print(f'{key}: {val}')

# Dict comprehension
squared = {x: x**2 for x in range(5)}

Python for Data Science & AI — 15-Day Study Notes Page 7


■ Tuples & Sets
Tuples are immutable — use for fixed data and unpacking. Sets store unique elements and support fast
membership testing O(1) and set algebra (union, intersection, difference).

# Tuple unpacking
point = (3.0, 7.5)
x, y = point

# Sets
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a & b) # {3, 4} intersection
print(a | b) # {1,2,3,4,5,6} union
print(a - b) # {1, 2} difference
print(3 in a) # True O(1) lookup

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Given the list [5,2,8,1,9,3,7,4,6], sort it ascending and descending without modifying the original. Print
all three.

Easy 2. Create a dictionary of 5 countries and their capitals. Then print only the countries whose capital
contains the letter 'a'.

Mediu 3. Given a sentence string, count the frequency of each word using a dictionary (without Counter). Then
m
find the top 3 most frequent words.

Mediu 4. You have two lists: students = ['Alice','Bob','Charlie','Diana'] and scores = [88,72,95,81]. Create a dict
m
mapping student to score, then find the student with the highest score.

Hard 5. Implement a simple inventory system using a dict. Support: add_item(name, qty), remove_item(name,
qty), check_stock(name), and list_low_stock(threshold). Test with at least 5 items.

Python for Data Science & AI — 15-Day Study Notes Page 8


■ Day 4 — Functions & Modules
■ Core · 3 topics · 5 problems

Functions are reusable blocks of code that promote modularity and readability. Python's flexible argument system
(*args, **kwargs) is heavily used in ML libraries like PyTorch and scikit-learn.

■ Defining Functions & Defaults


def creates a function. Default arguments must come after required ones. Always write a docstring. Functions are
first-class objects — they can be passed as arguments.

def train_model(data, lr=0.001, epochs=10):


'''Train and return loss history.'''
history = []
for e in range(epochs):
loss = sum(data) * lr
[Link](loss)
return history

result = train_model(my_data, lr=0.01)

■ *args and **kwargs


*args collects extra positional arguments into a tuple. **kwargs collects extra keyword arguments into a dict. Both
are heavily used in ML library APIs.

def log_metrics(*args, **kwargs):


print('Values:', args) # tuple
print('Named: ', kwargs) # dict

log_metrics(0.91, 0.88, model='RF', epoch=5)


# Values: (0.91, 0.88)
# Named: {'model': 'RF', 'epoch': 5}

# Unpacking when calling


def add(a, b, c): return a + b + c
nums = [1, 2, 3]
print(add(*nums)) # 6

Python for Data Science & AI — 15-Day Study Notes Page 9


■ Lambda & Higher-Order Functions
Lambda creates anonymous one-line functions. map() applies a function to each element. filter() keeps elements
where function returns True. sorted() with key= is the most common use.

relu = lambda x: max(0, x)


square = lambda x: x ** 2

scores = [0.9, 0.3, 0.7, 0.1, 0.8]


top = sorted(scores, key=lambda x: -x)
above = list(filter(lambda x: x > 0.5, scores))
doubled = list(map(lambda x: x * 2, scores))

# Same with comprehensions (preferred)


above2 = [x for x in scores if x > 0.5]
doubled2 = [x * 2 for x in scores]

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Write a function celsius_to_all(c) that returns a dict with keys 'fahrenheit' and 'kelvin' containing the
converted values.

Easy 2. Write a function is_palindrome(s) that returns True if the string reads the same forwards and
backwards (case-insensitive).

Mediu 3. Write a function flatten(nested_list) that takes a list of lists (any depth) and returns a flat list. E.g.
m
flatten([1,[2,[3,4]],5]) -> [1,2,3,4,5].

Mediu 4. Write a function moving_average(data, window) that returns the list of moving averages. Handle edge
m
cases where len(data) < window.

Hard 5. Implement a decorator @timer that measures and prints the execution time of any function. Test it on
a function that computes the sum of squares from 1 to 1,000,000.

Python for Data Science & AI — 15-Day Study Notes Page 10


Day 5 — File I/O & Error Handling
■ Core · 3 topics · 5 problems

Production data science requires robust file handling and error management. Data pipelines that crash silently or
lose results are useless — proper I/O and exception handling keeps systems reliable.

■ Reading & Writing Files


Always use the with statement — it guarantees the file is closed even if an exception occurs. Modes: 'r' (read), 'w'
(write/overwrite), 'a' (append), 'rb'/'wb' (binary).

# Read entire file


with open('[Link]', 'r') as f:
content = [Link]()

# Read line by line (memory efficient)


with open('big_file.txt') as f:
for line in f:
process([Link]())

# Write
with open('[Link]', 'w') as f:
for r in [0.91, 0.93, 0.95]:
[Link](f'{r}\n')

■ CSV & JSON


CSV is the most common data format. JSON is used for configs and API responses. Both have standard library
modules. For large CSVs, prefer pandas.read_csv().

import csv, json

# Read CSV
with open('[Link]') as f:
reader = [Link](f)
rows = list(reader)

# JSON round-trip
config = {'lr': 0.001, 'epochs': 50}
with open('[Link]', 'w') as f:
[Link](config, f, indent=2)
with open('[Link]') as f:
loaded = [Link](f)

Python for Data Science & AI — 15-Day Study Notes Page 11


■ Exception Handling
try/except catches errors gracefully. Catch specific exceptions before general ones. finally always runs — use for
cleanup. raise re-raises or throws new exceptions.

try:
model = load_model('[Link]')
preds = [Link](X_test)
except FileNotFoundError:
print('Model missing — train first!')
except ValueError as e:
print(f'Shape error: {e}')
except Exception as e:
print(f'Unexpected: {e}')
raise # re-raise for debugging
finally:
print('Pipeline step done.')

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Write a program that reads a text file and prints: total lines, total words, and total characters.

Easy 2. Create a JSON config file for a neural network (layers, lr, epochs, optimizer). Write a function
load_config(path) that loads and returns it, with a default if file not found.

Mediu 3. Write a CSV logger class that appends a row (timestamp, metric_name, value) to a CSV file on each
m
call to log(name, value). Include a read_all() method that returns all logged rows.

Mediu 4. Write a safe_divide(a, b) function that raises a custom exception DivisionByZeroError with a helpful
m
message when b is 0.

Hard 5. Build a simple data pipeline: read a CSV of numbers, clean it (skip non-numeric rows, log errors),
compute statistics (mean, std, min, max), and write results to a JSON report file.

Python for Data Science & AI — 15-Day Study Notes Page 12


■ Day 6 — OOP — Classes & Objects
■ Intermediate · 3 topics · 5 problems

Object-Oriented Programming organises code into reusable classes. All PyTorch models ([Link]), scikit-learn
estimators, and pandas DataFrames are classes. Understanding OOP lets you extend and build ML components
from scratch.

■ Class Basics — __init__ & Methods


self refers to the instance. __init__ is the constructor called when you create an object. Instance methods take self
as the first argument. Use _ prefix for 'private' methods by convention.

class NeuralNetwork:
framework = 'PyTorch' # class attribute

def __init__(self, layers, lr=0.001):


[Link] = layers
[Link] = lr
[Link] = []

def train(self, epochs=10):


for e in range(epochs):
loss = self._compute_loss()
[Link](loss)
return self

def _compute_loss(self):
return 1.0 / (len([Link]) + 1)

def __repr__(self):
return f'NN(layers={[Link]})'

■ Inheritance & super()


Inheritance allows a child class to reuse and extend a parent class. super() calls the parent's method. Multiple
inheritance is possible but use sparingly.

class CNN(NeuralNetwork):
def __init__(self, layers, filters, lr=0.001):
super().__init__(layers, lr)
[Link] = filters

def train(self, epochs=10):


print(f'CNN: {[Link]} filters')
return super().train(epochs)

cnn = CNN([3,64,10], filters=[32,64])


[Link](5)
print(cnn)

Python for Data Science & AI — 15-Day Study Notes Page 13


■ Magic Methods & Properties
Magic (dunder) methods customise class behaviour. Common ones: __len__, __str__, __repr__, __eq__, __iter__.
@property creates computed attributes.

class Dataset:
def __init__(self, X, y):
self.X = X
self.y = y

def __len__(self): return len(self.y)


def __getitem__(self, i): return self.X[i], self.y[i]
def __repr__(self):
return f'Dataset({len(self)} samples)'

@property
def n_features(self): return [Link][1]

ds = Dataset(X, y)
print(len(ds)) # 1000
print(ds.n_features) # 20

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Create a BankAccount class with attributes owner and balance. Add methods deposit(amount),
withdraw(amount) (raise error if insufficient funds), and __str__.

Easy 2. Create a Rectangle class with width and height. Add methods area(), perimeter(), and is_square().
Override __eq__ to compare two rectangles by area.

Mediu 3. Create a Stack class using a list internally. Implement push(item), pop(), peek(), is_empty(), and
m
__len__. Raise StackEmptyError on pop/peek when empty.

Mediu 4. Build a Student class and a Classroom class. Classroom has a list of Students and methods:
m
add_student(), remove_student(), class_average(), and top_n(n) returning top n students by grade.

Hard 5. Implement a SimpleLinearRegression class with fit(X, y), predict(X), and score(X, y) methods using
only pure Python (no numpy). Store coefficients as attributes.

Python for Data Science & AI — 15-Day Study Notes Page 14


Day 7 — NumPy Mastery
■ Data Science · 3 topics · 5 problems

NumPy is the computational backbone of Python data science. It provides n-dimensional arrays with C-speed
operations. Pandas, scikit-learn, PyTorch, and TensorFlow all use NumPy arrays internally.

■ Array Creation & Properties


NumPy's ndarray has shape (dimensions), dtype (data type), and ndim (number of dimensions). Always check
these when debugging shape mismatches in ML code.

import numpy as np

a = [Link]([1, 2, 3, 4, 5])
b = [Link]((3, 4)) # 3x4 zeros
c = [Link]((2, 2))
d = [Link](3) # identity matrix
e = [Link](100, 10) # standard normal
f = [Link](0, 10, 0.5) # 0 to 9.5, step 0.5
g = [Link](0, 1, 50) # 50 evenly spaced

print([Link]) # (100, 10)


print([Link]) # float64
print([Link]) # 2

■ Indexing, Slicing & Boolean Masking


Boolean masking is one of the most powerful NumPy features. It selects elements based on a condition and is
used constantly for data filtering and analysis.

X = [Link](1000, 20)
X[0] # first row
X[:, 5] # column 5 all rows
X[10:20, :5] # rows 10-19, cols 0-4

# Boolean masking
scores = [Link]([0.9, 0.4, 0.8, 0.2, 0.7])
mask = scores > 0.5
high = scores[mask] # [0.9, 0.8, 0.7]
idx = [Link](mask)[0] # [0, 2, 4]

Python for Data Science & AI — 15-Day Study Notes Page 15


■ Broadcasting & Linear Algebra
Broadcasting applies operations between arrays of different shapes without explicit loops. Linear algebra
operations are essential for understanding ML algorithms under the hood.

X = [Link](1000, 10)
mean = [Link](axis=0) # shape (10,)
std = [Link](axis=0) # shape (10,)
X_norm = (X - mean) / std # broadcasts!

# Linear algebra
A = [Link](3, 3)
b = [Link]([1.0, 2.0, 3.0])
x = [Link](A, b) # Ax = b
evals, evecs = [Link](A)
U, S, Vt = [Link](A) # SVD

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Create a 5x5 matrix where the diagonal is 1,2,3,4,5 and all other elements are 0. Print it. Then
compute row sums and column sums.

Easy 2. Given an array of 100 random numbers from N(0,1), count how many are > 1.96, compute mean/std,
and replace all values < -2 with exactly -2.

Mediu 3. Implement Z-score normalisation from scratch using NumPy: write normalize(X) that standardises
m
each column to mean=0, std=1. Verify on a 100x5 random matrix.

Mediu 4. Implement matrix multiplication manually using [Link]. Then implement softmax(x) =
m
exp(x)/sum(exp(x)) and verify it sums to 1.0 for any input vector.

Hard 5. Implement linear regression using only NumPy: solve the normal equation W = (X^T X)^{-1} X^T y.
Test on synthetic data y = 3x + 2 + noise and report the learned coefficients.

Python for Data Science & AI — 15-Day Study Notes Page 16


Day 8 — Pandas for Data Analysis
■ Data Science · 3 topics · 5 problems

Pandas is the primary tool for data manipulation in Python. It provides the DataFrame — a 2D table with labelled
axes. 80% of a data scientist's time is spent cleaning and exploring data with Pandas.

■ Loading & Inspecting Data


Always start with .info(), .describe(), and .isnull().sum() to understand your dataset before any modelling. These
reveal data types, missing values, and basic statistics.

import pandas as pd

df = pd.read_csv('[Link]')
[Link]() # first 5 rows
[Link]() # dtypes + null counts
[Link]() # stats summary
[Link] # (891, 12)
[Link]() # all column names
[Link]().sum() # nulls per column
df['Age'].value_counts().head(10)

■ Selecting, Filtering & Cleaning


.loc[] uses labels/conditions; .iloc[] uses integer positions. Data cleaning is the most time-consuming part of
real-world data science — master fillna, dropna, and astype.

# Selection
df['Age'] # Series
df[['Age', 'Survived']] # DataFrame
[Link][df['Age'] > 30, ['Name']] # conditional
[Link][10:20, 0:4] # positional

# Cleaning
df['Age'].fillna(df['Age'].median(), inplace=True)
[Link](subset=['Embarked'], inplace=True)
df.drop_duplicates(inplace=True)
df['Age'] = df['Age'].astype(int)

Python for Data Science & AI — 15-Day Study Notes Page 17


■ GroupBy & Feature Engineering
groupby is one of the most powerful Pandas operations. Feature engineering — creating new columns from
existing data — is how you give ML models more signal.

# GroupBy
survival = ([Link]('Pclass')['Survived']
.agg(['mean','count']).round(2))

# Feature Engineering
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
df['IsAlone'] = (df['FamilySize'] == 1).astype(int)
df['AgeBin'] = [Link](df['Age'],
bins=[0,18,35,60,100],
labels=['youth','adult','mid','senior'])
df['Title'] = df['Name'].[Link](r', ([A-Za-z]+)\.')

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Load the Titanic CSV. Find: (a) survival rate by gender, (b) average age by passenger class, (c)
percentage of missing values in each column.

Easy 2. Create a DataFrame of 5 employees with columns: name, department, salary, years. Filter to
employees with salary > 60000 or years > 5.

Mediu 3. Given a DataFrame with sales data (date, product, quantity, price), compute: daily revenue, top 5
m
products by total revenue, and month-over-month growth rate.

Mediu 4. Clean a messy dataset: handle missing values intelligently (median for numeric, mode for categorical),
m
remove duplicate rows, fix inconsistent string casing, and report what was changed.

Hard 5. Perform a full EDA on the Titanic dataset: survival rates by all categorical variables, age distribution by
survival, correlation matrix, and create at least 3 new meaningful features.

Python for Data Science & AI — 15-Day Study Notes Page 18


Day 9 — Data Visualization
■ Data Science · 3 topics · 5 problems

Visualization is how you communicate insights from data. Without EDA plots, you are modelling blind. Matplotlib
provides full control; Seaborn provides statistical beauty with minimal code.

■ Matplotlib — Figures & Subplots


Always use fig, axes = [Link]() — it gives fine-grained control. Set title, xlabel, ylabel on every plot. Save
figures with savefig before show().

import [Link] as plt


import numpy as np

fig, axes = [Link](1, 2, figsize=(12, 4))

# Training curve
axes[0].plot(train_loss, label='Train', color='#2563EB', lw=2)
axes[0].plot(val_loss, label='Val', color='#DC2626', ls='--')
axes[0].set(title='Loss Curve', xlabel='Epoch', ylabel='Loss')
axes[0].legend()
axes[0].grid(alpha=0.3)

# Histogram
axes[1].hist(data, bins=30, color='#16A34A', edgecolor='white')
plt.tight_layout()
[Link]('[Link]', dpi=150, bbox_inches='tight')

■ Seaborn — Statistical Plots


Seaborn wraps matplotlib with beautiful defaults. heatmap() for correlations, histplot(hue=) for distributions by
class, boxplot() for outliers, pairplot() for feature overviews.

import seaborn as sns


sns.set_theme(style='darkgrid')

# Correlation heatmap
[Link](figsize=(10, 8))
[Link]([Link](), annot=True, fmt='.2f',
cmap='coolwarm', center=0, square=True)

# Distribution by class
[Link](data=df, x='Age', hue='Survived', kde=True)

# Box plot for outlier detection


[Link](data=df, x='Pclass', y='Fare')

Python for Data Science & AI — 15-Day Study Notes Page 19


■ EDA Plots for Machine Learning
The confusion matrix heatmap is mandatory for classification. Feature importance bar plots explain model
decisions. Residual plots diagnose regression quality.

from [Link] import confusion_matrix

# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['No','Yes'],
yticklabels=['No','Yes'])
[Link]('Predicted'); [Link]('Actual')

# Feature importance
feat_df = [Link]({'feature': names,
'importance': importances})
feat_df.sort_values('importance').[Link](x='feature')

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Plot the sine and cosine functions from 0 to 4*pi on the same figure with different colors and a legend.
Add gridlines and a title.

Easy 2. Create a 2x2 subplot grid showing: histogram, box plot, scatter plot, and bar chart — all from the same
dataset of your choice.

Mediu 3. Load the Titanic dataset. Create a figure with 4 subplots: (a) survival count bar chart, (b) age
m
distribution by survival (overlapping histograms), (c) fare distribution by class (box), (d) correlation
heatmap.

Mediu 4. Plot training and validation loss curves for a hypothetical model over 50 epochs (simulate with random
m
data trending down). Mark the point of best validation loss with a vertical dashed line.

Hard 5. Create a complete EDA dashboard for any dataset: at least 6 different plot types, consistent color
scheme, proper labels on everything, saved as a high-res PNG. Write a caption for each plot.

Python for Data Science & AI — 15-Day Study Notes Page 20


Day 10 — Scikit-Learn & ML Basics
■ Machine Learning · 3 topics · 5 problems

Scikit-learn is the most widely used ML library in Python. Its consistent fit/transform/predict API means you can
swap any algorithm with one line of code. Mastering this API is essential for every ML practitioner.

■ The Sklearn Pattern — Split, Scale, Train, Evaluate


The golden rule: fit the scaler only on training data, then transform both train and test. Fitting on test data causes
data leakage — a common and critical mistake.

from sklearn.model_selection import train_test_split


from [Link] import StandardScaler
from [Link] import RandomForestClassifier
from [Link] import classification_report

X_tr, X_te, y_tr, y_te = train_test_split(


X, y, test_size=0.2, stratify=y, random_state=42)

scaler = StandardScaler()
X_tr_s = scaler.fit_transform(X_tr) # fit + transform
X_te_s = [Link](X_te) # transform only!

model = RandomForestClassifier(n_estimators=100)
[Link](X_tr_s, y_tr)
print(classification_report(y_te, [Link](X_te_s)))

■ Pipelines & Cross-Validation


Pipelines bundle preprocessing and modelling into one object, preventing leakage automatically. Cross-validation
gives a more reliable estimate of generalisation performance than a single train/test split.

from [Link] import Pipeline


from sklearn.model_selection import cross_val_score

pipe = Pipeline([
('scaler', StandardScaler()),
('clf', RandomForestClassifier(100))
])

cv = cross_val_score(pipe, X, y, cv=5, scoring='f1_weighted')


print(f'CV F1: {[Link]():.3f} +/- {[Link]():.3f}')

[Link](X_tr, y_tr)
print([Link](X_te, y_te))

Python for Data Science & AI — 15-Day Study Notes Page 21


■ Common Algorithms & Evaluation Metrics
For classification: accuracy (balanced data), F1 (imbalanced), ROC-AUC (probability ranking). For regression:
MAE, RMSE, R2. Always choose the metric that matches the business problem.

from sklearn.linear_model import LogisticRegression


from [Link] import DecisionTreeClassifier
from [Link] import roc_auc_score, f1_score

models = {
'LR': LogisticRegression(max_iter=1000),
'DT': DecisionTreeClassifier(max_depth=5),
'RF': RandomForestClassifier(100)
}
for name, m in [Link]():
[Link](X_tr_s, y_tr)
auc = roc_auc_score(y_te,
m.predict_proba(X_te_s)[:,1])
print(f'{name}: AUC={auc:.3f}')

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Load the sklearn iris dataset. Train a LogisticRegression. Print accuracy, classification report, and
confusion matrix. Visualise the confusion matrix.

Easy 2. Compare DecisionTree depths 1, 3, 5, 10, None on the breast cancer dataset. Plot train vs test
accuracy for each depth.

Mediu 3. Build a full sklearn Pipeline for the Titanic dataset: impute missing values, encode categoricals, scale
m
numerics, then train RandomForest. Evaluate with 5-fold cross-validation.

Mediu 4. Demonstrate data leakage: show that fitting the scaler on ALL data before splitting gives artificially
m
better results vs the correct approach. Use a real dataset.

Hard 5. Implement a model comparison framework that trains 5 different sklearn algorithms, evaluates each
with 5-fold CV, plots a boxplot of CV scores, and prints a summary table with mean +/- std.

Python for Data Science & AI — 15-Day Study Notes Page 22


Day 11 — Advanced ML & Tuning
■ Machine Learning · 3 topics · 5 problems

Getting from 'good model' to 'best model' requires systematic hyperparameter tuning and proper evaluation.
XGBoost is the industry standard for tabular data. Model explainability is increasingly required in production.

■ GridSearchCV & RandomizedSearchCV


GridSearchCV exhaustively tries all combinations — expensive but thorough. RandomizedSearchCV samples
n_iter combinations — much faster and often just as good. Always use n_jobs=-1 to parallelise.

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [3, 5, 7, None],
'min_samples_split': [2, 5, 10]
}
gs = GridSearchCV(RandomForestClassifier(42),
param_grid, cv=5,
scoring='roc_auc', n_jobs=-1)
[Link](X_tr, y_tr)
print(gs.best_params_)
print(f'Best AUC: {gs.best_score_:.4f}')

■ XGBoost — The Industry Standard


XGBoost (eXtreme Gradient Boosting) wins most tabular data competitions. Key params: learning_rate
(shrinkage), max_depth (tree complexity), subsample (row sampling), n_estimators (trees). Use early stopping to
avoid overfitting.

import xgboost as xgb

model = [Link](
n_estimators=500, learning_rate=0.05,
max_depth=5, subsample=0.8,
colsample_bytree=0.8,
early_stopping_rounds=20,
eval_metric='auc',
random_state=42, use_label_encoder=False)

[Link](X_tr, y_tr,
eval_set=[(X_te, y_te)], verbose=50)
print(f'Best iter: {model.best_iteration}')

Python for Data Science & AI — 15-Day Study Notes Page 23


■ SHAP — Explainability
SHAP (SHapley Additive exPlanations) explains individual predictions and global feature importance for any
model. Mandatory in regulated industries (finance, healthcare).

import shap

explainer = [Link](model)
shap_vals = explainer.shap_values(X_te)

# Global importance
shap.summary_plot(shap_vals, X_te,
feature_names=feature_names)

# Single prediction
shap.force_plot(explainer.expected_value,
shap_vals[0], X_te[0],
feature_names=feature_names)

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Use RandomizedSearchCV with 20 iterations to tune a GradientBoostingClassifier on any dataset.


Report best params, best CV score, and test set AUC.

Easy 2. Plot a learning curve (training size vs. train/val score) for a RandomForest. Identify whether the model
is high-bias or high-variance.

Mediu 3. Train an XGBoost model on the Titanic dataset with early stopping. Plot the training/validation AUC
m
curves by number of trees. Report the optimal number of trees.

Mediu 4. Compare Logistic Regression, Random Forest, and XGBoost using the same 5-fold CV splits. Create
m
a bar chart of mean AUC with error bars showing std.

Hard 5. Build a full AutoML-lite: for a given dataset, automatically try 5 algorithms, tune each with
RandomizedSearchCV, ensemble the top 3 with a VotingClassifier, and report a comprehensive
evaluation.

Python for Data Science & AI — 15-Day Study Notes Page 24


Day 12 — Deep Learning with PyTorch
■ Deep Learning · 3 topics · 5 problems

PyTorch is the dominant deep learning framework in research and increasingly in production. Every modern LLM
(GPT, BERT, LLaMA) is built with PyTorch. Understanding tensors and autograd is the foundation.

■ Tensors & Autograd


Tensors are like NumPy arrays but with GPU support and automatic differentiation. requires_grad=True enables
gradient computation — the engine behind backpropagation.

import torch

x = [Link]([[1.0, 2.0], [3.0, 4.0]])


x = [Link](64, 784) # random batch
x = [Link]() # move to GPU

# Autograd
w = [Link](784, 10, requires_grad=True)
loss = (x @ w).pow(2).mean()
[Link]() # compute gradients
print([Link]) # (784, 10)

# NumPy interop
arr = [Link]().detach().numpy()

■ Building Neural Networks with [Link]


All PyTorch models inherit from [Link]. Define layers in __init__ and the forward pass in forward().
[Link] chains layers for simple architectures.

import [Link] as nn

class MLP([Link]):
def __init__(self, in_dim, hid, out_dim, p=0.3):
super().__init__()
[Link] = [Link](
[Link](in_dim, hid),
nn.BatchNorm1d(hid),
[Link](),
[Link](p),
[Link](hid, out_dim))

def forward(self, x):


return [Link](x)

device = 'cuda' if [Link].is_available() else 'cpu'


model = MLP(784, 256, 10).to(device)

Python for Data Science & AI — 15-Day Study Notes Page 25


■ Training Loop & DataLoader
The training loop has 5 steps every iteration: zero gradients, forward pass, compute loss, backward pass, update
weights. DataLoader handles batching and shuffling.

import [Link] as optim


from [Link] import DataLoader, TensorDataset

optimizer = [Link]([Link](), lr=1e-3)


criterion = [Link]()
ds = TensorDataset(X_tensor, y_tensor)
loader = DataLoader(ds, batch_size=32, shuffle=True)

for epoch in range(num_epochs):


[Link]()
for X_b, y_b in loader:
X_b, y_b = X_b.to(device), y_b.to(device)
optimizer.zero_grad()
loss = criterion(model(X_b), y_b)
[Link]()
[Link]()

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Create a 3-layer MLP in PyTorch for binary classification (input=10, hidden=64, output=1 with
sigmoid). Print the model architecture and total number of trainable parameters.

Easy 2. Implement the ReLU, Sigmoid, and Tanh activation functions manually using PyTorch tensors. Plot all
three on the range [-5, 5].

Mediu 3. Train a PyTorch MLP on the sklearn digits dataset (64 features, 10 classes). Track train/val loss each
m
epoch. Plot the curves. Achieve at least 95% test accuracy.

Mediu 4. Implement a training loop with early stopping: stop training if validation loss does not improve for 10
m
consecutive epochs. Save the best model checkpoint.

Hard 5. Build a CNN for MNIST classification: 2 conv layers + 2 FC layers. Train for 10 epochs, achieve > 99%
test accuracy, and visualise 10 misclassified examples.

Python for Data Science & AI — 15-Day Study Notes Page 26


Day 13 — NLP & Transformers
■ AI / LLMs · 3 topics · 5 problems

Natural Language Processing has been revolutionised by Transformer models. HuggingFace provides pre-trained
models for hundreds of NLP tasks. You can fine-tune state-of-the-art LLMs with a few lines of Python.

■ Text Preprocessing & TF-IDF


Text preprocessing converts raw text into numerical features. TF-IDF is a strong baseline for many tasks. For
modern approaches, use tokenizer from HuggingFace instead.

import re
from sklearn.feature_extraction.text import TfidfVectorizer

def clean(text):
text = [Link]()
text = [Link](r'<.*?>', '', text) # strip HTML
text = [Link](r'[^a-z0-9\s]', '', text)
return [Link](r'\s+', ' ', text).strip()

vectorizer = TfidfVectorizer(max_features=10000,
ngram_range=(1, 2))
X = vectorizer.fit_transform(texts)
print([Link]) # (n_docs, 10000)

■ HuggingFace Pipeline API


The pipeline() function is the fastest way to use AI models. It handles tokenisation, model inference, and
post-processing automatically. Available tasks include sentiment, NER, summarisation, QA, and zero-shot
classification.

from transformers import pipeline

# Sentiment Analysis
clf = pipeline('sentiment-analysis')
print(clf('I love deep learning!'))
# [{'label': 'POSITIVE', 'score': 0.9998}]

# Zero-shot (no training!)


zsc = pipeline('zero-shot-classification')
zsc('NASA launched a new telescope',
candidate_labels=['science','sports','tech'])

# Summarisation
summ = pipeline('summarization')
summ(long_text, max_length=60)

Python for Data Science & AI — 15-Day Study Notes Page 27


■ Fine-tuning BERT
Fine-tuning adapts a pre-trained model to your specific task. With HuggingFace Trainer, this requires only defining
the dataset format and training arguments — the framework handles everything else.

from transformers import (AutoTokenizer,


AutoModelForSequenceClassification, Trainer,
TrainingArguments)

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained(
'bert-base-uncased', num_labels=2)

args = TrainingArguments(
output_dir='./out', num_train_epochs=3,
per_device_train_batch_size=16,
evaluation_strategy='epoch')

trainer = Trainer(model=model, args=args,


train_dataset=train_ds, eval_dataset=val_ds)
[Link]()

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Use HuggingFace pipeline to analyse sentiment of 10 movie reviews. Plot a bar chart showing count
of POSITIVE vs NEGATIVE predictions.

Easy 2. Use zero-shot classification to categorise 20 news headlines into 5 categories without any training.
Print each headline with its predicted category and confidence score.

Mediu 3. Build a text classification pipeline: clean text -> TF-IDF features -> Logistic Regression. Train on the
m
20 newsgroups dataset. Report accuracy and plot a confusion matrix.

Mediu 4. Use HuggingFace to: (a) summarise a long article, (b) answer 3 questions about it using QA pipeline,
m
(c) extract named entities. Use any real article.

Hard 5. Fine-tune a small BERT model (distilbert-base-uncased) on the IMDb sentiment dataset for 2 epochs.
Report train/val loss curves and final accuracy. Compare to TF-IDF + LR baseline.

Python for Data Science & AI — 15-Day Study Notes Page 28


Day 14 — MLOps & Best Practices
■ Production · 3 topics · 5 problems

MLOps bridges the gap between a working notebook and a production system. Without proper experiment
tracking, model versioning, and deployment skills, your models stay in Jupyter forever.

■ Project Structure & Environments


A consistent project structure makes collaboration easier and code reviewable. Virtual environments isolate
dependencies per project. [Link] ensures reproducibility.

# Recommended structure
my_project/
data/raw/ # NEVER modify raw data
data/processed/
notebooks/ # EDA only
src/
[Link] # data loading
[Link] # feature engineering
[Link] # model definition
[Link] # training script
models/ # saved artifacts
[Link]
[Link]

# Setup: python -m venv venv


# pip freeze > [Link]

■ MLflow — Experiment Tracking


MLflow logs parameters, metrics, and model artifacts for every experiment run. The UI lets you compare runs
visually. It integrates with sklearn, PyTorch, and XGBoost.

import mlflow
import [Link]

mlflow.set_experiment('titanic-v1')

with mlflow.start_run(run_name='rf_tuned'):
mlflow.log_params({'n_est': 200, 'depth': 5})
[Link](X_tr, y_tr)
auc = roc_auc_score(y_te,
model.predict_proba(X_te)[:,1])
mlflow.log_metric('auc', auc)
[Link].log_model(model, 'model')

# View: mlflow ui (run in terminal)

Python for Data Science & AI — 15-Day Study Notes Page 29


■ Streamlit Deployment
Streamlit turns a Python script into an interactive web app in minutes. No HTML/CSS/JS required. Deploy free on
Streamlit Community Cloud by connecting your GitHub repo.

# [Link]
import streamlit as st
import joblib, numpy as np

[Link]('ML Predictor')
model = [Link]('[Link]')

age = [Link]('Age', 1, 80, 30)


income = st.number_input('Income', 0, 200000, 50000)

if [Link]('Predict'):
X = [Link]([[age, income]])
pred = [Link](X)[0]
prob = model.predict_proba(X)[0][1]
[Link](f'Result: {pred} ({prob:.1%})')

# Run: streamlit run [Link]

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Easy 1. Set up an MLflow experiment for any classification task. Log at least 3 different parameter sets, their
metrics, and the best model. Open the MLflow UI and take a screenshot.

Easy 2. Write a Python logging setup (using the logging module) that logs INFO/WARNING/ERROR
messages both to console and to a rotating file. Test it in a simple ML pipeline.

Mediu 3. Build a Streamlit app that: (a) accepts CSV file upload, (b) shows basic stats and plots, (c) lets user
m
choose a model (LR, RF, XGBoost), (d) shows predictions and evaluation metrics.

Mediu 4. Refactor a Jupyter notebook into a proper src/ package structure with separate data loading, feature
m
engineering, training, and evaluation modules. Add a main [Link] script.

Hard 5. Create a full ML pipeline with: MLflow tracking, model registry, a REST API (using FastAPI or Flask)
that serves predictions, and a Streamlit frontend. Deploy locally and test end-to-end.

Python for Data Science & AI — 15-Day Study Notes Page 30


Day 15 — End-to-End Project & Next Steps
■ Capstone · 3 topics · 5 problems

Congratulations on completing 14 days of intensive Python for DS/AI! Today we consolidate everything into a
complete production-grade pipeline and chart your path forward.

■ Complete ML Pipeline Template


A production ML pipeline integrates every skill: loading, cleaning, feature engineering, a tuned pipeline, evaluation,
logging, and model saving. This template is a reusable starting point.

import pandas as pd, numpy as np, joblib, logging


from sklearn.model_selection import train_test_split, GridSearchCV
from [Link] import StandardScaler
from [Link] import Pipeline
from [Link] import GradientBoostingClassifier
from [Link] import classification_report, roc_auc_score

log = [Link](__name__)
[Link](level=[Link])

df = pd.read_csv('data/raw/[Link]')
[Link](f'Loaded {len(df)} rows')

# Clean + engineer features


[Link]([Link](numeric_only=True), inplace=True)
df['ratio'] = df['feat_a'] / (df['feat_b'] + 1e-8)

X, y = [Link]('target',axis=1).values, df['target'].values
X_tr,X_te,y_tr,y_te = train_test_split(
X,y,test_size=0.2,stratify=y,random_state=42)

pipe = Pipeline([('sc',StandardScaler()),
('clf',GradientBoostingClassifier())])
gs = GridSearchCV(pipe,
{'clf__n_estimators':[100,200],'clf__lr':[0.05,0.1]},
cv=5, scoring='roc_auc', n_jobs=-1)
[Link](X_tr, y_tr)
[Link](f'Best AUC: {gs.best_score_:.4f}')
[Link](gs.best_estimator_, 'models/[Link]')

Python for Data Science & AI — 15-Day Study Notes Page 31


■ Kaggle Workflow
Kaggle competitions are the best way to practice end-to-end ML. The workflow: EDA -> baseline -> feature
engineering -> tuning -> ensembling -> submission.

# Kaggle workflow skeleton


import kaggle

# 1. EDA: understand the data deeply


# 2. Baseline: simple model as reference
# 3. Feature engineering: create new features
# 4. Tuning: GridSearch / Optuna
# 5. Ensemble: blend predictions
# final = 0.5*xgb_pred + 0.3*rf_pred + 0.2*lr_pred
# 6. Submit and iterate

# Recommended starter competitions:


# - Titanic (classification)
# - House Prices (regression)
# - Digit Recognizer (CNN)
# - Natural Language Processing Disaster Tweets

■ What to Learn Next


The 15-day plan gives you the foundation. Specialise in one area and build 2-3 portfolio projects. Employers care
about GitHub projects, not certificates.

# Learning roadmap after Day 15

# Track 1: Data Analyst


# SQL (2 weeks) -> Advanced Pandas -> Tableau/Power BI

# Track 2: ML Engineer
# XGBoost deep dive -> Feature Stores -> Docker -> AWS

# Track 3: Deep Learning / AI


# PyTorch CNNs -> RNNs -> Transformers -> LLMs -> RAG

# Track 4: NLP / LLMOps


# HuggingFace -> LangChain -> Fine-tuning -> Agents

# Portfolio projects to build:


# 1. Kaggle Titanic end-to-end + blog post
# 2. Sentiment analyser + Streamlit app + GitHub
# 3. Image classifier + REST API deployment
# 4. Chatbot / RAG app using LangChain

DS/ML Relevance: These skills are used directly in production data pipelines, model training scripts, and ML
engineering roles.

Practice Problems

Python for Data Science & AI — 15-Day Study Notes Page 32


Easy 1. Download the Titanic dataset from Kaggle. Run the complete pipeline from this day's template. Submit
your predictions and note your leaderboard position.

Mediu 2. Build a complete end-to-end project on the House Prices dataset: EDA + feature engineering +
m
stacked model + submission. Write a README explaining every decision.

Mediu 3. Build and deploy a Streamlit ML app on any dataset. Push to GitHub and deploy on Streamlit
m
Community Cloud. Share the public URL.

Hard 4. Create a portfolio project combining everything: scrape or download a real dataset, perform full EDA,
engineer features, train and tune multiple models, track experiments with MLflow, serve predictions with
FastAPI, and build a Streamlit frontend.

Hard 5. CAPSTONE: Pick any Kaggle competition, build your best model, write a 500-word technical blog post
explaining your approach, and publish it on Medium or a personal site.

Python for Data Science & AI — 15-Day Study Notes Page 33

You might also like