RuntimeWarning: divide by zero encountered in divide average_loss1=np.sum(np.abs((predict-testY)/testY))/p
时间: 2024-04-30 16:22:29 浏览: 12
This warning occurs when the code tries to divide a number by zero. In this case, it is likely that the testY data contains some values that are zero, causing the division to fail. To avoid this warning, you can add a check to ensure that the denominator is not zero before dividing. For example:
```
mask = testY != 0
average_loss1 = np.sum(np.abs((predict-testY)/testY[mask])) / np.sum(mask)
```
This code creates a boolean mask to identify the non-zero values in testY, and then uses this mask to calculate the average loss only for those values.
相关问题
RuntimeWarning: divide by zero encountered in power d_inv_sqrt = np.power(rowsum, -0.5).flatten()
这个警告是因为有些行的元素和为0,导致在计算倒数平方根时出现了除以0的情况。你可以在代码中加入判断,避免除以0的情况发生,例如:
```python
rowsum = np.sum(adj_matrix, axis=1)
rowsum[rowsum == 0] = 1 # 避免除以0的情况
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
```
这样就可以避免出现警告了。
RuntimeWarning: divide by zero encountered in scalar divide m_lr_i = np.log(numerator / denominator)
这个错误是由于除数为0导致的。你可以在计算除法之前加一个判断,如果分母为0,则将结果设置为一个极大值或者0。例如,可以将代码修改为:
```
if denominator == 0:
m_lr_i = 1e9 # 或者设置为0
else:
m_lr_i = np.log(numerator / denominator)
```
这样就能避免这个错误了。另外,如果你需要处理大量的计算,可以考虑使用NumPy等库来加速计算过程。