如果字段包含floats
,我将尝试规范化结构化数组的不同字段中包含的所有数据。但是,即使我一个接一个地遍历每个字段,我仍会收到警告。
for idt, dt in enumerate(data.dtype.names):
if "float32" in data.dtype[idt].name:
stds = np.std(data[dt])
means = np.mean(data[dt])
data[dt] = (data[dt] - means) / stds
执行完最后一行后,将弹出:
FutureWarning:Numpy已检测到您(可能)正在写入返回的数组
通过numpy.diagonal或通过选择结构化中的多个字段
数组。此代码可能会在将来的numpy版本中中断-
有关详细信息,请参见numpy.diagonal或arrays.indexing参考文档。
快速解决方案是制作一个明确的副本(例如
arr.diagonal()。copy()或arr [[''f0','f1']]。copy())。 data [dt] =(data [dt]-平均值)/ stds
我可以在调试器中逐行运行它,以确保一切都按预期进行,例如:
In[]: data.dtype
Out[]: dtype([('a', '<f4'), ('b', '<f4'), ('c', '<f4'), ('d', '<i4')])
In[]: dt
Out[]: 'a'
In[]: data[dt].shape
Out[]: (2000, 8)
遵循警告消息中的建议,复制数组即可:
data2 = data.copy()
for idt, dt in enumerate(data2.dtype.names):
if "float32" in data2.dtype[idt].name:
stds = np.std(data2[dt])
means = np.mean(data2[dt])
data2[dt] = (data2[dt] - means) / stds
data = data2
有什么更优雅的方式摆脱警告?在这种情况下,副本发生了什么变化?
最佳答案
def foo(data):
for idt, dt in enumerate(data.dtype.names):
if "float32" in data.dtype[idt].name:
data[dt] = data[dt] + idt
In [23]: dt = np.dtype([('a', '<f4'), ('b', '<f4'), ('c', '<f4'), ('d', '<i4')])
In [24]: data = np.ones((3,), dtype=dt)
In [25]: foo(data)
In [26]: data
Out[26]:
array([( 1., 2., 3., 1), ( 1., 2., 3., 1), ( 1., 2., 3., 1)],
dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4'), ('d', '<i4')])
这在没有警告的情况下起作用。但是,如果尝试使用数据的多字段选择,则会收到警告:
In [27]: data1 = data[['a','d']]
In [28]: foo(data1)
/usr/local/bin/ipython3:4: FutureWarning: Numpy has detected that you (may be) writing to an array returned
by numpy.diagonal or by selecting multiple fields in a structured
array. This code will likely break in a future numpy release --
see numpy.diagonal or arrays.indexing reference docs for details.
The quick fix is to make an explicit copy (e.g., do
arr.diagonal().copy() or arr[['f0','f1']].copy()).
import re
可以在副本上进行操作:
In [38]: data1 = data[['d','a']].copy()
In [39]: foo(data1)
In [40]: data1
Out[40]:
array([(1, 2.), (1, 2.), (1, 2.)],
dtype=[('d', '<i4'), ('a', '<f4')])
(接下来,我将尝试使用
h5py
保存和检索此数组,看看是否有区别。)使用
h5py
,d1 = f['data']
foo(d1) # operate directly on the dataset
data1 = d1[:]; foo(data1) # operate on a copy
data1 = d1[:,'a','b'] # also a copy
我无法使用
h5py
数据集重现警告。也可以禁止警告。但是首先,您需要清楚地了解警告的含义和任何后果。