0% found this document useful (0 votes)
1 views38 pages

Python DataScience Complete Guide

The document is a comprehensive guide to Python for Data Science, covering topics from foundational Python concepts to advanced machine learning and deep learning techniques. It includes practical examples, projects, and exercises, with a structured table of contents divided into multiple parts such as NumPy, Pandas, Data Visualization, and NLP. The guide aims to equip learners with the skills needed for a successful career in data science, featuring industry-standard practices and tools.

Uploaded by

nme2it44
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)
1 views38 pages

Python DataScience Complete Guide

The document is a comprehensive guide to Python for Data Science, covering topics from foundational Python concepts to advanced machine learning and deep learning techniques. It includes practical examples, projects, and exercises, with a structured table of contents divided into multiple parts such as NumPy, Pandas, Data Visualization, and NLP. The guide aims to equip learners with the skills needed for a successful career in data science, featuring industry-standard practices and tools.

Uploaded by

nme2it44
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
Complete Mastery Guide — Beginner to Advanced
NumPy Pandas Matplotlib Scikit-Learn TensorFlow Deep Learning

■ 500+ Real-World Examples ■ Step-by-Step Diagrams ■ Projects & Exercises

■ NumPy & Pandas Mastery ■ ML & Deep Learning ■ NLP & Computer Vision

■ Statistics & Probability ■ Data Visualization ■ Interview Prep & Tips

World-Class Study Guide • Beginner → Expert • Industry Standards


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

■ TABLE OF CONTENTS

PART 1: PYTHON FOUNDATIONS


Ch 1: Python Environment Setup & Jupyter Notebooks ·············· pg 6
Ch 2: Variables, Data Types & Operators ························· pg 14
Ch 3: Control Flow — Conditionals, Loops, Comprehensions ········ pg 26
Ch 4: Functions, Lambda & Functional Programming ················ pg 40
Ch 5: OOP for Data Scientists ··································· pg 54

PART 2: NUMPY — NUMERICAL COMPUTING


Ch 6: NumPy Arrays — Creation & Operations ······················ pg 70
Ch 7: Indexing, Slicing & Broadcasting ·························· pg 86
Ch 8: Mathematical & Statistical Functions ······················ pg 100
Ch 9: Linear Algebra with NumPy ································· pg 114

PART 3: PANDAS — DATA MANIPULATION


Ch 10: Series & DataFrame Fundamentals ·························· pg 130
Ch 11: Loading Data — CSV, Excel, JSON, SQL ····················· pg 146
Ch 12: Data Cleaning & Preprocessing ···························· pg 160
Ch 13: GroupBy, Merge, Join & Reshape ··························· pg 176
Ch 14: Time Series Analysis ····································· pg 194

PART 4: DATA VISUALIZATION


Ch 15: Matplotlib — Complete Guide ······························ pg 212
Ch 16: Seaborn — Statistical Visualization ······················ pg 234
Ch 17: Plotly — Interactive Charts ······························ pg 252

PART 5: STATISTICS & PROBABILITY


Ch 18: Descriptive Statistics & EDA ····························· pg 272
Ch 19: Probability Theory & Distributions ······················· pg 290
Ch 20: Hypothesis Testing ······································· pg 310
Ch 21: Correlation & Regression Analysis ························ pg 328

PART 6: MACHINE LEARNING


Ch 22: ML Fundamentals & Scikit-Learn ··························· pg 350
Ch 23: Supervised Learning — Regression ························· pg 370
Ch 24: Supervised Learning — Classification ····················· pg 392
Ch 25: Unsupervised Learning — Clustering & PCA ················· pg 416
Ch 26: Model Evaluation & Hyperparameter Tuning ················· pg 438
Ch 27: Ensemble Methods — Random Forest, XGBoost ················ pg 460

PART 7: DEEP LEARNING


Ch 28: Neural Networks from Scratch ····························· pg 486
Ch 29: TensorFlow & Keras ······································· pg 510
Ch 30: CNNs — Convolutional Neural Networks ····················· pg 534
Ch 31: RNNs & LSTMs — Sequence Modelling ························ pg 558
Ch 32: Transfer Learning & Fine-Tuning ·························· pg 580

PART 8: NLP & COMPUTER VISION

© Python Data Science Mastery Guide 2


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Ch 33: Natural Language Processing ······························ pg 604


Ch 34: Transformers & BERT ······································ pg 628
Ch 35: Computer Vision with OpenCV ······························ pg 652

PART 9: ADVANCED & CAPSTONE


Ch 36: Feature Engineering Masterclass ·························· pg 678
Ch 37: MLOps — Deployment & Monitoring ·························· pg 700
Ch 38: Big Data with PySpark ···································· pg 722
Ch 39: Capstone Projects & Interview Prep ······················· pg 744

© Python Data Science Mastery Guide 3


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 1

Python Environment Setup & Jupyter


Notebooks

1.1 Why Python for Data Science?

Python dominates data science, machine learning and AI because of its readable syntax, massive
ecosystem, and world-class community. Companies like Google, Netflix, Uber and NASA rely on
Python for data pipelines and ML systems. Learning Python for data science opens doors to one of
the highest-paying career paths in tech.

