您提到使用来自 scikit-learn 的现有 train_test_split 路由。如果这是您使用 scikit-learn 的唯一东西,那将是矫枉过正。但是,如果您已经将它用于任务的其他部分,您也可以这样做。 Astropy Tables 一开始就由 Numpy 数组支持,因此您无需“来回转换数据”。
由于表的 'ID' 列索引表中的行,因此将其正式设置为表的 index 会很有用,以便可以使用 ID 值来索引表中的行(独立它们的实际位置索引)。例如:
>>> from astropy.table import Table
>>> import numpy as np
>>> t = Table({
... 'ID': [1, 3, 5, 6, 7, 9],
... 'a': np.random.random(6),
... 'b': np.random.random(6)
... })
>>> t
<Table length=6>
ID a b
int64 float64 float64
----- ------------------- -------------------
1 0.7285295918917892 0.6180944983953155
3 0.9273855839237182 0.28085439237508925
5 0.8677312765220222 0.5996267567496841
6 0.06182255608446752 0.6604620336092745
7 0.21450048405835265 0.5351066893214822
9 0.928930682667869 0.8178640424254757
然后将'ID'设置为表的索引:
>>> t.add_index('ID')
使用train_test_split 对 ID 进行分区:
>>> train_ids, test_ids = train_test_split(t['ID'], test_size=0.2)
>>> train_ids
<Column name='ID' dtype='int64' length=4>
7
9
5
1
>>> test_ids
<Column name='ID' dtype='int64' length=2>
6
3
>>> train_set = t.loc[train_ids]
>>> test_set = t.loc[test_ids]
>>> train_set
<Table length=4>
ID a b
int64 float64 float64
----- ------------------- ------------------
7 0.21450048405835265 0.5351066893214822
9 0.928930682667869 0.8178640424254757
5 0.8677312765220222 0.5996267567496841
1 0.7285295918917892 0.6180944983953155
>>> test_set
<Table length=2>
ID a b
int64 float64 float64
----- ------------------- -------------------
6 0.06182255608446752 0.6604620336092745
3 0.9273855839237182 0.28085439237508925
(注:
>>> isinstance(t['ID'], np.ndarray)
True
>>> type(t['ID']).__mro__
(astropy.table.column.Column,
astropy.table.column.BaseColumn,
astropy.table._column_mixins._ColumnGetitemShim,
numpy.ndarray,
object)
)
对于它的价值,因为它可能会帮助您在将来更轻松地找到此类问题的答案,这将有助于更抽象地考虑您正在尝试做的事情(似乎您已经 em> 这样做,但你的问题的措辞暗示了其他):你表中的列只是 Numpy 数组——一旦它是这种形式,它们是从 FITS 文件中读取的无关紧要的。你所做的也与 Astropy 没有直接关系。问题就变成了如何随机划分一个 Numpy 数组。
您可以找到此问题的通用答案,例如in this question。但如果你有它,使用现有的专用实用程序(如 train_test_split)也很好。