numpy stack在numpy 1.23中无法用于astropy量。

huangapple go评论55阅读模式
英文:

numpy stack not working for astropy quantities in numpy 1.23

问题

以下是翻译好的部分:

在numpy 1.23.5中以下代码可行,但在1.24.3中不行:

from astropy import units as u
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
np.stack([a,b]*u.m)

使用numpy 1.24.3时,我得到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 200, in stack
  File "~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py", line 1683, in __array_function__
    return super().__array_function__(function, types, args, kwargs)
  File "~/opt/anaconda3/lib/python3.9/site-packages/numpy/core/shape_base.py", line 471, in stack
    return _nx.concatenate(expanded_arrays, axis=axis, out=out,
  File "<__array_function__ internals>", line 200, in concatenate
  File "~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py", line 1688, in __array_function__
    args, kwargs, unit, out = function_helper(*args, **kwargs)
TypeError: concatenate() got an unexpected keyword argument 'dtype'

而在numpy 1.23.5中,我得到:

<Quantity [[[1., 2.],
            [3., 4.]],

           [[5., 6.],
            [7., 8.]]] m>

我是否做错了什么,或者是numpy 1.24引入了一个错误,还是需要更新astropy units以使其与新的numpy版本兼容?

英文:

The following works in numpy 1.23.5 but not in 1.24.3:

from astropy import units as u
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
np.stack([a,b]*u.m)

With numpy 1.24.3, I get:

Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;&lt;__array_function__ internals&gt;&quot;, line 200, in stack
  File &quot;~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py&quot;, line 1683, in __array_function__
    return super().__array_function__(function, types, args, kwargs)
  File &quot;~/opt/anaconda3/lib/python3.9/site-packages/numpy/core/shape_base.py&quot;, line 471, in stack
    return _nx.concatenate(expanded_arrays, axis=axis, out=out,
  File &quot;&lt;__array_function__ internals&gt;&quot;, line 200, in concatenate
  File &quot;~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py&quot;, line 1688, in __array_function__
    args, kwargs, unit, out = function_helper(*args, **kwargs)
TypeError: concatenate() got an unexpected keyword argument &#39;dtype&#39;

While with numpy 1.23.5, I get:

&lt;Quantity [[[1., 2.],
            [3., 4.]],

           [[5., 6.],
            [7., 8.]]] m&gt;

Am I doing something wrong or has been a bug introduced in numpy 1.24 or does astropy units have to be updated to work with the new numpy version?

答案1

得分: 2

这是我的设置,对我有效:

Python 3.9.12(主要版本,2022年4月5日,01:53:17)[Clang 12.0.0];
Numpy 1.24.3;
Astropy 5.3。

在NumPy 1.24.3版本中,开发人员为函数stack添加了新的参数:casting和dtype。
根据提供的错误消息,看起来astropy收到了意外的关键字dtype,这可能是一个问题。我假设您的astropy版本为5.1,并建议升级至5.3。

英文:

It's work for me with setup:

Python 3.9.12 (main, Apr 5 2022, 01:53:17) [Clang 12.0.0 ]<br>
Numpy 1.24.3<br>
Astropy 5.3

In version NumPy 1.24.3 developers added new arguments for function stack: <i>casting</i> and <i>dtype</i>.
As I see in provided error message, astropy got unexpected keyword dtype and that may be an issue here. I assume that your version of astropy - 5.1 and recommend upgrade to 5.3.

答案2

得分: 0

遇到了相同的消息/问题 - Conda 无法使用 "conda update astropy" 更新到 5.3.1 版本的 astropy(一直给我 v5.1)。最后我使用 pip install 安装了它。即使使用 astropy 5.3.1,它仍然无法与 numpy 1.24.3 一起工作。不得不降级到 numpy 1.23.5,使用 "conda install numpy=1.23.5"。现在一切都正常。

英文:

Had the same message/issue - Conda wouldn't update astropy to 5.3.1 using "conda update astropy" (kept giving me v5.1). Ended up doing a pip install. Even with astropy 5.3.1 it still wouldn't work with numpy 1.24.3. Had to downgrade to numpy 1.23.5. doing "conda install numpy=1.23.5". Now all is well.

huangapple
  • 本文由 发表于 2023年6月2日 05:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定