Python is the most popular language for ML due to its rich ecosystem of libraries. This chapter covers the essential libraries every ML practitioner should know: NumPy, Pandas, Scikit-learn, TensorFlow, PyTorch, and Jupyter.
NumPy provides support for large, multi-dimensional arrays and matrices. It offers a wide collection of mathematical functions to operate on these arrays efficiently. Almost every ML library builds on NumPy.
import numpy as np
# Create arrays
arr = np.array([1, 2, 3, 4, 5])
zeros = np.zeros((3, 3))
ones = np.ones((2, 4))
# Operations
print(arr.mean(), arr.std(), arr.sum())
# Broadcasting
result = arr + 10
print(result)
NumPy supports element-wise arithmetic, matrix multiplication, slicing, reshaping, and broadcasting. These operations are vectorized and run at C speed.
Pandas is the data manipulation library of choice. Its DataFrame and Series data structures make it easy to load, clean, transform, and analyze tabular data.
import pandas as pd
# Load CSV
df = pd.read_csv('data.csv')
# Explore
print(df.head())
print(df.describe())
print(df.isnull().sum())
# Filter and group
filtered = df[df['age'] > 30]
grouped = df.groupby('category').mean()
Scikit-learn provides consistent APIs for preprocessing, model training, evaluation, and selection. It includes estimators (models), transformers (preprocessing), and pipelines that chain them together.
Estimators implement .fit() and .predict() methods. Transformers implement .fit() and .transform(). Pipelines combine transformers and estimators for clean workflow management.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', RandomForestClassifier())
])
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
TensorFlow is Google's open-source deep learning framework. It provides Keras as a high-level API for building neural networks quickly. TensorFlow also supports distributed training and deployment to mobile and web.
PyTorch, developed by Meta, is known for its dynamic computation graph and ease of debugging. It has become the preferred framework for research and is widely used in academia.
Jupyter provides an interactive environment for exploratory analysis, visualization, and documentation. Notebooks combine code, output, and markdown explanations. JupyterLab is the next-generation interface with a more powerful layout.
Use pip or conda to install ML libraries. It is recommended to use virtual environments to isolate project dependencies.
# Create and activate a virtual environment
python -m venv ml_env
.\ml_env\Scripts\Activate.ps1
# Install essential libraries
pip install numpy pandas scikit-learn matplotlib seaborn
pip install jupyter tensorflow torch
requirements.txt to document your project's dependencies so others can reproduce your environment.
Mastering these tools is essential for productive ML work. Practice using NumPy and Pandas for data manipulation, Scikit-learn for traditional ML, and TensorFlow or PyTorch for deep learning.