我有一个 numpy 结构化数组,我需要在其中控制各个元素的数据类型。
这是我的例子:
y = array([(True, 144.0),
(True, 86.0),
(True, 448.0),
(True, 76.0),
(True, 511.0)], dtype=object)
如果我做:
print(y.dtype.fields)
我回来了:
None
但是,我想要的是“bool”和“float”。如果我访问单个元素,例如y[0][0]
andy[0][1]
我肯定会看到它们确实是 bool 和 float。
我对此非常困惑。有任何想法吗?
我需要这个,因为我使用包“sciki-survival 梯度提升”:https ://scikit-survival.readthedocs.io/en/latest/generated/sksurv.ensemble.GradientBoostingSurvivalAnalysis.html#sksurv.ensemble.GradientBoostingSurvivalAnalysis.fit Where输入需要是“bool”和“float”类型的结构化数组。
初始化结构化数组时,请确保指定数据类型。
例如:
y = np.array([(True, 144.0), (True, 86.0), (True, 448.0)],
dtype=[('col_1', 'bool'), ('col_2', 'f4')])
这应该有效并且:
y.dtype.fields
根据需要显示:
mappingproxy({'col_1': (dtype('bool'), 0), 'col_2': (dtype('float32'), 1)})
请参阅此处的文档:https : //docs.scipy.org/doc/numpy/user/basics.rec.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句