以下是一段求解MSE的代码:
import numpy as np
def mse_loss(_y_true: np.ndarray, _y_pred: np.ndarray) -> np.float:
return ((_y_true - _y_pred) ** 2).mean()
y_true = np.array([1, 0, 0, 1])
y_pred = np.array([0, 0, 0, 0])
print(mse_loss(y_true, y_pred))
运行报错:
DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations after removing the cwd from sys.path.
解决方法:将np.float
替换成np.float64
。
补充说明:Python编码时明确指定参数类型和返回值类型是一个很好的习惯!
done