{ "cells": [ { "cell_type": "markdown", "id": "eb8baa51", "metadata": {}, "source": [ "# Quickstart: regression\n", "\n", "Fit a DJINN regressor on the California housing data, check how well it does,\n", "then save it, load it and keep training." ] }, { "cell_type": "code", "execution_count": 1, "id": "62c67ddf", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:25:17.180067Z", "iopub.status.busy": "2026-10-10T21:25:17.179882Z", "iopub.status.idle": "2026-10-10T21:25:18.945755Z", "shell.execute_reply": "2026-10-10T21:25:18.944877Z" } }, "outputs": [], "source": [ "import os\n", "import shutil\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from sklearn.datasets import fetch_california_housing\n", "from sklearn.model_selection import train_test_split\n", "\n", "from djinn import DJINN_Regressor, load" ] }, { "cell_type": "markdown", "id": "4befc214", "metadata": {}, "source": [ "## Data\n", "\n", "The full dataset has about 20,000 rows and 8 features. The target is the\n", "median house value of a block group in units of $100,000. A random 3,000 rows\n", "are enough here and keep the notebook quick." ] }, { "cell_type": "code", "execution_count": 2, "id": "7a3ef6cd", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:25:18.948770Z", "iopub.status.busy": "2026-10-10T21:25:18.948563Z", "iopub.status.idle": "2026-10-10T21:25:18.960716Z", "shell.execute_reply": "2026-10-10T21:25:18.960114Z" } }, "outputs": [ { "data": { "text/plain": [ "((2400, 8), (600, 8))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X, y = fetch_california_housing(return_X_y=True)\n", "rng = np.random.default_rng(0)\n", "idx = rng.choice(len(y), 3000, replace=False)\n", "X_train, X_test, y_train, y_test = train_test_split(\n", " X[idx], y[idx], test_size=0.2, random_state=0\n", ")\n", "X_train.shape, X_test.shape" ] }, { "cell_type": "markdown", "id": "b5f94f88", "metadata": {}, "source": [ "## Choosing a learning rate\n", "\n", "If `learning_rate` is left as `None`, `fit()` searches for a learning rate,\n", "number of epochs and batch size before it trains. The search can also be run on\n", "its own with `get_hyperparameters()`, and the results passed to the\n", "constructor. That's worth doing when you'll fit the same data more than once,\n", "since the search takes longer than the training itself." ] }, { "cell_type": "code", "execution_count": 3, "id": "1a82c748", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:25:18.962204Z", "iopub.status.busy": "2026-10-10T21:25:18.962086Z", "iopub.status.idle": "2026-10-10T21:26:22.518938Z", "shell.execute_reply": "2026-10-10T21:26:22.518309Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 11min 39s, sys: 57.4 s, total: 12min 36s\n", "Wall time: 1min 3s\n" ] }, { "data": { "text/plain": [ "{'batch_size': 120,\n", " 'learning_rate': 0.0054612805388816795,\n", " 'learn_rate': 0.0054612805388816795,\n", " 'epochs': 210,\n", " 'ntrees': 1}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "search = DJINN_Regressor(n_trees=1, max_tree_depth=6, random_state=0)\n", "hyper = search.get_hyperparameters(X_train, y_train, seed=0)\n", "hyper" ] }, { "cell_type": "markdown", "id": "02fad809", "metadata": {}, "source": [ "## Fit\n", "\n", "`n_trees` is the number of trees in the random forest, and so the number of\n", "networks in the ensemble. `max_tree_depth` limits how deep each tree grows,\n", "and the network built from a tree has `max_tree_depth - 1` hidden layers." ] }, { "cell_type": "code", "execution_count": 4, "id": "4219c33a", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:26:22.520709Z", "iopub.status.busy": "2026-10-10T21:26:22.520494Z", "iopub.status.idle": "2026-10-10T21:26:39.606345Z", "shell.execute_reply": "2026-10-10T21:26:39.605859Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3min 9s, sys: 15.5 s, total: 3min 24s\n", "Wall time: 17.1 s\n" ] }, { "data": { "text/html": [ "
DJINN_Regressor(batch_size=120, epochs=210, learning_rate=0.0054612805388816795,\n",
" max_tree_depth=6, n_trees=3, random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. DJINN_Regressor(batch_size=120, epochs=210, learning_rate=0.0054612805388816795,\n",
" max_tree_depth=6, n_trees=3, random_state=0)