RuntimeWarning: invalid value encountered in divide predict[:,i] = sum_mat[:,i+1] / sum_mat[:,0]
时间: 2024-05-16 07:12:16 浏览: 102
错误的提示
This warning occurs when there is a division by zero or a division of a non-finite number (such as infinity or NaN). In this case, it looks like the division is attempting to divide by the first column of the matrix `sum_mat`, which could potentially contain zeros or non-finite values.
To address this warning, you can add a check to ensure that the denominator is not zero or non-finite before performing the division. For example:
```
denominator = sum_mat[:,0]
denominator[denominator == 0] = np.nan
predict[:,i] = sum_mat[:,i+1] / denominator
```
This will replace any zeros in the denominator with NaN, which will propagate through the division and result in NaNs in the output where division by zero occurred. Alternatively, you can set the denominator to a small value instead of NaN to avoid the warning, but this may lead to inaccurate predictions.
阅读全文