The Complete Data Science Pipeline

Data Clean Feature Model


Collect → & EDA → Eng. → Train → Evaluate
→ Deploy

Language Ease of Learn ML Libraries Community Industry Use

Python ■■■■■ ■■■■■ ■■■■■ ■■■■■

R ■■■ ■■■■ ■■■■ ■■■

Julia ■■■ ■■■ ■■ ■■

MATLAB ■■ ■■ ■■■ ■■

1.2 Installing Anaconda — The Data Science Platform

Anaconda bundles Python 3.10+, Jupyter, and 250+ pre-installed packages. It is the recommended
setup for data scientists at every level.

• Download from [Link]/download — choose Python 3.10+


• Windows: run .exe installer and check "Add Anaconda to PATH"
• macOS: run .pkg installer OR brew install --cask anaconda
• Linux: bash Anaconda3-*.sh → accept license → confirm init
• Verify: open terminal → conda --version && python --version

© Python Data Science Mastery Guide 4


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# Verify installation
import sys
print(f'Python: {[Link]}')

# Check key libraries


import numpy as np
import pandas as pd
import sklearn
print(f'NumPy : {np.__version__}')
print(f'Pandas : {pd.__version__}')
print(f'sklearn : {sklearn.__version__}')
# → Python 3.11.x | NumPy 1.26.x | Pandas 2.x | sklearn 1.3.x

1.3 Virtual Environments — Best Practice

NOTE
Always isolate project dependencies in a virtual environment. This prevents
version conflicts and makes projects reproducible.

Python

# conda — recommended
conda create -n ds_env python=3.11
conda activate ds_env
conda install numpy pandas matplotlib seaborn scikit-learn
pip install tensorflow torch jupyter

# venv alternative
python -m venv myenv
source myenv/bin/activate # Linux/macOS
myenv\Scripts\activate # Windows

# Save & restore environment


pip freeze > [Link]
pip install -r [Link]

1.4 Jupyter Notebook & JupyterLab

Jupyter is the standard tool for interactive data exploration. You write code in cells, see results inline,
and weave in Markdown explanations — perfect for sharing analyses.

© Python Data Science Mastery Guide 5


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

jupyter notebook # Classic interface (port 8888)


jupyter lab # Modern IDE-like interface

# Essential magic commands


%timeit sum(range(10000)) # Benchmark a line
%%time # Time an entire cell
%matplotlib inline # Show plots in notebook
%who # List all variables
%reset # Clear workspace
%load_ext autoreload # Auto-reload modules

# Keyboard shortcuts
# Shift+Enter → Run cell & go to next
# Ctrl+Enter → Run cell, stay
# A / B → Insert cell Above / Below
# M / Y → Markdown / Code mode
# DD → Delete cell
# Ctrl+Z → Undo in cell

TIP
Use JupyterLab for a full IDE experience: file browser, terminal, multiple tabs,
git integration, and rich extension ecosystem.

© Python Data Science Mastery Guide 6


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 2

Python Basics — Variables, Data Types &


Operators

2.1 Variables & Dynamic Typing

Python is dynamically typed — no type declarations needed. Variables are created on assignment
and can be reassigned to any type. This flexibility speeds up development but requires discipline in
data science to avoid silent type errors.

Python

# Variable assignment
name = 'Alice' # str
age = 28 # int
height = 1.72 # float
is_active = True # bool
score = None # NoneType

# Multiple / unpacking
x = y = z = 0 # All equal 0
a, b, c = 1, 2, 3 # Tuple unpack
first, *rest = [1,2,3,4,5] # Extended unpack
# first=1, rest=[2,3,4,5]

# Type checking
print(type(name)) # <class 'str'>
print(isinstance(age, int)) # True

# Type conversion
num_str = '42'
num_int = int(num_str) # 42
num_flt = float(num_str) # 42.0
back = str(num_int) # '42'

Type Example Mutable Notes

int 42, -7, 0 No Arbitrary precision

float 3.14, -0.5 No 64-bit double

complex 3+2j No Real + imaginary

str "hello" No Unicode, immutable

bool True / False No Subclass of int

© Python Data Science Mastery Guide 7


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

list [1,2,3] Yes Ordered, indexed

tuple (1,2,3) No Ordered, fixed

dict {"a":1} Yes Key-value pairs

set {1,2,3} Yes Unique elements

NoneType None No Null / missing

2.2 Strings — Text Processing in Data Science

Python

s1 = 'single quotes'
s2 = "double quotes"
s3 = """multi
line"""
s4 = r'raw\nstring' # Backslash literal

# f-Strings (Python 3.6+) — use always!


name, score = 'Alice', 95.5
print(f'Student: {name}, Score: {score:.2f}%')
# → Student: Alice, Score: 95.50%

# Essential string methods for data cleaning


text = ' Hello, World! '
[Link]() # Remove whitespace
[Link]() # lowercase
[Link]() # UPPERCASE
[Link](',', '') # Remove commas
[Link](',') # Split to list
','.join(['a','b','c']) # Join list
'World' in text # True — substring check
[Link]("l") # 3
[Link](" ") # True

2.3 Lists & Slicing — Core Data Structure

© Python Data Science Mastery Guide 8


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

