0% found this document useful (0 votes)
8 views

Python Notes

Uploaded by

ssaurabh_ss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Notes

Uploaded by

ssaurabh_ss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Week 1: Python Programming Essen0als and Algorithmic Problem Solving

1. Introduc0on to Python Programming


• Objec0ve: Master the basics of Python programming with a focus on data science
applica<ons.
• Key Areas:
o Core data structures: lists, tuples, sets, and dic<onaries.
o Essen<al data science libraries: NumPy, SciPy, Matplotlib, and Pandas.
o Techniques for reading, wri<ng, and processing data in Python.
2. Algorithmic Problem Solving in Python
• Algorithm Defini0on: A series of clear, step-by-step instruc<ons to solve a problem,
with defined inputs and outputs.
• Benefits of Algorithms:
o Simplify program development.
o Enable itera<ve refinement and straighIorward code conversion.
o Facilitate tes<ng and op<miza<on of solu<ons.
• Proper0es of Effec0ve Algorithms:
o Efficiency: Minimizes resource usage and <me.
o Correctness: Produces accurate outputs for valid inputs.
o Clarity: Easy to understand and maintain.
o Scalability: Can handle larger data sizes without performance loss.
3. Problem-Solving Strategies
• Computa0onal Thinking: A systema<c, structured approach to solving problems.
o Steps:
§ Decomposi0on: Breaking down complex problems into smaller parts.
§ PaLern Recogni0on: Iden<fying similari<es to previously solved
problems.
§ Abstrac0on: Focusing only on relevant details, simplifying the
problem.
§ Algorithm Design: CraOing an efficient solu<on.
• Polya’s Four Steps for Problem Solving:
§ Understand the Problem: Iden<fy what is known and unknown.
§ Devise a Plan: Outline the steps needed to reach a solu<on.
§ Carry Out the Plan: Implement each step.
§ Review the Solu0on: Verify accuracy and efficiency.
4. Represen0ng Algorithms with Pseudocode and Flowcharts
• Pseudocode: Uses simple English-like statements to explain algorithms, avoiding
specific syntax.
• Flowcharts: Visual diagrams showing program flow.
o Symbols:
§ Rectangle: Represents process steps.
§ Diamond: Indicates decision points.
§ Parallelogram: Used for input/output.
§ Arrows: Show the direc<on of control flow.
o Example: Algorithm to check if a number is even or odd:
pseudo
BEGIN
INPUT number
IF (number % 2 == 0)
PRINT "Even"
ELSE
PRINT "Odd"
END
5. Building Blocks of Programs
• Data and Variables: Basic units for storing informa<on in a program.
• Control Structures: Manage program flow.
o Loops: Repeat ac<ons as specified (e.g., for and while loops).
o Condi0onals: Make decisions based on condi<ons (e.g., if, elif, else).

Summary of Key Python Func0ons and Libraries


• Basic Func0ons:
o len(): Returns the length of an object.
o upper(): Converts a string to uppercase.
o find(): Finds the posi<on of a substring within a string.
• Data Science Libraries:
o NumPy: For numerical opera<ons and array manipula<ons.
o SciPy: For scien<fic computa<ons.
o Matplotlib: For data visualiza<on and plobng.
o Pandas: For data analysis and manipula<on.
Key Concepts to Remember
• Computa0onal Thinking: Approach problems with a structured, logical method.
• Algorithmic Thinking: Break problems into solvable steps.
• Flowchart Design: Visualize logic before wri<ng code.
• Error Handling: Essen<al for crea<ng robust, error-tolerant programs.

Week 2: Python Programming Essen0als and Concepts

