我很难将使用CSV从加载的结构化数组np.genfromtxt
转换为np.array
,以使数据适合Scikit-Learn估算器。问题在于,在某些时候会发生从结构化数组到常规数组的转换,从而导致ValueError: can't cast from structure to non-structure
。很长时间以来,我一直.view
在执行转换,但是这导致NumPy发出了许多弃用警告。代码如下:
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
data = np.genfromtxt(path, dtype=float, delimiter=',', names=True)
target = "occupancy"
features = [
"temperature", "relative_humidity", "light", "C02", "humidity"
]
# Doesn't work directly
X = data[features]
y = data[target].astype(int)
clf = GradientBoostingClassifier(random_state=42)
clf.fit(X, y)
引发的异常是: ValueError: Can't cast from structure to non-structure, except if the structure only has a single field.
我的第二次尝试是使用如下视图:
# View is raising deprecation warnings
X = data[features]
X = X.view((float, len(X.dtype.names)))
y = data[target].astype(int)
哪个有效,并且确实执行我想要的操作(我不需要数据副本),但是会导致弃用警告:
FutureWarning: Numpy has detected that you may be viewing or writing to
an array returned by selecting multiple fields in a structured array.
This code may break in numpy 1.15 because this will return a view
instead of a copy -- see release notes for details.
目前,我们正在使用tolist()
将结构化数组转换为列表,然后转换为np.array
。这行得通,但是似乎效率很低:
# Current method (efficient?)
X = np.array(data[features].tolist())
y = data[target].astype(int)
必须有更好的方法,我会很感激任何建议。
注意:此示例的数据来自“ UCI ML占用信息库”,数据显示如下:
array([(nan, 23.18, 27.272 , 426. , 721.25, 0.00479299, 1.),
(nan, 23.15, 27.2675, 429.5 , 714. , 0.00478344, 1.),
(nan, 23.15, 27.245 , 426. , 713.5 , 0.00477946, 1.), ...,
(nan, 20.89, 27.745 , 423.5 , 1521.5 , 0.00423682, 1.),
(nan, 20.89, 28.0225, 418.75, 1632. , 0.00427949, 1.),
(nan, 21. , 28.1 , 409. , 1864. , 0.00432073, 1.)],
dtype=[('datetime', '<f8'), ('temperature', '<f8'), ('relative_humidity', '<f8'),
('light', '<f8'), ('C02', '<f8'), ('humidity', '<f8'), ('occupancy', '<f8')])
添加.copy()
到data[features]
:
X = data[features].copy()
X = X.view((float, len(X.dtype.names)))
而FutureWarning
消息已不存在。
这应该比先转换为列表更有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句