Skip to content

Coding

Update

05/02/2023 Move to python 3.10 in docker, retest docker env with all code. See samples section below.

09/10/2023: Add PyTorch

12/2023: Clean Jupyter

01/2026: Migrate to uv for package management

Environments

uv is a fast Python package and project manager written in Rust. It replaces pip, pip-tools, pipx, poetry, pyenv, virtualenv, and more.

Installation:

# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# or via Homebrew
brew install uv

Project setup for examples and demo as subfolder within this project:

# Initialize a new project with pyproject.toml
uv init

# Create virtual environment and install dependencies
uv sync

# Add a dependency
uv add numpy pandas torch

# Run a script
uv run python script.py

# Run Jupyter
uv run jupyter lab

Quick environment for existing projects:

# Create venv and install from pyproject.toml or requirements.txt
uv venv
source .venv/bin/activate
uv pip install -r requirements.txt

Each project folder contains a pyproject.toml for dependency management.

VSCode

Jupyter Notebook

  • To select an environment, use the Python: Select Interpreter command from the Command Palette (⇧⌘P)
  • Use Create: New Jupyter Notebook from command Palette
  • Select a kernel using the kernel picker in the top right.
  • Within a Python Notebook, it's possible to view, inspect, sort, and filter the variables within the current Jupyter session, using Variables in toolbar.
  • We can offload intensive computation in a Jupyter Notebook to other computers by connecting to a remote Jupyter server. Use server URL with security token.

Run Kaggle image

As an alternate Kaggle has a more complete docker image to start with.

# CPU based
docker run --rm -v $(pwd):/home -it gcr.io/kaggle-images/python /bin/bash
# GPU based
docker run -v $(pwd):/home --runtime nvidia --rm -it gcr.io/kaggle-gpu-images/python /bin/bash

Important Python Libraries

numpy

  • Array computing in Python. Numpy official quickstar.t
  • NumPy dimensions are called axes.

    import numpy as np
    a = np.array([2, 3, 4])
    b = np.ones((2, 3, 4), dtype=np.int16)
    c = np.zeros((3, 4))
    

  • Create a sequence of number: np.arange(10, 30, 5)

  • Matrix product: using .dot or @
    A = np.array([[1, 1], [0, 1]])
    B = np.array([[2, 0], [3, 4]])
    A @ B
    A.dot(B)
    

scipy

SciPy is a collection of mathematical algorithms and convenience functions built on top of NumPy. See product documentation.

  • Get a normal distribution function: use the probability density function (pdf)
    from scipy.stats import norm
    x = np.arange(-3, 3, 0.01)
    y=norm.pdf(x)
    

MatPlotLib

Persent figure among multiple axes, from the data for human analysis.

  • Classic import

    import matplotlib.pyplot as plt
    import numpy as np
    
    import matplotlib as mpl
    

  • See Notebook

Seaborn

Seaborn provides a high-level interface for drawing attractive and informative statistical graphics. Based on top of MatPlotLib and integrated with Pandas.

See the introduction for different examples

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
sns.relplot(
    data=masses_data,
    x="age", y="shape", 
    hue="density", size="density"
)
plt.show()

PyTorch

Via conda or pip, install pytorch torchvision torchaudio.

Example of getting started code in deep-neural-net folder.

Summary of the library and deeper studies

Code Samples

Code is organized in three main folders: examples/ for library-specific samples, e2e-demos/ for end-to-end applications, and notebooks/ for Jupyter notebooks.

Machine Learning Classifiers

Located in examples/ml-python/classifiers/:

Code Description
TestPerceptron.py Perceptron classifier for iris flowers using identity activation
TestAdaline.py ADAptive LInear NEuron with linear activation function
SVM-IRIS.py Support Vector Machine on iris dataset
DecisionTreeIRIS.py Decision tree classification
demo_lasso_ridge.py L1/L2 regularization comparison

PyTorch Deep Learning

Located in examples/pytorch/:

Code Description
get_started/ Tensor basics, workflow notebooks
classification/classifier.ipynb Binary classification with neural networks
classification/multiclass-classifier.ipynb Multi-class classification
computer-vision/fashion_cnn.py CNN on Fashion MNIST
computer-vision/transfer_learning.py Transfer learning with EfficientNet
ddp/ Distributed Data Parallel training

LangChain and LLM Integration

Located in examples/llm-langchain/:

Code Description
openai/ OpenAI API integration, agents, streaming
anthropic/ Claude integration
bedrock/ AWS Bedrock with CoT prompts
mistral/ Mistral AI tool calling
gemini/ Google Gemini chat
cohere/ Cohere integration

RAG Implementations

Located in examples/llm-langchain/rag/:

Code Description
build_agent_domain_rag.py Build RAG with ChromaDB and OpenAI
multiple_queries_rag.py Multi-query RAG pattern
rag_fusion.py RAG fusion with reciprocal rank
rag_hyde.py Hypothetical Document Embedding

LangGraph Agent Patterns

Located in examples/llm-langchain/langgraph/:

Code Description
first_graph_with_tool.py Basic graph with tool calling
react_lg.py ReAct pattern implementation
adaptive_rag.py Adaptive RAG with routing
human_in_loop.py Human-in-the-loop pattern
ask_human_graph.py Human approval workflow
stream_agent_node.py Streaming agent output

Ollama Local LLM

Located in examples/llm-ollama/:

Code Description
chat_with_mistral.py Chat with local Mistral
async_chat_with_mistral.py Async chat streaming
chat_with_ollama_openai_api.py Ollama with OpenAI-compatible API

End-to-End Demos

Located in e2e-demos/:

Demo Description
qa_retrieval/ Q&A with RAG and ChromaDB
chat_with_pdf/ PDF document chat application
streaming-demo/ WebSocket streaming with LangGraph
resume_tuning/ Resume optimization with LLM
think_deeply/ Deep reasoning with LLM
gemini_cmd/ Gemini CLI integration

UI Frameworks

Located in techno/:

Framework Code
CrewAI techno/crew-ai/ - Multi-agent examples
Streamlit techno/streamlit/ - Dashboard apps
Gradio techno/gradio/ - ML interfaces
NiceGUI techno/nicegui/ - Python web UI
Taipy techno/taipy/ - Data apps

Jupyter Notebooks

Located in notebooks/:

Notebook Topic
ConditionalProbabilityExercise.ipynb Probability and Bayes
Distributions.ipynb Statistical distributions
LinearRegression.ipynb Linear regression basics
KNN.ipynb K-Nearest Neighbors
DecisionTree.ipynb Decision tree classifier
KMeans.ipynb K-Means clustering
PCA.ipynb Principal Component Analysis
Keras-CNN.ipynb CNN with Keras
Keras-RNN.ipynb RNN with Keras