Can someone explain to me why attempt #1 does not work?
有人能向我解释为什么尝试1不起作用吗?
import numpy as np
x = np.zeros(1, dtype=np.dtype([('field', '<f8', (1,2))]))
Attempt #1:
尝试# 1:
x[0]['field'] = np.array([3.,4.], dtype=np.double)
print x, '\n'
[([[ 3. 0.]])]
(why was only the'3'
copied over?)[([[3。(为什么只有'3'才被抄送过来?)
Attempt #2:
尝试# 2:
x['field'][0] = np.array([3.,4.], dtype=np.double)
print x
[([[ 3. 4.]])]
(this worked)[([[3。4。]])](工作)
2 个解决方案
#1
2
To be honest... I'm not sure I'm getting the results either. It seems inconsistent/broken. Part of it is due to inconsistent shapes but not all of it. Some data seems to be disappearing.
老实说……我也不确定我是否得到了结果。似乎不一致/坏了。部分原因是形状不一致,但不是全部。一些数据似乎正在消失。
For example (note the shapes):
例如(注意形状):
In [1]: import numpy as np
In [2]: x = np.zeros(1, dtype=np.dtype([('field', '<f8', (1, 2))]))
In [3]: y = x[0]['field'].copy()
In [4]: y[0] = 3
In [5]: y[1] = 4
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-5-cba72439f97c> in <module>()
----> 1 y[1] = 4
IndexError: index 1 is out of bounds for axis 0 with size 1
In [6]: y[0][1] = 4
In [7]: x
Out[7]:
array([([[0.0, 0.0]],)],
dtype=[('field', '<f8', (1, 2))])
In [8]: y
Out[8]: array([[ 3., 4.]])
In [9]: x[0]['field'] = y
In [10]: x
Out[10]:
array([([[3.0, 0.0]],)],
dtype=[('field', '<f8', (1, 2))])
So... to make it easier to grasp, let's make the shape simpler.
所以…为了让它更容易掌握,让我们让形状更简单。
In [1]: import numpy as np
In [2]: x = np.zeros(1, dtype=np.dtype([('field', '<f8', 2)]))
In [3]: y = x[0]['field'].copy()
In [4]: y[0] = 3
In [5]: y[1] = 4
In [6]: x[0]['field'] = y
In [7]: x
Out[7]:
array([([3.0, 0.0],)],
dtype=[('field', '<f8', (2,))])
In [8]: y
Out[8]: array([ 3., 4.])
Where the data is going in this case... not a clue. Assigning in a way that the data does get stored seems easily possible though.
在这种情况下数据的去向……不是一个线索。以数据存储的方式进行分配似乎很容易。
Several options:
几个选项:
In [9]: x['field'][0] = y
In [10]: x
Out[10]:
array([([3.0, 4.0],)],
dtype=[('field', '<f8', (2,))])
In [11]: x['field'] = y * 2
In [12]: x
Out[12]:
array([([6.0, 8.0],)],
dtype=[('field', '<f8', (2,))])
In [13]: x['field'][:] = y
In [14]: x
Out[14]:
array([([3.0, 4.0],)],
dtype=[('field', '<f8', (2,))])
In [15]: x[0]['field'][:] = y * 2
In [16]: x
Out[16]:
array([([6.0, 8.0],)],
dtype=[('field', '<f8', (2,))])
#2
2
It appears to be a recognized bug in Numpy. There is discussion there of possible fixes, but the bug is still open.
它在Numpy中似乎是一个可识别的错误。有人在讨论可能的修复,但是这个bug仍然是开放的。
#1
2
To be honest... I'm not sure I'm getting the results either. It seems inconsistent/broken. Part of it is due to inconsistent shapes but not all of it. Some data seems to be disappearing.
老实说……我也不确定我是否得到了结果。似乎不一致/坏了。部分原因是形状不一致,但不是全部。一些数据似乎正在消失。
For example (note the shapes):
例如(注意形状):
In [1]: import numpy as np
In [2]: x = np.zeros(1, dtype=np.dtype([('field', '<f8', (1, 2))]))
In [3]: y = x[0]['field'].copy()
In [4]: y[0] = 3
In [5]: y[1] = 4
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-5-cba72439f97c> in <module>()
----> 1 y[1] = 4
IndexError: index 1 is out of bounds for axis 0 with size 1
In [6]: y[0][1] = 4
In [7]: x
Out[7]:
array([([[0.0, 0.0]],)],
dtype=[('field', '<f8', (1, 2))])
In [8]: y
Out[8]: array([[ 3., 4.]])
In [9]: x[0]['field'] = y
In [10]: x
Out[10]:
array([([[3.0, 0.0]],)],
dtype=[('field', '<f8', (1, 2))])
So... to make it easier to grasp, let's make the shape simpler.
所以…为了让它更容易掌握,让我们让形状更简单。
In [1]: import numpy as np
In [2]: x = np.zeros(1, dtype=np.dtype([('field', '<f8', 2)]))
In [3]: y = x[0]['field'].copy()
In [4]: y[0] = 3
In [5]: y[1] = 4
In [6]: x[0]['field'] = y
In [7]: x
Out[7]:
array([([3.0, 0.0],)],
dtype=[('field', '<f8', (2,))])
In [8]: y
Out[8]: array([ 3., 4.])
Where the data is going in this case... not a clue. Assigning in a way that the data does get stored seems easily possible though.
在这种情况下数据的去向……不是一个线索。以数据存储的方式进行分配似乎很容易。
Several options:
几个选项:
In [9]: x['field'][0] = y
In [10]: x
Out[10]:
array([([3.0, 4.0],)],
dtype=[('field', '<f8', (2,))])
In [11]: x['field'] = y * 2
In [12]: x
Out[12]:
array([([6.0, 8.0],)],
dtype=[('field', '<f8', (2,))])
In [13]: x['field'][:] = y
In [14]: x
Out[14]:
array([([3.0, 4.0],)],
dtype=[('field', '<f8', (2,))])
In [15]: x[0]['field'][:] = y * 2
In [16]: x
Out[16]:
array([([6.0, 8.0],)],
dtype=[('field', '<f8', (2,))])
#2
2
It appears to be a recognized bug in Numpy. There is discussion there of possible fixes, but the bug is still open.
它在Numpy中似乎是一个可识别的错误。有人在讨论可能的修复,但是这个bug仍然是开放的。