Skip to content

Python Packages for AI and Machine Learning

  • The major Python AI/ML package categories and what problem each solves.
  • When to use NumPy, pandas, scikit-learn, PyTorch, and hosted model SDKs.
  • Minimal installation sets for common goals: data analysis, classical ML, LLM apps, and RAG pipelines.
  • How to choose packages without over-engineering the stack.

Python dominates AI and ML workflows largely because of its package ecosystem. The ecosystem spans numerical computing, data manipulation, classical machine learning, deep learning, hosted model APIs, and orchestration frameworks. Knowing which layer you need prevents the common mistake of pulling in a heavy orchestration framework when a direct SDK call is sufficient.

Layer Purpose Key packages
Numerical computing Arrays, vectorized math, linear algebra NumPy, SciPy
Data analysis Tables, joins, cleaning, aggregation pandas, Polars
Classical ML Structured data models, evaluation, pipelines scikit-learn, XGBoost, LightGBM
Deep learning Neural networks, GPU training, inference PyTorch, TensorFlow/Keras
Hosted model SDKs Calling LLMs via API openai, google-genai, anthropic
Orchestration Chains, agents, multi-step pipelines LangChain, LlamaIndex
Vector storage Embedding search and RAG Chroma, FAISS, pgvector, Weaviate
Evaluation Testing and benchmarking LLM outputs DeepEval, Ragas, Promptfoo

NumPy provides the ndarray — a fast, typed, multi-dimensional array — and the functions to operate on it. Almost every other scientific Python package (pandas, scikit-learn, PyTorch) uses NumPy arrays internally.

import numpy as np
# Create arrays
a = np.array([1.0, 2.0, 3.0])
b = np.zeros((3, 3)) # 3x3 matrix of zeros
c = np.linspace(0, 1, 100) # 100 evenly spaced values
# Vectorized operations — no loop needed
prices = np.array([10.0, 20.0, 30.0])
discounted = prices * 0.9 # [9.0, 18.0, 27.0]
print(discounted.mean()) # 18.0
print(discounted.sum()) # 54.0
# Matrix multiplication
A = np.random.randn(3, 4)
B = np.random.randn(4, 2)
C = A @ B # shape (3, 2)

Install: python -m pip install numpy

pandas provides the DataFrame — a labeled, tabular data structure. Use it for loading CSVs, cleaning records, filtering, grouping, and preparing features for ML models.

import pandas as pd
# Load data
df = pd.read_csv("orders.csv")
# Explore
print(df.shape) # (rows, columns)
print(df.dtypes) # column types
print(df.describe()) # summary statistics
# Filter
large_orders = df[df["amount"] > 100.0]
# Group and aggregate
monthly = df.groupby("month")["amount"].agg(["sum", "count", "mean"])
# Clean
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
df = df.dropna(subset=["amount"])
# Export
df.to_csv("orders_clean.csv", index=False)

Install: python -m pip install pandas

scikit-learn covers the full classical ML workflow: preprocessing, feature engineering, model training, evaluation, and pipelines. Use it for structured/tabular data classification, regression, and clustering.

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
import pandas as pd
df = pd.read_csv("orders.csv")
X = df[["amount", "items_count", "days_since_last_order"]]
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)
predictions = model.predict(X_test_scaled)
print(classification_report(y_test, predictions))

Install: python -m pip install scikit-learn

PyTorch is the leading framework for neural network research and production training. Use it when you need to train custom models, work with embeddings, or run inference on local GPU hardware.

import torch
import torch.nn as nn
# Check GPU availability
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Simple linear model
model = nn.Sequential(
nn.Linear(10, 64),
nn.ReLU(),
nn.Linear(64, 1),
nn.Sigmoid(),
).to(device)
# Forward pass
x = torch.randn(32, 10).to(device) # batch of 32 examples
output = model(x)
print(output.shape) # torch.Size([32, 1])

Install: python -m pip install torch (CPU-only). For GPU builds, use the official installation command which picks the right CUDA version.

For applications that call a hosted LLM rather than training their own models, use the provider’s Python SDK directly.

# OpenAI
from openai import OpenAI
client = OpenAI()
resp = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Summarize this order: ORD-001, BensonCo, $149.99"}],
)
print(resp.choices[0].message.content)
# Anthropic Claude
import anthropic
client = anthropic.Anthropic()
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=200,
messages=[{"role": "user", "content": "What is RAG?"}],
)
print(msg.content[0].text)
# Google Gemini
from google import genai
client = genai.Client()
resp = client.models.generate_content(model="gemini-2.5-flash", contents="What is RAG?")
print(resp.text)

Install: python -m pip install openai anthropic google-genai

LangChain is an orchestration framework for multi-step LLM workflows: retrieval pipelines, agent loops, tool use, and prompt management. Use it when a single API call is not enough — but do not introduce it prematurely.

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
# Build a simple RAG pipeline
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.load_local("./order_docs", embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
llm = ChatOpenAI(model="gpt-4o")
chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
result = chain.invoke({"query": "What is the return policy for electronics?"})
print(result["result"])

Install: python -m pip install langchain langchain-openai langchain-community faiss-cpu

Data analysis:

Terminal window
python -m pip install numpy pandas matplotlib seaborn

Classical machine learning:

Terminal window
python -m pip install numpy pandas scikit-learn xgboost

LLM application:

Terminal window
python -m pip install openai anthropic google-genai python-dotenv

RAG pipeline:

Terminal window
python -m pip install langchain langchain-openai chromadb tiktoken

Deep learning:

Terminal window
python -m pip install torch # see pytorch.org for GPU builds
  • Start with the smallest stack. A direct SDK call (openai.chat.completions.create) is simpler to debug, test, and maintain than a LangChain chain that wraps the same call.
  • Add orchestration when you have state, tools, or multi-step logic. If your workflow needs tool calling, loop retries, or memory across turns, LangChain or LlamaIndex adds value.
  • Check Python version compatibility before standardizing on a package. Some older ML packages lag behind the latest Python releases.
  • Pin versions in requirements.txt. The ML ecosystem moves fast — an unpinned install can break between CI runs.