Essential Python programming concepts for machine learning including NumPy, Pandas, data structures, functions, and OOP with practical ML-focused examples.
Python is the undisputed language of machine learning. Its simple syntax, vast ecosystem of libraries, and strong community make it the perfect choice for data scientists and ML engineers. This guide covers the essential Python skills you need before diving into ML algorithms.
Why Python for ML?
| Feature | Benefit for ML |
|---|
| Simple syntax | Focus on algorithms, not language complexity |
| NumPy/Pandas | Efficient numerical computing and data manipulation |
| Scikit-learn | Comprehensive ML library with consistent API |
| TensorFlow/PyTorch | Deep learning frameworks |
| Matplotlib/Seaborn | Visualization for data exploration |
| Jupyter Notebooks | Interactive development and documentation |
Essential Data Structures
import numpy as np
# Lists vs NumPy arrays (ML always uses arrays)
python_list = [1, 2, 3, 4, 5]
numpy_array = np.array([1, 2, 3, 4, 5])
# NumPy is orders of magnitude faster for math
import time
size = 1_000_000
list_a = list(range(size))
arr_a = np.arange(size)
# List operation
start = time.time()
result_list = [x * 2 for x in list_a]
list_time = time.time() - start
# NumPy operation (vectorized)
start = time.time()
result_arr = arr_a * 2
numpy_time = time.time() - start
print(f"List time: {list_time:.4f}s")
print(f"NumPy time: {numpy_time:.4f}s")
print(f"NumPy is {list_time/numpy_time:.0f}x faster!")
NumPy for ML
import numpy as np
# Creating arrays (datasets in ML are arrays)
X = np.random.randn(100, 5) # 100 samples, 5 features
y = np.random.randint(0, 2, 100) # Binary labels
print(f"Feature matrix shape: {X.shape}")
print(f"Labels shape: {y.shape}")
# Common operations
print(f"Mean of each feature: {X.mean(axis=0).round(3)}")
print(f"Std of each feature: {X.std(axis=0).round(3)}")
# Matrix operations (foundation of ML math)
W = np.random.randn(5, 3) # Weight matrix
output = X @ W # Matrix multiplication (@ operator)
print(f"Linear transformation: {X.shape} @ {W.shape} = {output.shape}")
# Broadcasting (automatic shape matching)
# Normalize features: (X - mean) / std
X_normalized = (X - X.mean(axis=0)) / X.std(axis=0)
print(f"Normalized mean: {X_normalized.mean(axis=0).round(10)}")
Pandas for Data Handling
import pandas as pd
import numpy as np
# Creating DataFrames (how you load and manipulate datasets)
df = pd.DataFrame({
'age': [25, 30, 35, 40, 45, 50],
'salary': [30000, 45000, 60000, 80000, 90000, 120000],
'experience': [1, 3, 5, 8, 12, 20],
'department': ['IT', 'HR', 'IT', 'Finance', 'IT', 'Finance'],
'promoted': [0, 0, 1, 1, 1, 1]
})
# Essential operations for ML preprocessing
print("Dataset Overview:")
print(df.describe())
print(f"\nMissing values:\n{df.isnull().sum()}")
# Filtering and selection
it_dept = df[df['department'] == 'IT']
high_salary = df[df['salary'] > 50000]
# Feature engineering
df['salary_per_year_exp'] = df['salary'] / df['experience']
# Encoding categorical variables
df_encoded = pd.get_dummies(df, columns=['department'])
print(f"\nAfter encoding:\n{df_encoded.head()}")
Functions and Pipelines
def ml_preprocessing_pipeline(df, target_col, test_size=0.2):
"""Reusable ML preprocessing pipeline"""
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Separate features and target
X = df.drop(target_col, axis=1)
y = df[target_col]
# Handle categorical columns
cat_cols = X.select_dtypes(include=['object']).columns
X = pd.get_dummies(X, columns=cat_cols)
# Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42
)
# Scale numeric features
scaler = StandardScaler()
X_train_scaled = pd.DataFrame(
scaler.fit_transform(X_train),
columns=X_train.columns
)
X_test_scaled = pd.DataFrame(
scaler.transform(X_test),
columns=X_test.columns
)
return X_train_scaled, X_test_scaled, y_train, y_test, scaler
# Usage
# X_train, X_test, y_train, y_test, scaler = ml_preprocessing_pipeline(df, 'promoted')
Object-Oriented ML
class SimpleLinearRegression:
"""Custom implementation showing OOP in ML"""
def __init__(self, learning_rate=0.01, n_iterations=1000):
self.lr = learning_rate
self.n_iter = n_iterations
self.weights = None
self.bias = None
self.losses = []
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for i in range(self.n_iter):
y_pred = X @ self.weights + self.bias
# Gradients
dw = (1/n_samples) * X.T @ (y_pred - y)
db = (1/n_samples) * np.sum(y_pred - y)
# Update
self.weights -= self.lr * dw
self.bias -= self.lr * db
# Track loss
loss = np.mean((y_pred - y) ** 2)
self.losses.append(loss)
return self
def predict(self, X):
return X @ self.weights + self.bias
def score(self, X, y):
y_pred = self.predict(X)
ss_res = np.sum((y - y_pred) ** 2)
ss_tot = np.sum((y - y.mean()) ** 2)
return 1 - (ss_res / ss_tot)
# Test it
X = np.random.randn(100, 3)
y = 2*X[:, 0] + 3*X[:, 1] - X[:, 2] + np.random.randn(100) * 0.1
model = SimpleLinearRegression(learning_rate=0.1)
model.fit(X, y)
print(f"R² Score: {model.score(X, y):.4f}")
print(f"Learned weights: {model.weights.round(3)}")
print(f"True weights: [2.0, 3.0, -1.0]")
Interview Questions
- Why is Python preferred over R or Java for ML?
Python offers the best balance of ease of use, library ecosystem (NumPy, sklearn, TensorFlow), production readiness, and community support. R is strong for statistics but weaker for deployment.
- What is the difference between a Python list and a NumPy array?
NumPy arrays are typed, contiguous in memory, support vectorized operations, and are orders of magnitude faster for math. Lists are flexible but slow for numerical computation.
- What is broadcasting in NumPy?
Broadcasting automatically expands smaller arrays to match larger ones during operations, avoiding explicit loops. Example: subtracting a mean vector from a matrix.
- How would you handle a 10GB dataset that doesn't fit in memory?
Use chunked reading (pd.read_csv with chunksize), Dask for parallel computing, or database queries. For ML, use mini-batch algorithms or sampling.
- What is the purpose of __init__, fit, and predict in sklearn-style classes?
__init__ sets hyperparameters, fit learns from data (stores parameters), predict uses learned parameters on new data. This consistent API is why sklearn is so popular.