nums = [1, 2, 3, 4, 5]
mixed = [1, 'hello', 3.14, True, None]
matrix = [[1,2,3],[4,5,6],[7,8,9]] # 2D list

# Indexing (0-based, negative counts from end)


print(nums[0]) # 1 (first)
print(nums[-1]) # 5 (last)
print(nums[1:4]) # [2,3,4]
print(nums[::2]) # [1,3,5] every 2nd
print(nums[::-1]) # [5,4,3,2,1] reversed

# Mutating
[Link](6) # add to end
[Link](0, 0) # insert at index
[Link](3) # remove first match
[Link]() # remove & return last
[Link]() # sort in-place
sorted(nums) # return new sorted list

# List comprehensions — fast & Pythonic


squares = [x**2 for x in range(1,11)]
evens = [x for x in range(20) if x%2==0]
flat = [v for row in matrix for v in row]

KEY CONCEPT
List comprehensions are 2–5× faster than equivalent for-loops and are the standard
Python idiom in data science for building arrays before converting to NumPy.

2.4 Dictionaries & Sets

© Python Data Science Mastery Guide 9


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# dict — hash map


student = {'name':'Alice', 'age':23, 'gpa':3.8}
student['major'] = 'Data Science' # add
[Link]('city', 'N/A') # safe access
del student['age']

for k, v in [Link]():
print(f'{k}: {v}')

# Dict comprehension
scores = {'Alice':95,'Bob':87,'Carol':91}
grades = {n: 'A' if s>=90 else 'B'
for n,s in [Link]()}

# Counter — counting frequencies (NLP essential)


from collections import Counter
words = ['the','cat','sat','on','the','mat','the']
c = Counter(words)
print(c.most_common(3)) # [('the',3),('cat',1)...]

# set — unique elements


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

© Python Data Science Mastery Guide 10


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 3

Control Flow — Conditionals, Loops &


Comprehensions

3.1 Conditionals

Python

score = 85
if score >= 90: grade = 'A'
elif score >= 80: grade = 'B'
elif score >= 70: grade = 'C'
else: grade = 'F'

# One-liner ternary
result = 'Pass' if score >= 60 else 'Fail'

# match-case (Python 3.10+)


match grade:
case 'A': print('Excellent!')
case 'B': print('Good job!')
case _: print('Keep studying')

3.2 Loops

© Python Data Science Mastery Guide 11


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# for loop over range


for i in range(5):
print(i, end=" ") # 0 1 2 3 4

# while loop
n = 10
while n > 0: n -= 2

# enumerate — index + value


fruits = ['apple','banana','cherry']
for idx, fruit in enumerate(fruits):
print(f'{idx}: {fruit}')

# zip — parallel iteration


names = ['Alice','Bob','Carol']
scores = [95, 87, 91]
for name, score in zip(names, scores):
print(f'{name}: {score}')

# break / continue / else


for n in range(2, 20):
for d in range(2, n):
if n % d == 0: break
else: print(f'{n} is prime')

3.3 Comprehensions — Pythonic Power

Python

# List comprehension
squares = [x**2 for x in range(10)]
even_sq = [x**2 for x in range(10) if x%2==0]

# Nested (matrix flatten)


matrix = [[1,2,3],[4,5,6],[7,8,9]]
flat = [v for row in matrix for v in row]

# Dict comprehension
word_len = {w: len(w) for w in ['cat','elephant','fox']}

# Set comprehension
unique_sq = {x**2 for x in range(-5, 6)}

# Generator expression — memory-efficient


gen = (x**2 for x in range(10**6))
total = sum(gen) # Lazy evaluation, no big list

© Python Data Science Mastery Guide 12


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 4

Functions, Lambda & Functional


Programming

4.1 Defining & Calling Functions

Python

def greet(name, greeting="Hello"):


"""Greet a person. Docstring goes here."""
return f"{greeting}, {name}!"

print(greet("Alice")) # Hello, Alice!


print(greet("Bob", "Hi")) # Hi, Bob!

# *args — variable positional arguments


def mean(*nums):
return sum(nums) / len(nums)

print(mean(3, 5, 7, 9)) # 6.0

# **kwargs — variable keyword arguments


def profile(**info):
for k,v in [Link](): print(f"{k}: {v}")

profile(name="Alice", age=23, city="NYC")

# Type hints (Python 3.5+) — strongly recommended


def bmi(weight: float, height: float) -> float:
return round(weight / height**2, 2)

4.2 Lambda & Functional Programming

© Python Data Science Mastery Guide 13


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# Lambda — anonymous one-liner function


square = lambda x: x**2
add = lambda x,y: x+y
classify= lambda s: "A" if s>=90 else "B" if s>=80 else "C"

# map() — transform every element


nums = [1,2,3,4,5]
doubled = list(map(lambda x: x*2, nums)) # [2,4,6,8,10]

# filter() — keep matching elements


evens = list(filter(lambda x: x%2==0, nums)) # [2,4]

# reduce() — fold to single value


from functools import reduce
product = reduce(lambda x,y: x*y, nums) # 120

# sorted() with key


