{ "cells": [ { "cell_type": "markdown", "id": "1c6b3c50", "metadata": {}, "source": [ "# Multiple outputs\n", "\n", "DJINN can predict several targets at once with one network per tree. Here the\n", "inputs are the launch speed and angle of a projectile, and the two outputs are\n", "how far it goes and how high it gets." ] }, { "cell_type": "code", "execution_count": 1, "id": "4a5368ac", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:27:06.647764Z", "iopub.status.busy": "2026-10-10T21:27:06.647555Z", "iopub.status.idle": "2026-10-10T21:27:08.368833Z", "shell.execute_reply": "2026-10-10T21:27:08.368056Z" } }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from sklearn.metrics import mean_absolute_error, r2_score\n", "from sklearn.model_selection import train_test_split\n", "\n", "from djinn import DJINN_Regressor" ] }, { "cell_type": "markdown", "id": "d1768b66", "metadata": {}, "source": [ "## Data\n", "\n", "Without drag, a projectile launched at speed $v$ and angle $\\theta$ travels a\n", "range $R = v^2 \\sin(2\\theta) / g$ and reaches a height\n", "$H = v^2 \\sin^2(\\theta) / (2g)$. A little noise is added to both." ] }, { "cell_type": "code", "execution_count": 2, "id": "3918d2a4", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:27:08.371162Z", "iopub.status.busy": "2026-10-10T21:27:08.370953Z", "iopub.status.idle": "2026-10-10T21:27:08.377588Z", "shell.execute_reply": "2026-10-10T21:27:08.377012Z" } }, "outputs": [ { "data": { "text/plain": [ "((1600, 2), (1600, 2))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng(0)\n", "n = 2000\n", "speed = rng.uniform(10, 50, n) # m/s\n", "angle = rng.uniform(10, 80, n) # degrees\n", "g = 9.81\n", "\n", "theta = np.radians(angle)\n", "distance = speed**2 * np.sin(2 * theta) / g\n", "height = speed**2 * np.sin(theta) ** 2 / (2 * g)\n", "\n", "X = np.column_stack([speed, angle])\n", "Y = np.column_stack([distance, height])\n", "Y += rng.normal(0, 0.02 * Y.std(axis=0), Y.shape)\n", "\n", "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)\n", "X_train.shape, Y_train.shape" ] }, { "cell_type": "markdown", "id": "725c33f6", "metadata": {}, "source": [ "## Fit\n", "\n", "Nothing changes for multiple outputs: pass a 2-D `y` and the networks get one\n", "output neuron per column. The random forest is fit on all the outputs together,\n", "so each tree's splits are chosen for both targets." ] }, { "cell_type": "code", "execution_count": 3, "id": "ffbb25cf", "metadata": { "execution": { "iopub.execute_input": "2026-10-10T21:27:08.379609Z", "iopub.status.busy": "2026-10-10T21:27:08.379497Z", "iopub.status.idle": "2026-10-10T21:27:34.974175Z", "shell.execute_reply": "2026-10-10T21:27:34.973589Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4min 41s, sys: 24.2 s, total: 5min 5s\n", "Wall time: 26.6 s\n" ] }, { "data": { "text/html": [ "
DJINN_Regressor(epochs=300, learning_rate=0.005, max_tree_depth=6, n_trees=3,\n",
" random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. DJINN_Regressor(epochs=300, learning_rate=0.005, max_tree_depth=6, n_trees=3,\n",
" random_state=0)