{"id":1038,"hash":"15ef65fa40c464770481eb0c7440b71443d2b4a482504c5aabf17de43f808bb8","pattern":"sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(&#39;float64&#39;)","full_message":"I am using sklearn and having a problem with the affinity propagation. I have built an input matrix and I keep getting the following error. \n\nValueError: Input contains NaN, infinity or a value too large for dtype('float64').\n\nI have run\n\nnp.isnan(mat.any()) #and gets False\nnp.isfinite(mat.all()) #and gets True\n\nI tried using\n\nmat[np.isfinite(mat) == True] = 0\n\nto remove the infinite values but this did not work either. \nWhat can I do to get rid of the infinite values in my matrix, so that I can use the affinity propagation algorithm?\n\nI am using anaconda and python 2.7.9.","ecosystem":"pypi","package_name":"python-2.7","package_version":null,"solution":"This might happen inside scikit, and it depends on what you're doing. I recommend reading the documentation for the functions you're using. You might be using one which depends e.g. on your matrix being positive definite and not fulfilling that criteria.\n\nEDIT: How could I miss that:\n\nnp.isnan(mat.any()) #and gets False\nnp.isfinite(mat.all()) #and gets True\n\nis obviously wrong. Right would be:\n\nnp.any(np.isnan(mat))\n\nand\n\nnp.all(np.isfinite(mat))\n\nYou want to check whether any of the elements are NaN, and not whether the return value of the any function is a number...","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/31323499/sklearn-error-valueerror-input-contains-nan-infinity-or-a-value-too-large-for","votes":272,"created_at":"2026-04-19T04:52:13.797375+00:00","updated_at":"2026-04-19T04:52:13.797375+00:00"}