Python Modules for AI & Data Science Explained
Explore essential Python modules for AI, machine learning, and data science. Learn list comprehensions, module functionalities, and practical use cases.
Python Modules
This documentation outlines various essential Python modules, providing insights into their functionalities and common use cases.
5.1 Python List Comprehension
List comprehensions offer a concise way to create lists. They are often more readable and efficient than using traditional for
loops.
Syntax:
new_list = [expression for item in iterable if condition]
Explanation:
expression
: The value to be included in the new list.item
: The variable representing each element in theiterable
.iterable
: The sequence (e.g., list, tuple, string) to iterate over.condition
(optional): A filter that determines which elements are included.
Example:
# Create a list of squares for numbers from 0 to 9
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Create a list of even numbers from 0 to 19
even_numbers = [x for x in range(20) if x % 2 == 0]
print(even_numbers) # Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
5.2 Python Collections Module
The collections
module provides specialized container datatypes that offer alternatives to Python's built-in general-purpose containers like dict
, list
, set
, and tuple
.
Counter
A Counter
is a dict subclass for counting hashable objects.
Example:
from collections import Counter
my_string = "abracadabra"
char_counts = Counter(my_string)
print(char_counts)
# Output: Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1})
print(char_counts['a']) # Output: 5
defaultdict
A defaultdict
is a dictionary subclass that calls a factory function to supply missing values.
Example:
from collections import defaultdict
# Create a defaultdict where missing keys get an integer value of 0
int_dict = defaultdict(int)
int_dict['apple'] += 1
print(int_dict['apple']) # Output: 1
print(int_dict['banana']) # Output: 0 (key 'banana' was missing, int() was called)
# Create a defaultdict where missing keys get an empty list
list_dict = defaultdict(list)
list_dict['fruits'].append('apple')
print(list_dict['fruits']) # Output: ['apple']
print(list_dict['vegetables']) # Output: [] (key 'vegetables' was missing, list() was called)
OrderedDict
An OrderedDict
is a dictionary subclass that remembers the order that keys were first inserted.
Example:
from collections import OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
print(od) # Output: OrderedDict([('a', 1), ('b', 2), ('c', 3)])
deque
A deque
(double-ended queue) is a list-like container with fast appends and pops from either end.
Example:
from collections import deque
d = deque(['a', 'b', 'c'])
d.append('d') # Appends to the right
d.appendleft('z') # Appends to the left
print(d) # Output: deque(['z', 'a', 'b', 'c', 'd'])
d.pop() # Removes from the right
d.popleft() # Removes from the left
print(d) # Output: deque(['a', 'b', 'c'])
5.3 Python Math Module
The math
module provides access to mathematical functions for floating-point numbers.
Common Functions:
math.ceil(x)
: Returns the smallest integer greater than or equal tox
.math.floor(x)
: Returns the largest integer less than or equal tox
.math.sqrt(x)
: Returns the square root ofx
.math.pow(x, y)
: Returnsx
raised to the powery
.math.sin(x)
,math.cos(x)
,math.tan(x)
: Trigonometric functions (expects radians).math.radians(x)
: Converts anglex
from degrees to radians.math.degrees(x)
: Converts anglex
from radians to degrees.math.log(x[, base])
: Returns the logarithm ofx
to the givenbase
. If the base is not specified, it returns the natural logarithm.math.pi
: The mathematical constant pi.math.e
: The mathematical constant e.
Example:
import math
print(math.ceil(4.2)) # Output: 5
print(math.floor(4.8)) # Output: 4
print(math.sqrt(16)) # Output: 4.0
print(math.pow(2, 3)) # Output: 8.0
print(math.sin(math.radians(90))) # Output: 1.0
print(math.pi) # Output: 3.141592653589793
5.4 Python OS Module
The os
module provides a way of using operating system dependent functionality.
Key Functionalities:
-
File System Navigation and Manipulation:
os.getcwd()
: Get the current working directory.os.chdir(path)
: Change the current working directory.os.listdir(path='.')
: Return a list containing the names of the entries in the directory given bypath
.os.mkdir(path)
: Create a directory.os.makedirs(path)
: Create directories recursively.os.remove(path)
: Remove (delete) a file.os.rmdir(path)
: Remove a directory.os.rename(src, dst)
: Rename a file or directory.os.path.join(path1, path2, ...)
: Concatenate path components intelligently.os.path.exists(path)
: ReturnTrue
ifpath
refers to an existing path.os.path.isdir(path)
: ReturnTrue
ifpath
is an existing directory.os.path.isfile(path)
: ReturnTrue
ifpath
is an existing regular file.
-
Process Management:
os.system(command)
: Execute the command in a subshell.os.getpid()
: Return the current process ID.os.fork()
: Create a child process (Unix only).
-
Environment Variables:
os.environ
: A dictionary representing the environment variables.os.getenv(key, default=None)
: Get the value of an environment variable.
Example:
import os
# Current working directory
print(f"Current Directory: {os.getcwd()}")
# Create a new directory (if it doesn't exist)
if not os.path.exists("my_new_directory"):
os.mkdir("my_new_directory")
print("Created directory: my_new_directory")
# List directory contents
print(f"Contents of current directory: {os.listdir('.')}")
# Create a file path
file_path = os.path.join("my_new_directory", "my_file.txt")
print(f"File path: {file_path}")
# Check if a path is a file
print(f"Is '{file_path}' a file? {os.path.isfile(file_path)}")
# Get an environment variable
user_home = os.getenv('HOME') # Or 'USERPROFILE' on Windows
print(f"User's home directory: {user_home}")
5.5 Python Random Module
The random
module implements pseudo-random number generators for various distributions.
Common Functions:
random.random()
: Returns a random float in the range [0.0, 1.0).random.randint(a, b)
: Returns a random integerN
such thata <= N <= b
.random.randrange(start, stop[, step])
: Returns a randomly selected element fromrange(start, stop, step)
.random.choice(seq)
: Returns a random element from the non-empty sequenceseq
.random.choices(population, weights=None, *, cum_weights=None, k=1)
: Returns a list ofk
elements chosen from thepopulation
with replacement.random.shuffle(x[, random])
: Shuffles the sequencex
in place.random.sample(population, k)
: Returns ak
length list of unique elements chosen from thepopulation
sequence or set.
Example:
import random
print(f"Random float: {random.random()}")
print(f"Random integer between 1 and 10: {random.randint(1, 10)}")
print(f"Random choice from a list: {random.choice(['apple', 'banana', 'cherry'])}")
print(f"Shuffled list: {random.sample(['a', 'b', 'c', 'd', 'e'], 3)}") # Unique elements
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(f"Shuffled in place: {my_list}")
5.6 Python Statistics Module
The statistics
module provides functions for calculating mathematical statistics of numeric data.
Common Functions:
statistics.mean(data)
: Returns the arithmetic mean (average) of the data.statistics.median(data)
: Returns the median (middle value) of the data.statistics.mode(data)
: Returns the most common data point from discrete or nominal data.statistics.stdev(data)
: Returns the sample standard deviation of the data.statistics.variance(data)
: Returns the sample variance of the data.
Example:
import statistics
data_points = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10]
print(f"Mean: {statistics.mean(data_points)}")
print(f"Median: {statistics.median(data_points)}")
print(f"Mode: {statistics.mode(data_points)}") # Returns 5 as it appears most often
print(f"Standard Deviation: {statistics.stdev(data_points)}")
print(f"Variance: {statistics.variance(data_points)}")
5.7 Python Sys Module
The sys
module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Key Functionalities:
sys.argv
: The list of command-line arguments passed to a Python script.sys.argv[0]
is the script name itself.sys.path
: A list of strings that specifies the search path for modules.sys.exit([arg])
: Exit the interpreter. An optional argument can be provided to specify an exit status.sys.version
: A string containing the version number of the Python interpreter.sys.platform
: A string identifying the platform on which Python is running (e.g., 'linux', 'win32', 'darwin').sys.executable
: The absolute path of the executable binary for the Python interpreter.sys.stdin
,sys.stdout
,sys.stderr
: File objects corresponding to the interpreter's standard input, output, and error streams.
Example:
import sys
# Command-line arguments
print(f"Script name: {sys.argv[0]}")
if len(sys.argv) > 1:
print(f"Other arguments: {sys.argv[1:]}")
# Python version
print(f"Python Version: {sys.version}")
# Platform
print(f"Platform: {sys.platform}")
# Path where modules are searched
# print(f"Module Search Path: {sys.path}")
# Exit the script with a status code of 0 (success)
# sys.exit(0)
Python Lambda Functions: Concise, Anonymous Functions
Master Python Lambda functions for efficient, anonymous coding. Ideal for AI/ML tasks with map, filter, and sorted. Learn to write concise, single-purpose functions.
Python List Comprehension: Concise List Creation for AI
Master Python list comprehension for efficient data manipulation. Learn how this powerful feature simplifies list creation, ideal for AI and ML workflows.