我拆分 numpy.ndarray 对象:
array([[1, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]])
通过测试和训练样本(与sklearn.model_selection.train_test_split
)。之后我需要确定,测试样本的哪些元素与父样本的元素相对应。enter code here
这该怎么做?
您可以将 np.array 转换为数据框:
In [13]: data
array([[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
In [14]: data = pd.DataFrame(data)
然后拆分数据和相关索引以测试和训练部分:
In [15]: train, test, train_index, test_index = train_test_split(data, data.index)
In [16]: train
Out[16]:
0 1 2 3 4 5
0 1 0 0 0 0 0
1 1 0 0 0 0 0
In [17]: test
Out[17]:
0 1 2 3 4 5
2 0 0 0 0 0 0
In [18]: train_index
Out[18]: Int64Index([0, 1], dtype='int64')
In [19]: test_index
Out[19]: Int64Index([2], dtype='int64')
然后,您可以访问原始数据框中的训练和测试行,如下所示:
In [24]: data.loc[test_index, :]
Out[24]:
0 1 2 3 4 5
2 0 0 0 0 0 0
In [25]: data.loc[train_index, :]
Out[25]:
0 1 2 3 4 5
0 1 0 0 0 0 0
1 1 0 0 0 0 0
注意:我想这也是@cᴏʟᴅsᴘᴇᴇᴅ 在评论中提出的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句