numpy进行复现时候高维矩阵是否对齐api

本文介绍了NumPy库中的assert_allclose和allclose函数,用于检查两个数组是否足够接近,包括精确度和NaN处理。通过实例演示了它们的用法,并对比了两者的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy.testing.assert_allclose — NumPy v1.21 Manual

x = [1e-5, 1e-3, 1e-1]
y = np.arccos(np.cos(x))
np.testing.assert_allclose(x, y, rtol=1e-5, atol=0)

numpy.allclose — NumPy v1.21 Manual

np.allclose([1e10,1e-7], [1.00001e10,1e-8])
False
np.allclose([1e10,1e-8], [1.00001e10,1e-9])
True
np.allclose([1e10,1e-8], [1.0001e10,1e-9])
False
np.allclose([1.0, np.nan], [1.0, np.nan])
False
np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
True

参考: 

从Python Examples of numpy.testing.assert_allclose

python - Test if two numpy arrays are (close to) equal, including shape - Stack Overflow

Traceback (most recent call last): File "D:\35433\Documents\机器学习\任务1\任务1.py", line 17, in <module> (theta,cost_history) = linear_regression.train(learning_rate,num_iterations) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\35433\Documents\机器学习\任务1\梯度下降类.py", line 11, in train cost_history = self.gradient_descent(alpha,num_iterations) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\35433\Documents\机器学习\任务1\梯度下降类.py", line 18, in gradient_descent self.gradient_step(alpha)#每次都走一步,更新一次theta(w,参数列) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\35433\Documents\机器学习\任务1\梯度下降类.py", line 26, in gradient_step t = prediction - self.labels#设置了一个临时变量,预测值-真实值 ~~~~~~~~~~~^~~~~~~~~~~~~ File "D:\python\Lib\site-packages\pandas\core\generic.py", line 2171, in __array_ufunc__ return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\arraylike.py", line 276, in array_ufunc result = maybe_dispatch_ufunc_to_dunder_op(self, ufunc, method, *inputs, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "ops_dispatch.pyx", line 113, in pandas._libs.ops_dispatch.maybe_dispatch_ufunc_to_dunder_op File "D:\python\Lib\site-packages\pandas\core\ops\common.py", line 76, in new_method return method(self, other) ^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\arraylike.py", line 198, in __rsub__ return self._arith_method(other, roperator.rsub) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\series.py", line 6135, in _arith_method return base.IndexOpsMixin._arith_method(self, other, op) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\base.py", line 1384, in _arith_method return self._construct_result(result, name=res_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\series.py", line 6231, in _construct_result out = self._constructor(result, index=self.index, dtype=dtype, copy=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\series.py", line 584, in __init__ data = sanitize_array(data, index, dtype, copy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\construction.py", line 659, in sanitize_array subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\construction.py", line 718, in _sanitize_ndim raise ValueError( ValueError: Data must be 1-dimensional, got ndarray of shape (150, 150) instead
最新发布
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值