gist of IPYNB with all code

Utility code


Datasets preview

# Generate and plot datasets
datasets: list[Dataset] = generate_datasets()
plot_datasets(datasets)

png

Models

Simple models

Linear regression

Demo

linear_dataset = datasets[0]
linear_regression_example(X=linear_dataset.X, y=linear_dataset.y)

png

Logistic regression

Demo

logistic_dataset = datasets[1]
logistic_regression_example(X=logistic_dataset.X, y=logistic_dataset.y)

png

GLM (Poisson)

Demo

poisson_dataset = datasets[2]
glm_poisson_example(poisson_dataset.X, poisson_dataset.y)

png

Nonlinear models - tree-based

nonlinear_dataset = datasets[3]

Decision tree

Demo

dt = decision_tree_example(X=nonlinear_dataset.X, y=nonlinear_dataset.y)
Prediction error: 0.02740

png

Analysis

import dtreeviz
 
viz_model = dtreeviz.model(
    model=dt,
    X_train=nonlinear_dataset.X,
    y_train=nonlinear_dataset.y,
    feature_names="X",
    target_name="y",
)
viz_model.view(scale=1.5, x=[1.2])

svg

Random forest

Demo

random_forest_example(X=nonlinear_dataset.X, y=nonlinear_dataset.y)
Prediction error: 0.02408

png

Gradient boosting

Demo

gradient_boosting_example(X=nonlinear_dataset.X, y=nonlinear_dataset.y)
Prediction error: 0.01625

png

Nonlinear models - other

Neural network

Demo

mlp = neural_network_example(
    X=nonlinear_dataset.X,
    y=nonlinear_dataset.y,
    activation="relu",  # tanh, relu, logistic, identity
    analyze=False,
)
Prediction error: 0.009900

png

Analysis

Plot
plot_activation_functions()

png

mlp = neural_network_example(
    X=nonlinear_dataset.X,
    y=nonlinear_dataset.y,
    activation="relu",  # tanh, relu, logistic, identity
    analyze=True,
)
Prediction error: 0.009900

Layer 1 equations:
z1_0 = relu(0.931x + 0.498)
z1_1 = relu(0.334x + -0.371)
z1_2 = relu(-0.218x + 0.941)
z1_3 = relu(-0.303x + 1.093)
z1_4 = relu(-0.801x + -0.848)

Layer 2 equations:
z2_0 = relu(0.302z1_0 + 0.808z1_1 + -0.812z1_2 + -1.661z1_3 + 0.000z1_4 + -0.283)
z2_1 = relu(-0.859z1_0 + 0.361z1_1 + 0.676z1_2 + 0.485z1_3 + -1.997z1_4 + 0.087)
z2_2 = relu(-0.446z1_0 + 0.504z1_1 + -0.176z1_2 + 0.068z1_3 + -0.111z1_4 + 0.497)
z2_3 = relu(0.631z1_0 + 0.487z1_1 + -0.312z1_2 + -0.111z1_3 + 0.000z1_4 + -0.729)
z2_4 = relu(-0.000z1_0 + -0.000z1_1 + -0.000z1_2 + 0.000z1_3 + -0.000z1_4 + -0.396)
z2_5 = relu(-0.619z1_0 + 0.321z1_1 + 0.110z1_2 + -0.022z1_3 + 0.718z1_4 + -0.858)
z2_6 = relu(-1.151z1_0 + -0.000z1_1 + 0.318z1_2 + 0.250z1_3 + -0.673z1_4 + 0.646)
z2_7 = relu(0.567z1_0 + 0.400z1_1 + -0.143z1_2 + -0.457z1_3 + -0.000z1_4 + -0.523)

Layer 3 equations:
y = 1.975z2_0 + -0.825z2_1 + -0.481z2_2 + -1.145z2_3 + 0.000z2_4 + -1.401z2_5 + -1.383z2_6 + -0.827z2_7 + 1.604

png

3D example