Skip to content

Refuse a 0-row matrix in DBSCAN::predict - #7

Merged
SomeB1oody merged 2 commits into
masterfrom
fix/dbscan-empty-predict
Oct 6, 2026
Merged

SomeB1oody merged 2 commits into
masterfrom
fix/dbscan-empty-predict

Conversation

@SomeB1oody

@SomeB1oody SomeB1oody commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Problem

DBSCAN::predict returns an empty array for a matrix with 0 rows. Every other model returns EmptyInput. The early return also comes before the feature-count check. A 0-row matrix with the wrong feature count therefore returns Ok, where it should return an error.

Reproduction

use ndarray::{Array2, array};
use rustyml::machine_learning::DBSCAN;

fn main() {
    let x = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.0], [5.0, 5.0], [5.1, 5.1], [5.0, 5.2]];
    let mut model = DBSCAN::new(0.5, 2).unwrap();
    model.fit(&x).unwrap();

    // The model has 2 features. Both 0-row matrices return Ok
    println!("0x2: {:?}", model.predict(&Array2::<f64>::zeros((0, 2))));
    println!("0x3: {:?}", model.predict(&Array2::<f64>::zeros((0, 3))));
}

Output on master:

0x2: Ok([], shape=[0], strides=[0], layout=CFcf (0xf), const ndim=1)
0x3: Ok([], shape=[0], strides=[0], layout=CFcf (0xf), const ndim=1)

Fix

This PR removes the early return, so predict goes through validate_predict_input like every other model. This is a behavior change, and CHANGELOG.md marks it as one. A fit that finds no core point still stores a (0, n_features) matrix, so the feature-count check stays correct for that model.

Output on this branch:

0x2: Err(EmptyInput("dataset to predict on"))
0x3: Err(EmptyInput("dataset to predict on"))

SomeB1oody and others added 2 commits October 5, 2026 16:32
DBSCAN::predict now returns EmptyInput for a matrix with 0 rows, as
every other model does. Before, it returned an empty array, and it
skipped the feature-count check for that matrix.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@SomeB1oody
SomeB1oody merged commit 403042f into master Oct 6, 2026
3 of 4 checks passed
@SomeB1oody
SomeB1oody deleted the fix/dbscan-empty-predict branch October 6, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant