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

07/2026: Reorganize code by subject under code/; two venvs (code + jupyter)

Environments

Two virtual environments: code for Python/PyTorch scripts, jupyter for notebook kernels.

Code / PyTorch (code/.venv)

cd code
uv venv .venv
source .venv/bin/activate
uv sync --extra pytorch          # PyTorch and computer vision
uv sync --extra llm --extra agents   # LLM and agent scripts

See code/SUBJECTS.md for subject folder mapping.

Jupyter (jupyter/.venv)

cd jupyter
uv venv .venv
source .venv/bin/activate
uv sync --extra deep-learning     # optional: Keras/TensorFlow notebooks
uv run python -m ipykernel install --user --name ml-studies-jupyter

Notebooks live under code/<subject>/ but use the jupyter kernel.

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:

# Code environment (scripts, PyTorch, LLM)
cd code && uv venv .venv && uv sync --extra pytorch

# Jupyter environment (notebooks under code/<subject>/)
cd jupyter && uv venv .venv && uv sync

Quick commands:

uv run python script.py
uv run jupyter lab

Subject folders are documented in code/SUBJECTS.md.

Legacy per-folder setup (deprecated)

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 code/deep-learning/fundamentals/.

Summary of the library and deeper studies

Code Samples

Code is organized by subject under code/: scripts and notebooks live together per topic. See SUBJECTS.md. End-to-end demos remain in e2e-demos/.

Perceptron

Located in code/perceptron/:

Code Description
test_perceptron.py Perceptron classifier for iris flowers using identity activation

Classification

Located in code/classification/:

Code Description
test_adaline.py ADAptive LInear NEuron with linear activation function
svm_iris.py Support Vector Machine on iris dataset
decision_tree_iris.py Decision tree classification

Regression

Located in code/regression/:

Code Description
demo_lasso_ridge.py L1/L2 regularization comparison
classify_with_pipe.py Logistic regression pipeline (code/logistic-regression/)

PyTorch Deep Learning

Located in code/deep-learning/ and code/computer-vision/:

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

LangChain and LLM Integration

Located in code/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 code/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 code/agents/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 code/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 by subject

Notebooks live under code/<subject>/. Use the jupyter environment kernel.

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