students = [('Alice',95),('Bob',87),('Carol',91)]
[Link](key=lambda t: t[1], reverse=True)
# [('Alice',95),('Carol',91),('Bob',87)]

# Decorators
def log(fn):
def wrapper(*a,**kw):
print(f"Calling {fn.__name__}")
return fn(*a,**kw)
return wrapper

@log
def add(x,y): return x+y

© Python Data Science Mastery Guide 14


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 6

NumPy Arrays — The Foundation of


Numerical Computing

6.1 Why NumPy?

NumPy provides the ndarray — a multidimensional array stored in contiguous memory, processed via
C/Fortran kernels. Operations are vectorized (no Python loops), making NumPy 100–1000× faster
than pure Python for numerical work. Every major data science library is built on NumPy arrays.

NumPy 2D Array — Shape (3, 4)


Shape: (3,4) ← columns →

1 2 3 4

rows → 5 6 7 8

9 10 11 12

NOTE
NumPy arrays have a fixed data type (dtype) and fixed size. This enables
contiguous memory storage and SIMD (vectorized) CPU instructions — the secret
behind its speed.

© Python Data Science Mastery Guide 15


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import numpy as np

# Creating arrays
a1 = [Link]([1,2,3,4,5]) # 1-D
a2 = [Link]([[1,2,3],[4,5,6]]) # 2-D
a3 = [Link](0, 10, 0.5) # 0..9.5
a4 = [Link](0, 1, 100) # 100 pts
z = [Link]((3,4)) # 3×4 zeros
o = [Link]((2,3,4)) # 3-D ones
I = [Link](4) # Identity
r = [Link](3,3) # Uniform [0,1)
rn = [Link](1000) # Normal
ri = [Link](0,100,(5,5)) # Random ints

# Array properties
arr = [Link]([[1,2,3],[4,5,6]])
print([Link]) # (2, 3)
print([Link]) # 2
print([Link]) # 6
print([Link]) # int64
print([Link]) # 48

6.2 Vectorized Operations & Broadcasting

© Python Data Science Mastery Guide 16


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import numpy as np
a = [Link]([1,2,3,4])
b = [Link]([10,20,30,40])

# Element-wise — no loops!
print(a + b) # [11 22 33 44]
print(a * b) # [10 40 90 160]
print(a ** 2) # [1 4 9 16]
print([Link](b)) # [3.16 4.47 5.47 6.32]

# Broadcasting
M = [Link]([[1,2,3],[4,5,6],[7,8,9]])
row = [Link]([10,20,30]) # shape (3,)
M + row # [[11,22,33],[14,25,36],[17,28,39]]

# Boolean masking — most used in EDA


data = [Link]([15,32,8,45,22,67,3,91])
print(data[data > 30]) # [32 45 67 91]
print(data[(data>20) & (data<60)]) # [32 45 22]

# Universal functions (ufuncs)


x = [Link](-[Link], [Link], 100)
y = [Link](x)
z = [Link](-x**2) # Gaussian

6.3 Indexing, Slicing & Reshaping

© Python Data Science Mastery Guide 17


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

arr = [Link](24).reshape(4,6)

# Indexing
print(arr[0,0]) # 0
print(arr[-1,-1]) # 23
print(arr[1,:]) # row 1
print(arr[:,2]) # column 2
print(arr[0:2,0:3]) # sub-matrix

# Fancy indexing
print(arr[[0,2]]) # rows 0 and 2

# Reshape
flat = [Link]() # 1-D copy
view = [Link]() # 1-D view
cube = [Link](2,3,4) # 3-D
T = arr.T # Transpose

# Stack & split


a = [Link]([[1,2],[3,4]])
b = [Link]([[5,6],[7,8]])
h = [Link]([a,b]) # cols side by side
v = [Link]([a,b]) # rows stacked

6.4 Statistical Functions

Python

import numpy as np
data = [Link]([23,45,12,67,34,89,56,78,43,65])

print(f'Mean : {[Link](data):.2f}')
print(f'Median : {[Link](data):.2f}')
print(f'Std : {[Link](data):.2f}')
print(f'Var : {[Link](data):.2f}')
print(f'Min/Max: {[Link](data)} / {[Link](data)}')

# Percentiles (IQR for outlier detection)


q1,q2,q3 = [Link](data,[25,50,75])
iqr = q3-q1
lower,upper = q1-1.5*iqr, q3+1.5*iqr

# Linear algebra
A = [Link]([[1,2],[3,4]])
det = [Link](A) # -2.0
inv = [Link](A)
eigv,eigvec = [Link](A)
U,S,Vt = [Link](A) # SVD

© Python Data Science Mastery Guide 18


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 10

Pandas — Series & DataFrame


Fundamentals

10.1 What is Pandas?

Pandas is the most essential library for data manipulation. It provides two data structures: Series (1-D
labeled array) and DataFrame (2-D labeled table with mixed types). Pandas combines the speed of
NumPy with the convenience of spreadsheet operations.

Pandas DataFrame — Structured Tabular Data


Name Age Score Grade

Alice 23 95.2 A

Bob 25 87.5 B+

Carol 22 92.1 A-

David 24 78.9 C+

DataFrame: 4 rows × 4 cols

© Python Data Science Mastery Guide 19


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import pandas as pd
import numpy as np

