DocumentationNeurondB Documentation

Unified ML API

Create Training Dataset

First, create a classification dataset with features and labels:

Create training data

-- Create training data: 10,000 transactions
CREATE TEMP TABLE unified_train_data AS
SELECT 
    i as transaction_id,
    ARRAY[
        (random() * 100)::real,   -- Feature 1: Transaction amount
        (random() * 50)::real,    -- Feature 2: Account age
        (random() * 10)::real     -- Feature 3: Location risk score
    ]::real[] as features,
    CASE WHEN random() > 0.7 THEN 1 ELSE 0 END as is_fraud
FROM generate_series(1, 10000) i;

Train Multiple Models

Use the unified neurondb.train() function to train different algorithms:

Linear Regression

Train linear regression

SELECT neurondb.train(
    'fraud_detection',           -- Project name
    'linear_regression',         -- Algorithm
    'unified_train_data',        -- Training table
    'is_fraud'                   -- Target column
) as model_id;

Logistic Regression

Train logistic regression

SELECT neurondb.train(
    'fraud_detection',           -- Project name
    'logistic_regression',       -- Algorithm
    'unified_train_data',        -- Training table
    'is_fraud'                   -- Target column
) as model_id;

Random Forest with Hyperparameters

Train random forest

SELECT neurondb.train(
    'fraud_detection',           -- Project name
    'random_forest',             -- Algorithm
    'unified_train_data',        -- Training table
    'is_fraud',                  -- Target column
    NULL,                        -- Feature columns (NULL = use all)
    '{
      "n_trees": 20, 
      "max_depth": 8, 
      "min_samples": 50
    }'::jsonb                    -- Hyperparameters
) as model_id;

Make Predictions

Use neurondb.predict() to make predictions with trained models:

Make predictions

SELECT neurondb.predict(
    'fraud_detection',           -- Project name
    'random_forest',             -- Algorithm
    features                     -- Input features
) as prediction
FROM test_data;

Deploy Models

Deploy models for production use:

Deploy model

SELECT neurondb.deploy(
    'fraud_detection',           -- Project name
    'random_forest',             -- Algorithm
    'production'                 -- Environment
) as deployment_id;

Next Steps