{"record":{"id":"efb7d4745cffafa3","repo":"TheAlgorithms/Python","slug":"wrong-input-data-s-dimensions-dataset-datase","errorCode":null,"errorMessage":"Wrong input data's dimensions... dataset : {dataset.ndim}, value_array : {value_array.ndim}","messagePattern":"Wrong input data's dimensions\\.\\.\\. dataset : (.+?), value_array : (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/similarity_search.py","lineNumber":105,"sourceCode":"\n    3. If data types are different.\n    When trying to compare, we are expecting same types so they should be same.\n    If not, it'll come up with errors.\n    >>> dataset = np.array([[0, 0], [1, 1], [2, 2]], dtype=np.float32)\n    >>> value_array = np.array([[0, 0], [0, 1]], dtype=np.int32)\n    >>> similarity_search(dataset, value_array)  # doctest: +NORMALIZE_WHITESPACE\n    Traceback (most recent call last):\n        ...\n    TypeError: Input data have different datatype...\n    dataset : float32, value_array : int32\n    \"\"\"\n\n    if dataset.ndim != value_array.ndim:\n        msg = (\n            \"Wrong input data's dimensions... \"\n            f\"dataset : {dataset.ndim}, value_array : {value_array.ndim}\"\n        )\n        raise ValueError(msg)\n\n    try:\n        if dataset.shape[1] != value_array.shape[1]:\n            msg = (\n                \"Wrong input data's shape... \"\n                f\"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}\"\n            )\n            raise ValueError(msg)\n    except IndexError:\n        if dataset.ndim != value_array.ndim:\n            raise TypeError(\"Wrong shape\")\n\n    if dataset.dtype != value_array.dtype:\n        msg = (\n            \"Input data have different datatype... \"\n            f\"dataset : {dataset.dtype}, value_array : {value_array.dtype}\"\n        )\n        raise TypeError(msg)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/similarity_search.py#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Reshape the query to match the dataset's dimensionality: use value_array.reshape(1, -1) for a single query against a 2-D dataset.","Confirm both inputs were built with consistent nesting (list of row-vectors for both).","Print dataset.ndim and value_array.ndim right before the call to spot the mismatch."],"exampleFix":"# before\nresult = similarity_search(dataset, query_vector)  # query_vector is 1-D\n\n# after\nresult = similarity_search(dataset, np.atleast_2d(query_vector))","handlingStrategy":"validation","validationCode":"import numpy as np\ndataset = np.asarray(dataset)\nvalue_array = np.asarray(value_array)\nif dataset.ndim != value_array.ndim:\n    value_array = np.atleast_2d(value_array) if dataset.ndim == 2 else value_array.reshape(-1)\nassert dataset.ndim == value_array.ndim\nresult = similarity_search(dataset, value_array)","typeGuard":"def same_ndim(a: np.ndarray, b: np.ndarray) -> bool:\n    return a.ndim == b.ndim","tryCatchPattern":null,"preventionTips":["Normalize all inputs with np.asarray + np.atleast_2d at the pipeline boundary.","Print .shape of both arrays before first use in a new integration."],"tags":["numpy","input-validation","similarity-search"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}