# ■■ Series ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
s = [Link]([10,20,30,40], index=['a','b','c','d'])
print(s['a']) # 10
print([Link]) # array([10,20,30,40])

# ■■ DataFrame ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
df = [Link]({
'Name' : ['Alice','Bob','Carol','David'],
'Age' : [23,25,22,24],
'Score': [95.2,87.5,92.1,78.9],
'Dept' : ['CS','Math','CS','Physics']
})

# Inspection
[Link] # (4, 4)
[Link] # data types per column
[Link](3) # first 3 rows
[Link]() # non-null counts + dtypes
[Link]() # count/mean/std/min/q/max
[Link]() # unique values per column

10.2 Selecting & Filtering Data

© Python Data Science Mastery Guide 20


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# Column selection
df['Name'] # Single → Series
df[['Name','Score']] # Multiple → DataFrame

# Row selection
[Link][0] # by label
[Link][0:2,'Name':'Score'] # slice by labels
[Link][0] # by integer position
[Link][-1] # last row

# Boolean filtering — most important!


high = df[df["Score"] >= 90]
cs = df[df["Dept"] == "CS"]
# Combine conditions
top_cs = df[(df["Score"]>=90) & (df["Dept"]=="CS")]
# SQL-style query
[Link]("Score >= 90 and Dept == 'CS'")

# isin() — multiple value filter


df[df['Dept'].isin(['CS','Math'])]

# str accessor — text filtering


df[df['Name'].[Link]('A')]

10.3 Data Cleaning

© Python Data Science Mastery Guide 21


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

# Missing values
[Link]().sum() # NaN count per col
[Link]().mean()*100 # % missing
[Link]() # drop NaN rows
df['Score'].fillna(df['Score'].mean())
[Link](0) # fill all NaN

# Duplicates
[Link]().sum()
df.drop_duplicates()
df.drop_duplicates(subset=['Name'])

# Type conversion
df['Date'] = pd.to_datetime(df['Date'])
df['Score'] = pd.to_numeric(df['Score'], errors='coerce')

# String cleaning
df['Name'] = df['Name'].[Link]().[Link]()
df['Phone'] = df['Phone'].[Link]('-','')

# Rename columns
[Link](columns={'old':'new'}, inplace=True)
[Link] = [Link]().[Link](" ","_")

10.4 GroupBy, Merge & Pivot

Python

# GroupBy
dept_stats = [Link]('Dept')['Score'].agg(
mean='mean', std='std', count='count')

# Multiple columns groupby


[Link](['Dept','Grade'])['Score'].mean()

# Merge (like SQL JOIN)


df1 = [Link]({"id":[1,2,3],"name":["A","B","C"]})
df2 = [Link]({"id":[1,2,4],"score":[90,85,78]})
merged = [Link](df1, df2, on='id', how='inner')
left = [Link](df1, df2, on='id', how='left')

# Pivot table
pd.pivot_table(df, values='Score',
index='Dept', columns='Grade',
aggfunc='mean', fill_value=0)

# Apply custom functions


df['Grade'] = df['Score'].apply(
lambda s: 'A' if s>=90 else 'B' if s>=80 else 'C')

© Python Data Science Mastery Guide 22


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 15

Matplotlib & Seaborn — Data Visualization

15.1 Matplotlib Architecture

Matplotlib uses a hierarchy: Figure (canvas) → Axes (plot area) → Artists (lines, labels etc.). Always
use the object-oriented API (fig, ax = [Link]()) for full control.

Python

import [Link] as plt


import numpy as np

fig, ax = [Link](figsize=(10,6))

x = [Link](0, 2*[Link], 100)


[Link](x, [Link](x), label='sin(x)', color='#1565C0', lw=2)
[Link](x, [Link](x), label='cos(x)', color='#E74C3C', lw=2)
ax.fill_between(x, [Link](x), alpha=0.15)

ax.set_title('Trig Functions', fontsize=16, fontweight='bold')


ax.set_xlabel('x (radians)')
ax.set_ylabel('f(x)')
[Link](); [Link](True, alpha=0.3)

[Link]('[Link]', dpi=300, bbox_inches='tight')

# Multiple subplots
fig, axes = [Link](2, 3, figsize=(15,8))
for i, ax in enumerate([Link]):
[Link]([Link](100).cumsum())
ax.set_title(f'Random Walk {i+1}')
plt.tight_layout()

15.2 Chart Types

© Python Data Science Mastery Guide 23


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import [Link] as plt


import numpy as np

fig, axes = [Link](2,3,figsize=(14,8))

# 1. Line chart
axes[0,0].plot([1,3,5,7,9],[2,4,3,8,6],'b-o')
axes[0,0].set_title('Line Chart')

# 2. Bar chart
cats=['A','B','C','D']
vals=[25,40,30,55]
axes[0,1].bar(cats, vals, color='#1565C0')
axes[0,1].set_title('Bar Chart')

# 3. Histogram
data = [Link](1000)
axes[0,2].hist(data, bins=30, color='#42A5F5', edgecolor='white')
axes[0,2].set_title('Histogram')

