TheAlgorithms/Python · error · ValueError

Wrong input data's dimensions... dataset : {dataset.ndim}, v

Error message

Wrong input data's dimensions... dataset : {dataset.ndim}, value_array : {value_array.ndim}

What it means

Raised by similarity_search when the dataset and the value_array (queries) have different numbers of dimensions, e.g. one is a 1-D vector and the other a 2-D matrix. The function computes nearest neighbours by Euclidean distance and requires both arrays to live in the same dimensional layout before comparing shapes element-wise.

Source

Thrown at machine_learning/similarity_search.py:105

    3. If data types are different.
    When trying to compare, we are expecting same types so they should be same.
    If not, it'll come up with errors.
    >>> dataset = np.array([[0, 0], [1, 1], [2, 2]], dtype=np.float32)
    >>> value_array = np.array([[0, 0], [0, 1]], dtype=np.int32)
    >>> similarity_search(dataset, value_array)  # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    TypeError: Input data have different datatype...
    dataset : float32, value_array : int32
    """

    if dataset.ndim != value_array.ndim:
        msg = (
            "Wrong input data's dimensions... "
            f"dataset : {dataset.ndim}, value_array : {value_array.ndim}"
        )
        raise ValueError(msg)

    try:
        if dataset.shape[1] != value_array.shape[1]:
            msg = (
                "Wrong input data's shape... "
                f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"
            )
            raise ValueError(msg)
    except IndexError:
        if dataset.ndim != value_array.ndim:
            raise TypeError("Wrong shape")

    if dataset.dtype != value_array.dtype:
        msg = (
            "Input data have different datatype... "
            f"dataset : {dataset.dtype}, value_array : {value_array.dtype}"
        )
        raise TypeError(msg)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Reshape the query to match the dataset's dimensionality: use value_array.reshape(1, -1) for a single query against a 2-D dataset.
  2. Confirm both inputs were built with consistent nesting (list of row-vectors for both).
  3. Print dataset.ndim and value_array.ndim right before the call to spot the mismatch.

Example fix

# before
result = similarity_search(dataset, query_vector)  # query_vector is 1-D

# after
result = similarity_search(dataset, np.atleast_2d(query_vector))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
dataset = np.asarray(dataset)
value_array = np.asarray(value_array)
if dataset.ndim != value_array.ndim:
    value_array = np.atleast_2d(value_array) if dataset.ndim == 2 else value_array.reshape(-1)
assert dataset.ndim == value_array.ndim
result = similarity_search(dataset, value_array)

Type guard

def same_ndim(a: np.ndarray, b: np.ndarray) -> bool:
    return a.ndim == b.ndim

Prevention

When it happens

Trigger: Calling similarity_search(dataset, value_array) where dataset.ndim != value_array.ndim, for example passing a 2-D dataset with a single flat 1-D query vector, or a 1-D dataset with a 2-D batch of queries.

Common situations: Forgetting to wrap a single query in brackets (query = [q] vs q), transposing one array but not the other, or mixing data loaded via np.array(list-of-lists) with np.array(flat-list).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/efb7d4745cffafa3. Report an issue: GitHub.