{"record":{"id":"14d2fd6516f12575","repo":"TheAlgorithms/Python","slug":"input-data-have-different-datatype-dataset-d","errorCode":null,"errorMessage":"Input data have different datatype... dataset : {dataset.dtype}, value_array : {value_array.dtype}","messagePattern":"Input data have different datatype\\.\\.\\. dataset : (.+?), value_array : (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"machine_learning/similarity_search.py","lineNumber":123,"sourceCode":"        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)\n\n    answer = []\n\n    for value in value_array:\n        dist = euclidean(value, dataset[0])\n        vector = dataset[0].tolist()\n\n        for dataset_value in dataset[1:]:\n            temp_dist = euclidean(value, dataset_value)\n\n            if dist > temp_dist:\n                dist = temp_dist\n                vector = dataset_value.tolist()\n\n        answer.append([vector, dist])\n\n    return answer\n","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/similarity_search.py#L105-L141","documentation":"Raised as a TypeError by similarity_search when dataset and value_array have different NumPy dtypes (e.g. float32 vs int32). The implementation requires identical dtypes so distance arithmetic is consistent; this is a deliberate strictness beyond what NumPy broadcasting would allow.","triggerScenarios":"Calling similarity_search where dataset.dtype != value_array.dtype, such as an integer dataset from np.arange with float queries from np.random.rand, or float32 model vectors with float64 queries.","commonSituations":"Mixing arrays from different sources (CSV load gives float64, saved .npy gives float32), or arrays created via literal Python ints on one side and floats on the other.","solutions":["Cast both to a common dtype before calling: np.asarray(x, dtype=np.float64) on dataset and value_array.","If dtype fidelity matters (float32 for memory), cast the queries to the dataset's dtype rather than the reverse.","Standardize array creation in your pipeline so both sides come from the same loader."],"exampleFix":"# before\nresult = similarity_search(dataset.astype(np.int32), queries_float)\n\n# after\nresult = similarity_search(\n    np.asarray(dataset, dtype=np.float64),\n    np.asarray(queries_float, dtype=np.float64),\n)","handlingStrategy":"validation","validationCode":"common_dtype = np.float64\ndataset = np.asarray(dataset, dtype=common_dtype)\nvalue_array = np.asarray(value_array, dtype=common_dtype)\nresult = similarity_search(dataset, value_array)","typeGuard":"def same_dtype(a: np.ndarray, b: np.ndarray) -> bool:\n    return a.dtype == b.dtype","tryCatchPattern":null,"preventionTips":["Cast to one dtype at load time; do not rely on implicit promotion (this API forbids it).","When memory matters, standardize the whole pipeline on float32, not just one side."],"tags":["numpy","dtype","input-validation","similarity-search"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}