# 4. Scatter plot
x,y = [Link](200),[Link](200)
axes[1,0].scatter(x,y,alpha=0.5,color='#E74C3C')
axes[1,0].set_title('Scatter Plot')

# 5. Box plot
groups = [[Link](100) for _ in range(4)]
axes[1,1].boxplot(groups, labels=['G1','G2','G3','G4'])
axes[1,1].set_title('Box Plot')

# 6. Heatmap (manual)
mat = [Link](6,6)
axes[1,2].imshow(mat, cmap='Blues')
axes[1,2].set_title('Heatmap')
plt.tight_layout()

15.3 Seaborn — Statistical Visualization

© Python Data Science Mastery Guide 24


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import seaborn as sns


import [Link] as plt

sns.set_theme(style='whitegrid', palette='husl')
tips = sns.load_dataset('tips') # Built-in dataset

# Distribution
[Link](tips['total_bill'], kde=True)
[Link](x='day', y='total_bill', data=tips)
[Link](x='day', y='tip', hue='sex', data=tips)

# Relationships
[Link](x='total_bill',y='tip',hue='sex',data=tips)
[Link](x='total_bill',y='tip',data=tips) # +regression

# Categorical
[Link](x='day',y='total_bill',data=tips,ci=95)
[Link](x='day',data=tips)

# Correlation heatmap
corr = tips.select_dtypes("number").corr()
[Link](corr, annot=True, fmt='.2f', cmap='coolwarm')

# Pair plot — all vs all


[Link](tips, hue='sex', diag_kind='kde')

© Python Data Science Mastery Guide 25


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 18

Statistics & Probability for Data Science

18.1 Descriptive Statistics

Python

import pandas as pd
import numpy as np
from scipy import stats

data = [Link]([12,23,34,45,56,23,34,45,67,78,23,45])

# Central tendency
print(f'Mean : {[Link]():.2f}')
print(f'Median : {[Link]():.2f}')
print(f'Mode : {[Link]()[0]}')

# Spread
print(f'Std : {[Link]():.2f}')
print(f'Var : {[Link]():.2f}')
print(f'IQR : {[Link](.75)-[Link](.25):.2f}')

# Shape
print(f'Skew : {[Link]():.2f}') # 0=symmetric
print(f'Kurt : {[Link]():.2f}') # 0=normal

# Full summary
[Link](percentiles=[.1,.25,.5,.75,.9])

18.2 Probability Distributions

© Python Data Science Mastery Guide 26


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import numpy as np
import [Link] as plt
from [Link] import norm, binom, poisson, uniform

# Normal distribution
mu, sigma = 100, 15
x = [Link](50,150,200)
pdf = [Link](x, mu, sigma)

# P(X < 120)


print(f"P(IQ<120): {[Link](120,mu,sigma):.4f}")
# P(90 < X < 110)
print(f"P(90<X<110): {[Link](110,mu,sigma)-[Link](90,mu,sigma):.4f}")

# Binomial
n,p = 10, 0.5 # 10 flips, fair coin
print(f"P(X=5): {[Link](5,n,p):.4f}")
print(f"P(X>=7): {[Link](6,n,p):.4f}")

# Poisson
lam = 3 # avg 3 events/hr
print(f"P(X=5): {[Link](5,lam):.4f}")

# Sampling from distributions


normal_samples = [Link](mu, sigma, 10000)
binom_samples = [Link](n, p, 10000)

18.3 Hypothesis Testing

© Python Data Science Mastery Guide 27


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

from scipy import stats


import numpy as np

# One-sample t-test
# H0: population mean = 100
sample = [Link](102, 15, 50)
t_stat, p_val = stats.ttest_1samp(sample, 100)
print(f't={t_stat:.3f}, p={p_val:.4f}')
conclusion = 'Reject H0' if p_val < 0.05 else 'Fail to reject H0'

# Two-sample t-test
groupA = [Link](50, 10, 30)
groupB = [Link](55, 12, 30)
t, p = stats.ttest_ind(groupA, groupB)
print(f'Two-sample: t={t:.3f}, p={p:.4f}')

# Chi-square test (categorical)


observed = [Link]([[10,20,30],[15,25,20]])
chi2, p, dof, expected = stats.chi2_contingency(observed)
print(f'Chi2={chi2:.3f}, p={p:.4f}, dof={dof}')

# Correlation test
x = [Link](100)
y = 2*x + [Link](100)
r, p = [Link](x, y)
print(f'r={r:.3f}, p={p:.4f}')

NOTE
A p-value < 0.05 means we reject the null hypothesis at 5% significance level.
ALWAYS check assumptions: normality, equal variance, independence, sample size.

© Python Data Science Mastery Guide 28


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 22

Machine Learning with Scikit-Learn

22.1 The Machine Learning Workflow

Machine Learning Workflow

Train Feature ML
Predict
Data Eng. Model

Test Eval
Data Metrics

Category Task Algorithms Example

Supervised Regression Linear, Ridge, RF House price prediction

Supervised Classification Logistic, SVM, XGB Spam detection

Unsupervised Clustering K-Means, DBSCAN Customer segments

Unsupervised Dim. Reduction PCA, t-SNE Visualization

Ensemble Both Random Forest, XGB Competitions