1. Introduc0on to Python
• Python: High-level, interpreted language created by Guido van Rossum in 1991,
designed for simplicity, readability, and versa<lity.
• Features:
o Easy to learn and read with simple syntax.
o Cross-plaIorm: Works on Windows, macOS, and Linux.
o Object-oriented: Supports modular and reusable code.
o Supports a large standard library for various tasks (databases, file handling,
networking).
o Popular in web development (Django, Flask), data science (NumPy, Pandas),
scrip<ng, and automa<on.
• Advantages:
o Efficient and produc<ve for developers.
o Dynamic typing: Types are assigned during execu<on.
o Free, open-source, and portable: Code can run across plaIorms without
modifica<on.
o Great community support and resources.
2. Python Installa0on
• Installa0on:
o Download from python.org or use a package manager.
o Python versions: Python 2.7 and Python 3.x (focus on Python 3.x for modern
applica<ons).
o IDEs: Op<ons include IDLE, Jupyter Notebook, and Spyder. Jupyter is highly
favored for data science because of its interac<ve interface and ability to
handle rich text and visualiza<ons.
3. Programming Basics in Python
• Iden0fiers:
o Names for variables, func<ons, classes, etc.
o Must begin with a leher (a-z, A-Z) or underscore, followed by lehers,
numbers, or underscores.
o Case-sensi<ve (e.g., Var and var are different).
• Variables:
o Used to store data.
o Assigned without specifying type (dynamic typing).
o Example:
x = 5 # integer
y = "Hi" # string
z = 3.5 # float
• Comments:
o Single-line: Use #.
o Mul<-line: Use triple quotes (""" comment """).
4. Data Types and Type Conversion
• Basic Data Types:
o int: Integer values.
o float: Decimal numbers.
o str: Text (enclosed in quotes).
o bool: True or False.
• Type Conversion:
o Convert between types using str(), int(), float(), etc.
o Example:
num_str = "10"
num_int = int(num_str) # converts string to integer
5. Operators and Expressions
• Operators:
o Arithme0c: +, -, *, /, // (floor division), % (modulus), ** (power).
o Comparison: ==, !=, >, <, >=, <=.
o Logical: and, or, not.
o Assignment: =, +=, -=, *=, /=, etc.
• Expressions:
o Combine values and operators to perform calcula<ons.
o Example:
a=5
b=2
result = (a + b) * (a - b)
6. Strings and String Opera0ons
• String Basics:
o Enclosed in single ('), double ("), or triple quotes (''', """) for mul<-line.
• String Opera0ons:
o Access characters by index: str[0].
o Slicing: str[start:end] to get a substring.
o Concatenate strings with + and repeat with *.
• String Func0ons:
o len(str): Returns length.
o str.lower(), str.upper(): Changes case.
o str.find("substring"): Finds posi<on of a substring.
o str.replace("old", "new"): Replaces substring.
7. Control Flow
• Condi0onals:
o if, elif, and else statements for decision-making.
o Example:
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
• Loops:
o for: Iterates over items in a sequence.
for i in range(5):
print(i) # prints numbers 0 to 4
o while: Repeats while a condi<on is true.
count = 0
while count < 5:
print(count)
count += 1
8. Python Func0ons
• Defining Func0ons:
o Use def keyword.
o Parameters are defined in parentheses.
o Example:
python
Copy code
def greet(name):
return "Hello, " + name
print(greet("Alice"))
• Built-in Func0ons:
o print(): Outputs data.
o input(): Takes input from the user.
o type(): Returns data type of an object.

Summary of Key Func0ons


• print(): Outputs to the console.
• len(): Returns length of a sequence.
• type(): Returns type of a variable.
• str.upper(), str.lower(): Change string case.
• int(), float(), str(): Convert data types.

Week 3: Python Operators and Control Flow

1. Operators in Python
• Arithme0c Operators:
o +: Addi<on
o -: Subtrac<on
o *: Mul<plica<on
o /: Division (returns a float)
o //: Floor Division (returns an integer)
o %: Modulus (returns remainder)
o **: Exponen<a<on
a=5
b=3
print(a + b) # 8
print(a ** b) # 125
• Comparison Operators:
o ==: Equal to
o !=: Not equal to
o >: Greater than
o <: Less than
o >=: Greater than or equal to
o <=: Less than or equal to
• Logical Operators:
o and: True if both operands are true.
o or: True if at least one operand is true.
o not: Inverts the truth value.
• Assignment Operators:
o =: Assign value.
o +=, -=, *=, /=: Compound assignment (e.g., x += 3 is equivalent to x = x + 3).
• Membership Operators:
o in: Checks if a value exists in a sequence.
o not in: Checks if a value does not exist in a sequence.
2. Control Flow Statements
• If Statements:
o Basic structure for decision-making.
o Syntax:
if condi<on:
# Code if condi<on is true
elif another_condi<on:
# Code if another_condi<on is true
else:
# Code if none of the condi<ons are true
o Example:
x = 20
if x < 15:
print("x is less than 15")
elif x == 20:
print("x is equal to 20")
else:
print("x is greater than 15 but not 20")
• While Loops:
o Executes a block of code as long as a condi<on is true.
o Syntax:
while condi<on:
# Code to execute
o Example:
i=1
while i <= 5:
print(i)
i += 1
• For Loops:
o Iterates over a sequence.
o Syntax:
for variable in sequence:
# Code to execute
o Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
3. Loop Control Statements
• Break Statement:
o Exits the loop immediately.
o Example:
for i in range(5):
if i == 3:
break
print(i)
# Output: 0, 1, 2
• Con0nue Statement:
o Skips the current itera<on and moves to the next.
o Example:
for i in range(5):
if i == 3:
con<nue
print(i)
# Output: 0, 1, 2, 4
• Else with Loops:
o else block executes when the loop completes normally, without encountering
a break.
o Example:
for i in range(5):
print(i)
else:
print("Loop completed")

Week 4: Collec0on Data Types in Python

1. Lists
• Defini0on: An ordered, mutable collec<on that allows duplicate elements.
• Syntax:
my_list = ["apple", "banana", "cherry"]
• Key Proper0es:
o Indexed: Access elements with my_list[index].
o Supports duplicates: Same elements can appear mul<ple <mes.
o Common Methods:
§ append(item): Adds item to the end.
§ insert(index, item): Inserts item at index.
§ remove(item): Removes the first occurrence of item.
§ pop(index): Removes item at index and returns it.
§ clear(): Removes all elements.
§ sort(): Sorts the list.
§ reverse(): Reverses the list order.
o Example:
my_list = [1, 3, 5]
my_list.append(7) # [1, 3, 5, 7]
my_list.sort() # [1, 3, 5, 7]
2. Tuples
• Defini0on: An ordered, immutable collec<on.
• Syntax:
my_tuple = ("apple", "banana", "cherry")
• Key Proper0es:
o Immutable: Cannot modify elements once created.
o Indexed: Access elements with my_tuple[index].
o Supports duplicates.
o Example:
my_tuple = (1, 2, 3)
print(my_tuple[0]) # 1
3. Sets
• Defini0on: An unordered collec<on of unique elements.
• Syntax:
my_set = {"apple", "banana", "cherry"}
• Key Proper0es:
o Unordered: No indexing.
o Unique items only: Duplicates are automa<cally removed.
o Common Methods:
§ add(item): Adds item.
§ remove(item): Removes item, raises error if not found.
§ discard(item): Removes item without error if not found.
§ pop(): Removes and returns an arbitrary element.
§ clear(): Removes all elements.
§ Set Opera0ons:
§ union(): Combines elements from both sets.
§ intersec<on(): Elements common to both sets.
§ difference(): Elements in one set but not the other.
o Example:
my_set = {1, 2, 3}
my_set.add(4) # {1, 2, 3, 4}
4. Dic0onaries
• Defini0on: An ordered collec<on of key-value pairs (as of Python 3.7).
• Syntax:
my_dict = {"brand": "Ford", "model": "Mustang", "year": 1964}
• Key Proper0es:
o Keys are unique and immutable; values can be of any type.
o Access elements by keys (my_dict["brand"]).
o Common Methods:
§ get(key): Returns the value for key.
§ update(): Updates with key-value pairs from another dic<onary.
§ pop(key): Removes key and returns its value.
§ clear(): Removes all items.
o Example:
car = {"brand": "Ford", "model": "Mustang", "year": 1964}
car["year"] = 2020 # Update value

5. List Comprehension
• Defini0on: A concise way to create lists using an expression.
• Syntax:
squares = [x**2 for x in range(10)]
• Example with Condi0on:
evens = [x for x in range(10) if x % 2 == 0]
Summary of Key Points
• Lists: Mutable, ordered, allows duplicates.
• Tuples: Immutable, ordered, allows duplicates.
• Sets: Mutable, unordered, unique items.
• Dic0onaries: Mutable, ordered, key-value pairs.
• List Comprehension: Efficient way to generate lists with condi<ons.

Week 5: Func0ons in Python

1. Defini0on and Basics


• Func0on: A block of reusable code that performs a specific task. Func<ons help
simplify code by dividing it into manageable parts.
• Syntax:
def func<on_name(parameters):
# code block
return value
2. Types of Func0ons
• Built-in Func0ons: Predefined func<ons, e.g., print(), len(), max().
• User-defined Func0ons: Created by the user to perform specific tasks.
3. Func0on Parameters and Arguments
• Parameters: Variables defined in the func<on declara<on.
• Arguments: Values passed when calling the func<on.
• Types of Arguments:
o Posi0onal Arguments: Passed in order.
o Keyword Arguments: Defined with key=value, allowing flexible ordering.
o Default Arguments: Parameters with default values.
o Arbitrary Arguments: Allows a variable number of arguments using *args (for
posi<onal) and **kwargs (for keyword).
4. Return Statement
• A func<on can return a value using return. If omihed, it returns None.
• Example:
def add(a, b):
return a + b
5. Variable Scope
• Local Variables: Defined within func<ons and accessible only inside them.
• Global Variables: Declared outside func<ons, accessible throughout the program.
• Using global: Allows modifica<on of a global variable inside a func<on.
6. Lambda Func0ons
• Defini0on: Anonymous, one-line func<ons using lambda.
• Syntax:
lambda arguments: expression
• Example:
square = lambda x: x * x
print(square(5)) # Output: 25
7. Map, Filter, and Reduce Func0ons
• Map: Applies a func<on to each item in an iterable.
numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, numbers)) # [1, 4, 9, 16]
• Filter: Filters items in an iterable based on a condi<on.
numbers = [1, 2, 3, 4]
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]
• Reduce (from functools): Reduces an iterable to a single value by applying a func<on.
from functools import reduce
sum_total = reduce(lambda x, y: x + y, numbers) # 10
8. Recursive Func0ons
• Defini0on: Func<ons that call themselves to solve smaller instances of a problem.
• Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)

Summary of Key Points


• Func<ons modularize code, increasing readability and reusability.
• Parameters: Posi<onal, keyword, default, and arbitrary types allow flexibility.
• Lambda func0ons and map/filter/reduce are useful for concise, func<onal
programming tasks.
• Recursion is helpful for problems that involve repe<<ve, smaller sub-tasks.

Week 6: Higher-Order Func0ons, Decorators, and Generators in Python

1. Higher-Order Func0ons
• Defini0on: Func<ons that accept other func<ons as arguments or return func<ons as
results.
• Key Proper0es:
o Can store a func<on in a variable.
o Can pass a func<on as an argument to another func<on.
o Can return a func<on from another func<on.
o Commonly used in decorators.
• Examples:
o Passing Func0ons as Arguments:
def shout(text):
return text.upper()

def greet(func):
message = func("Hello!")
print(message)

greet(shout) # Output: "HELLO!"


o Returning a Func0on:
python
Copy code
def create_mul<plier(n):
def mul<plier(x):
return x * n
return mul<plier

<mes3 = create_mul<plier(3)
print(<mes3(5)) # Output: 15
2. Decorators
• Defini0on: A decorator is a func<on that wraps another func<on to extend or alter
its behavior without modifying the original func<on's code.
• Syntax: The @decorator_name syntax is used above the func<on defini<on.
• Example:
def my_decorator(func):
def wrapper():
print("Something is happening before the func<on is called.")
func()
print("Something is happening aOer the func<on is called.")
return wrapper

@my_decorator
def say_hello():
print("Hello!")

say_hello()
# Output:
# "Something is happening before the func<on is called."
# "Hello!"
# "Something is happening aOer the func<on is called."
• Common Use Case:
o Error Checking:
def safe_divide(func):
def wrapper(a, b):
if b == 0:
print("Cannot divide by zero!")
return
return func(a, b)
return wrapper

@safe_divide
def divide(a, b):
return a / b

print(divide(10, 2)) # Output: 5.0


print(divide(10, 0)) # Output: "Cannot divide by zero!"
3. Generator Func0ons
• Defini0on: A func<on that returns an iterator, which yields values one at a <me using
the yield statement instead of return.
• Behavior: When called, the generator does not start execu<on immediately. Each call
to next() resumes where the func<on leO off aOer yield.
• Example:
def count_up_to(n):
count = 1
while count <= n:
yield count
count += 1

counter = count_up_to(5)
for number in counter:
print(number)
# Output: 1, 2, 3, 4, 5
• Use Cases:
o Infinite sequences: Generators can handle large or infinite sequences without
running out of memory.
o On-demand computa0on: Values are only computed as needed, improving
efficiency.
• Generator Expressions:
o Similar to list comprehensions but use () instead of [], yielding one item at a
<me.
squares = (x * x for x in range(5))
for square in squares:
print(square)
# Output: 0, 1, 4, 9, 16

Summary of Key Concepts


• Higher-Order Func0ons: Useful for crea<ng flexible, reusable code.
• Decorators: Extend func<onality of func<ons without modifying them directly.
• Generators: Efficient for handling sequences, especially useful for memory-intensive
tasks.

Week 7: Python Modules and NumPy for Data Science

1. Python Modules Overview


• Defini0on: A module is a Python file (.py) containing defini<ons, func<ons, classes,
and variables that can be reused in other Python scripts.
• Purpose: Modules organize code, making it easier to understand, use, and maintain.
• Crea0ng a Module:
o Write func<ons and save them in a file (e.g., mycalc.py).
o Example:
# mycalc.py
def add(a, b):
return a + b

def subtract(a, b):


return a - b
o To use, import it with import mycalc, then call func<ons as mycalc.add(2, 3).
• Impor0ng Modules:
o Basic import: import module_name.
o Alias import: import module_name as alias (e.g., import mycalc as mc).
o Selec<ve import: from module_name import func<on (e.g., from mycalc
import add).
o Wildcard import: from module_name import * imports all func<ons without
module prefix.
2. NumPy Basics
• NumPy: A powerful library for numerical computa<ons in Python, par<cularly with
arrays.
• Key Features:
o Mul<-dimensional array object (ndarray).
o Tools for mathema<cal opera<ons, linear algebra, and random number
genera<on.
3. Crea0ng Arrays in NumPy
• Array Crea0on:
o One-dimensional: np.array([1, 2, 3])
o Mul<-dimensional: np.array([[1, 2], [3, 4]])
• Special Arrays:
o Zeros: np.zeros((2, 3)) creates a 2x3 array of zeros.
o Ones: np.ones((3, 3)) creates a 3x3 array of ones.
o Iden<ty: np.eye(3) creates a 3x3 iden<ty matrix.
4. Array Opera0ons
• Element-wise Opera0ons:
o Addi<on: array1 + array2
o Mul<plica<on: array1 * array2
o Division: array1 / array2
• Matrix Mul0plica0on:
o Use @ or np.dot(array1, array2) for matrix mul<plica<on.
• Broadcas0ng: Enables opera<ons on arrays of different shapes, e.g., adding a scalar
to an array.
5. Reshaping and Slicing Arrays
• Reshape: Change the shape of an array without altering data.
arr = np.array([1, 2, 3, 4, 5, 6])
arr_reshaped = arr.reshape(2, 3)
• Slicing:
o Syntax: array[start:stop:step].
o Example: arr[0:3] selects elements from index 0 to 2.
o Nega<ve indexing and slicing are supported.
6. NumPy Func0ons
• Mathema0cal Func0ons:
o Sum: np.sum(array)
o Mean: np.mean(array)
o Standard Devia<on: np.std(array)
• Trigonometric Func0ons: np.sin(array), np.cos(array), etc.
• Exponen0al and Logarithmic Func0ons: np.exp(array), np.log(array)

Week 8: Scien0fic Compu0ng with Python - SciPy


Overview
SciPy is a Python library built on top of NumPy, providing algorithms and func0ons for
scien0fic and technical compu0ng. Key submodules and features include:
• Linear Algebra (scipy.linalg): Provides rou0nes for matrix opera0ons, solving
systems of equa0ons, and compu0ng eigenvalues.
Example:
import numpy as np
from scipy import linalg

A = np.array([[2, 1], [1, 3]])


b = np.array([5, 8])
x = linalg.solve(A, b)
print("Solu0on:", x)
• Op0miza0on (scipy.op0mize): For func0on minimiza0on/maximiza0on and
parameter es0ma0on.
Example:
from scipy.op0mize import minimize

def objec0ve(x):
return x**2 + 4*np.sin(5*x)

result = minimize(objec0ve, 0)
print("Minimizer:", result.x)
• Sta0s0cs (scipy.stats): Offers func0ons for sta0s0cal analysis, distribu0ons,
hypothesis tes0ng, and probability calcula0ons.
Example:
from scipy.stats import norm

print("P(X < 1.96):", norm.cdf(1.96))


• Integra0on (scipy.integrate): Provides single, double, and mul0ple integra0on
func0ons.
Example:
from scipy.integrate import quad

result, error = quad(lambda x: x**2, 0, 1)


print("Integral:", result, "Error:", error)
• Interpola0on (scipy.interpolate): Useful for es0ma0ng values between data points.
Example:
from scipy.interpolate import interp1d
import numpy as np

xs = np.array([0, 1, 2, 3])
ys = np.array([0, 1, 4, 9])
interpolator = interp1d(xs, ys, kind='linear')
print("Interpolated value at x=1.5:", interpolator(1.5))
• Special Func0ons (scipy.special): Contains mathema0cal func0ons like Bessel and
gamma func0ons.

Data Visualiza0on in Python - Matplotlib


Basic Plorng
Matplotlib is widely used for crea0ng sta0c, animated, and interac0ve visualiza0ons in
Python.
1. Line Plot:
import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [2, 4, 1]
plt.plot(x, y)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.0tle("Simple Line Plot")
plt.show()
2. Bar Plot:
import matplotlib.pyplot as plt

categories = ["A", "B", "C"]


values = [4, 7, 1]
plt.bar(categories, values)
plt.0tle("Bar Plot Example")
plt.show()
3. ScaLer Plot:
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]
plt.scaLer(x, y)
plt.0tle("ScaLer Plot Example")
plt.show()
Advanced Plorng Techniques
• Subplots and Grids:
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3, 4], [10, 20, 25, 30])
axs[1, 1].plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()
• Customizing Plots:
o linewidth for line thickness, color for color, and marker for point markers.
o Logarithmic and semi-log plots can be created using set_xscale and
set_yscale with arguments "log" or "symlog".
Example: Logarithmic Plot
import numpy as np
import matplotlib.pyplot as plt

x = np.logspace(0.1, 2, 100)
y = np.log(x)

plt.plot(x, y)
plt.xscale("log")
plt.0tle("Logarithmic Scale Example")
plt.show()

Interpola0on and Curve Firng


Using SciPy’s curve_fit from scipy.op0mize for data firng.
Example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.op0mize import curve_fit

def func(x, a, b):


return a * np.sin(b * x)

x_data = np.linspace(0, 2 * np.pi, 100)


y_data = func(x_data, 2.5, 1.3) + 0.5 * np.random.normal(size=len(x_data))

params, covariance = curve_fit(func, x_data, y_data)


a, b = params

plt.scaLer(x_data, y_data, label="Data")


plt.plot(x_data, func(x_data, a, b), label="FiLed Curve", color="red")
plt.legend()
plt.show()

Quick Reference Summary


• SciPy Key Func0ons:
o scipy.linalg.solve: Solves linear equa0ons.
o scipy.integrate.quad: Single integra0on.
o scipy.interpolate.interp1d: 1D interpola0on.
o scipy.stats.norm: Sta0s0cal func0ons for normal distribu0on.
• Matplotlib Essen0als:
o plt.plot, plt.scaLer, plt.bar: Basic plots.
o Customiza0on: color, linewidth, xlabel, ylabel, 0tle.
o Subplots: plt.subplots() for mul0ple panels.

Week 9: SciPy for Op0miza0on, Linear Algebra, and Sta0s0cs


1. Op0miza0on of Data
Op<miza<on in SciPy focuses on minimizing or maximizing func<ons with specific
constraints, essen<al in data science.
• Objec0ve Func0on (f(x)): The target func<on to be minimized or maximized.
• Decision Variables (x): Variables adjusted to find the func<on's op<mal value.
• Constraints (a ≤ x ≤ b): Limits on decision variables.
Example:
from scipy.op<mize import minimize

# Define objec<ve func<on


def objec<ve(x):
return x**2 + 4 * np.sin(5 * x)

result = minimize(objec<ve, 0)
print("Minimized value at x =", result.x)
SciPy's op<mize module offers methods for both constrained and unconstrained problems:
• Unconstrained Op0miza0on: No constraints on decision variables.
• Constrained Op0miza0on: Variables must sa<sfy specified limits.
Types of Op0miza0on Problems:
• Linear Programming: Linear objec<ve func<on and constraints.
• Nonlinear Programming: Nonlinear func<on or constraints.
• Integer and Mixed Integer Programming: Decision variables are integers or mixed
(con<nuous and discrete).

2. Linear Algebra with scipy.linalg


SciPy’s linalg module provides efficient implementa<ons for essen<al linear algebra tasks:
• Solving Systems of Equa0ons:
from scipy.linalg import solve
import numpy as np

A = np.array([[3, 2], [1, 2]])


b = np.array([5, 5])
x = solve(A, b)
print("Solu<on:", x)
• Matrix Decomposi0on:
o Determinant: scipy.linalg.det(matrix)
o Inverse: scipy.linalg.inv(matrix)
o Eigenvalues and Eigenvectors:
from scipy.linalg import eig
matrix = np.array([[1, 2], [3, 4]])
eig_vals, eig_vecs = eig(matrix)
• Singular Value Decomposi0on (SVD):
from scipy.linalg import svd
U, s, Vh = svd(matrix)

3. Sta0s0cs with scipy.stats


SciPy's stats module is packed with func<ons for sta<s<cal tests and probabilis<c
descrip<ons:
• Descrip0ve Sta0s0cs:
o Mean: np.mean(data)
o Median: np.median(data)
o Standard Devia0on: np.std(data)
o Variance: np.var(data)
o Correla0on: np.corrcoef(x, y)
• Random Distribu0ons:
o Normal Distribu0on:
from scipy.stats import norm
data = norm.rvs(size=100, loc=0, scale=1) # Generate random normal values
• Sta0s0cal Tests:
o T-test for independent samples:
from scipy.stats import hest_ind
a = np.random.normal(0, 1, size=100)
b = np.random.normal(1, 1, size=100)
t_stat, p_value = hest_ind(a, b)
o Correla0on Tests: pearsonr, spearmanr, kendalltau
Matplotlib for Data Visualiza0on
1. Basic Plorng
Matplotlib’s pyplot module provides powerful func<ons for crea<ng visualiza<ons:
• Line Plot:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.<tle("Line Plot")
plt.show()
• ScaLer Plot:
plt.scaher(x, y)
plt.show()
• Histogram:
python
Copy code
plt.hist(data, bins=10)
plt.show()
2. Advanced Plorng Techniques
• Subplots and Figure Customiza0on:
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[1, 1].scaher(x, y)
plt.show()
• Logarithmic Scale:
python
Copy code
plt.plot(x, y)
plt.xscale("log")
plt.show()
3. Curve Firng Example
Using SciPy’s curve_fit for data fibng:
from scipy.op<mize import curve_fit

def model(x, a, b):


return a * np.sin(b * x)

x_data = np.linspace(0, 10, 100)


y_data = model(x_data, 2, 1.5) + 0.5 * np.random.normal(size=len(x_data))

params, _ = curve_fit(model, x_data, y_data)


plt.scaher(x_data, y_data)
plt.plot(x_data, model(x_data, *params), color="red")
plt.show()
Quick Reference Summary
• SciPy Op0mize:
o minimize, curve_fit, minimize_scalar: For op<miza<on tasks.
• SciPy Linalg:
o solve, det, eig, svd: Linear algebra func<ons.
• SciPy Stats:
o norm, uniform, hest_ind, pearsonr: Random distribu<ons and sta<s<cal tests.
• Matplotlib Pyplot:
o plot, scaher, hist, subplots, xscale("log"): Basic to advanced plobng func<ons.

Week 10:

1. Pandas Series
A Series is a one-dimensional labeled array capable of holding any data type. Think of it as a
single column in a table. Useful for storing sequences of data, like <me series or one-
dimensional numerical/character data.
Crea0ng a Series
• Empty Series:
import pandas as pd
series = pd.Series()
print(series)
• Series from List:
data = [10, 20, 30]
series = pd.Series(data)
print(series)
• Series from Dic0onary:
data = {'a': 1, 'b': 2, 'c': 3}
series = pd.Series(data)
• Series with Custom Index:
data = [1, 2, 3]
series = pd.Series(data, index=['x', 'y', 'z'])
Accessing Series Data
• Indexing and Labeling: Access by integer index (series[0]) or label (series['x']).
• ALributes: series.index for index, series.values for data values.

2. Pandas DataFrame
A DataFrame is a two-dimensional, size-mutable, and labeled data structure, ideal for
represen<ng tabular data like rows and columns. Each column can hold different data types,
allowing for heterogeneous data.
Crea0ng DataFrames
• From List of Lists:
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B'])
• From Dic0onary of Lists:
data = {'Name': ['Tom', 'Jerry'], 'Age': [5, 7]}
df = pd.DataFrame(data)
• From Dic0onary of Series:
data = {'Name': pd.Series(['Tom', 'Jerry']), 'Age': pd.Series([5, 7])}
df = pd.DataFrame(data)
Accessing and Manipula0ng Data
• Selec0ng Columns:
df['Name'] # Single column
df[['Name', 'Age']] # Mul<ple columns
• Selec0ng Rows:
o By Index: df.iloc[0] for the first row.
o By Label: df.loc[0] for label-based indexing.
• Adding Columns:
df['Gender'] = ['M', 'F']
• Dropping Columns:
df.drop('Gender', axis=1, inplace=True)

3. Reindexing in Pandas
Reindexing aligns the DataFrame to a new index. This is essen<al for modifying DataFrame
rows/columns and handling missing data.
• Reindexing Rows:
new_index = ['row1', 'row2']
df_reindexed = df.reindex(new_index)
• Reindexing Columns:
df_reindexed = df.reindex(columns=['A', 'B', 'C'])
• Filling Missing Data During Reindexing:
df_reindexed = df.reindex(new_index, fill_value=0)

4. Func0on Applica0on with Pandas


Pandas offers func<ons to apply custom transforma<ons across rows, columns, or en<re
DataFrames.
Key Methods
• Table-wise with pipe: Applies func<ons on the DataFrame as a whole.
df.pipe(lambda x: x + 2)
• Row/Column-wise with apply: Applies a func<on along a specific axis.
df.apply(np.sum, axis=0) # Column-wise sum
df.apply(np.sum, axis=1) # Row-wise sum
• Element-wise with applymap: Applies a func<on to each element.
df.applymap(lambda x: x * 2)

Quick Reference Summary


• Pandas Series:
o Create with pd.Series(data, index).
o Indexing: .iloc[] for posi<on-based, .loc[] for label-based.
• Pandas DataFrame:
o Create from lists, dic<onaries, or series.
o Access: .iloc[] for rows, ['col_name'] for columns.
o Manipulate: .reindex(), .drop(), and applymap().
• Reindexing:
o Adjust row/column labels with .reindex().
o Fill missing data with fill_value parameter.
• Func0on Applica0ons:
o Use .pipe() for table-wise, .apply() for row/column-wise, and .applymap() for
element-wise transforma<ons.

You might also like