Deep Learning Both Neural Networks Images, text, speech

NOTE
Scikit-Learn follows a universal API: [Link](X_train, y_train) →
.predict(X_test) → .score(X_test, y_test). This works for ALL 100+ algorithms!

© Python Data Science Mastery Guide 29


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

from sklearn.model_selection import train_test_split


from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report
import numpy as np

# Step 1 — Data
X = [Link](1000, 10)
y = (X[:,0]+X[:,1] > 0).astype(int)

# Step 2 — Split
X_tr,X_te,y_tr,y_te = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)

# Step 3 — Scale (fit on train ONLY)


sc = StandardScaler()
X_tr = sc.fit_transform(X_tr)
X_te = [Link](X_te)

# Step 4 — Train
model = LogisticRegression(max_iter=1000)
[Link](X_tr, y_tr)

# Step 5 — Evaluate
y_pred = [Link](X_te)
print(f'Accuracy: {accuracy_score(y_te,y_pred):.4f}')
print(classification_report(y_te,y_pred))

22.2 Pipelines & Cross-Validation

© Python Data Science Mastery Guide 30


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

from [Link] import Pipeline


from sklearn.model_selection import cross_val_score, GridSearchCV
from [Link] import RandomForestClassifier

# Clean pipeline
pipe = Pipeline([
('scaler', StandardScaler()),
('clf', RandomForestClassifier(random_state=42))
])

# 5-fold cross-validation
scores = cross_val_score(pipe, X, y, cv=5, scoring='accuracy')
print(f'CV: {[Link]():.4f} ± {[Link]():.4f}')

# Grid search hyperparameter tuning


param_grid = {
'clf__n_estimators': [50, 100, 200],
'clf__max_depth': [None, 5, 10],
'clf__min_samples_split': [2, 5]
}
gs = GridSearchCV(pipe, param_grid, cv=5,
scoring='accuracy', n_jobs=-1)
[Link](X_tr, y_tr)
print(f'Best params: {gs.best_params_}')
print(f'Best score : {gs.best_score_:.4f}')

22.3 Model Evaluation Metrics

Python

from [Link] import (accuracy_score, precision_score,


recall_score, f1_score, roc_auc_score, confusion_matrix)

y_pred = [Link](X_te)
y_proba = model.predict_proba(X_te)[:,1]

print(f'Accuracy : {accuracy_score(y_te,y_pred):.4f}')
print(f'Precision: {precision_score(y_te,y_pred):.4f}')
print(f'Recall : {recall_score(y_te,y_pred):.4f}')
print(f'F1-Score : {f1_score(y_te,y_pred):.4f}')
print(f'ROC-AUC : {roc_auc_score(y_te,y_proba):.4f}')

# Confusion matrix
cm = confusion_matrix(y_te,y_pred)
print(cm)
# [[TN FP]
# [FN TP]]

© Python Data Science Mastery Guide 31


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 28

Neural Networks & Deep Learning

28.1 Neural Network Fundamentals

A neural network learns by adjusting weights through backpropagation. Input data flows forward
through layers; the error gradient flows backward, updating weights via gradient descent. Activation
functions introduce non-linearity, allowing the network to model complex patterns.

Activation Functions — Introducing Non-Linearity

ReLU Sigmoid Tanh


max(0,x) 1/(1+e^-x) (e^x-e^-x)/…

Python

import numpy as np

class NeuralNetwork:
def __init__(self, sizes):
self.W = [[Link](a,b)*0.01
for a,b in zip(sizes[:-1],sizes[1:])]
self.b = [[Link](b) for b in sizes[1:]]

def relu(self,x): return [Link](0,x)


def sigmoid(self,x): return 1/(1+[Link](-x))

def forward(self, X):


[Link] = [X]
for i,(W,b) in enumerate(zip(self.W,self.b)):
z = [Link][-1] @ W + b
fn = [Link] if i==len(self.W)-1 else [Link]
[Link](fn(z))
return [Link][-1]

nn = NeuralNetwork([784,256,128,10])
out = [Link]([Link](32,784))
print(f'Output: {[Link]}') # (32, 10)

28.2 TensorFlow & Keras

© Python Data Science Mastery Guide 32


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

import tensorflow as tf
from [Link] import layers

# Build model — Sequential API


model = [Link]([
[Link](shape=(784,)),
[Link](256, activation='relu'),
[Link](),
[Link](0.3),
[Link](128, activation='relu'),
[Link](0.2),
[Link](10, activation='softmax')
])

# Compile
[Link](
optimizer=[Link](1e-3),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

# Callbacks
cbs = [
[Link](patience=5, restore_best_weights=True),
[Link](factor=0.5, patience=3),
]

# Train
history = [Link](
X_train, y_train,
epochs=50, batch_size=64,
validation_split=0.2,
callbacks=cbs
)

28.3 Convolutional Neural Networks (CNNs)

© Python Data Science Mastery Guide 33


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

from [Link] import layers, Model

def build_cnn(input_shape=(32,32,3), n_classes=10):


inputs = [Link](shape=input_shape)

# Block 1
x = layers.Conv2D(32,3,padding='same',activation='relu')(inputs)
x = [Link]()(x)
x = layers.MaxPooling2D()(x)

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

# Block 3
x = layers.Conv2D(128,3,padding='same',activation='relu')(x)
x = layers.GlobalAveragePooling2D()(x)

# Classifier
x = [Link](256,activation='relu')(x)
x = [Link](0.5)(x)
outputs = [Link](n_classes,activation='softmax')(x)

return Model(inputs,outputs)

cnn = build_cnn()
[Link]()

© Python Data Science Mastery Guide 34


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 33

Natural Language Processing (NLP)

33.1 Text Preprocessing

Python

import re, string


from collections import Counter

def clean_text(text):
text = [Link]()
text = [Link](r'<.*?>','',text) # strip HTML
text = [Link](r'http\S+','',text) # strip URLs
text = [Link](r'[^a-z0-9 ]','',text) # keep alphanum
tokens = [Link]()
stops = {'the','a','an','is','it','in','to'}
return [t for t in tokens if t not in stops]

corpus = [
"Python is great for data science",
"Machine learning with scikit-learn",
]
[print(clean_text(t)) for t in corpus]

33.2 TF-IDF & Text Classification

© Python Data Science Mastery Guide 35


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Python

from sklearn.feature_extraction.text import TfidfVectorizer


from sklearn.naive_bayes import MultinomialNB
from [Link] import Pipeline
from [Link] import classification_report

# Pipeline: TF-IDF → Naive Bayes


text_clf = Pipeline([
('tfidf', TfidfVectorizer(max_features=5000, ngram_range=(1,2))),
('clf', MultinomialNB())
])
text_clf.fit(X_train_text, y_train)
y_pred = text_clf.predict(X_test_text)
print(classification_report(y_test, y_pred))

# Word2Vec with gensim


from [Link] import Word2Vec
sentences=[["machine","learning","python"],["deep","learning","nlp"]]
w2v = Word2Vec(sentences, vector_size=100, window=5, min_count=1)
vec = [Link]['python'] # 100-dim vector
sims = [Link].most_similar('python', topn=5)

33.3 Transformers & BERT

Python

from transformers import pipeline, AutoTokenizer, AutoModel


import torch

# Quick sentiment analysis


classifier = pipeline('sentiment-analysis')
result = classifier("Python makes data science easy!")
# [{'label': 'POSITIVE', 'score': 0.9998}]

# Named Entity Recognition


ner = pipeline('ner', grouped_entities=True)
entities = ner("Alice works at Google in NYC")

# BERT embeddings
model_name = 'bert-base-uncased'
tokenizer = AutoTokenizer.from_pretrained(model_name)
bert = AutoModel.from_pretrained(model_name)

text = 'Data science is the future'


inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
outputs = bert(**inputs)
embeddings = outputs.last_hidden_state[:,0,:] # [CLS] token
print(f'Embedding dim: {[Link]}') # [1, 768]

© Python Data Science Mastery Guide 36


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Chapter 39

Capstone Projects & Career Guide

39.1 Project 1 — Customer Churn Prediction

Python

import pandas as pd, numpy as np


from [Link] import Pipeline
from [Link] import ColumnTransformer
from [Link] import StandardScaler, OneHotEncoder
from [Link] import GradientBoostingClassifier
from [Link] import roc_auc_score

# Feature engineering
df["tenure_yr"] = df["tenure_months"]/12
df["chg_per_svc"] = df["monthly_charges"]/(df["num_services"]+1)

num_cols = ["tenure_yr","monthly_charges","chg_per_svc"]
cat_cols = ["contract","internet_service","payment_method"]

pre = ColumnTransformer([
("num", StandardScaler(), num_cols),
("cat", OneHotEncoder(drop="first"), cat_cols)
])

pipe = Pipeline([
("pre", pre),
("model", GradientBoostingClassifier(n_estimators=200))
])

[Link](X_tr, y_tr)
y_proba = pipe.predict_proba(X_te)[:,1]
print(f'ROC-AUC: {roc_auc_score(y_te, y_proba):.4f}')

39.2 Interview Preparation

Topic Key Concepts Common Questions

Statistics p-value, CLT, CI, power Explain bias-variance tradeoff

ML Theory Overfitting, regularization, CV What is gradient descent?

Pandas GroupBy, merge, pivot, time-series How do you handle missing data?

SQL Joins, subqueries, window functions Top-N per group query

© Python Data Science Mastery Guide 37


■ Python for Data Science — Complete Mastery Guide World-Class Study Material

Probability Bayes, distributions Monty Hall problem

System Design ML pipeline, A/B test, feature store Design a recommender system

TIP
Top study resources: Hands-On Machine Learning (Géron), [Link], Kaggle
competitions, StatQuest YouTube, and LeetCode SQL problems.

39.3 Data Science Career Paths

• Data Analyst — SQL + Pandas + dashboards (Power BI / Tableau)


• Data Scientist — Full ML lifecycle, statistics, Python
• ML Engineer — Production ML, MLOps, APIs, cloud deployment
• Research Scientist — Novel algorithms, deep learning, publications
• Data Engineer — Data pipelines, Spark, databases, ETL
• AI/ML Product Manager — Bridge between business and data teams

© Python Data Science Mastery Guide 38

You might also like