如何指定列numpy的分割数据集?
现在,我试图分裂数据集我有如下的格式,这是dataitems的,
{
"tweet_id": "1234456",
"tweet": "hello world",
"labels": {
"item1": 2,
"item2": 1
}
},
{
"tweet_id": "567890976",
"tweet": "testing",
"labels": {
"item1": 2,
"item2": 1,
"item3": 1,
"item4": 1
}
}
此刻的可行的方法越来越只是在一个列表和分裂的tweet_ids,但我想知道是否有方法直接分割使用numpy.split此JSON文件()
TRAINPCT = 0.50
DEVPCT = 0.25
TESTPCT = 1 - TRAINPCT - DEVPCT
train, dev, test = np.split(dataitems, [int(TRAINPCT * len(dataitems)), int((TRAINPCT+DEVPCT) * len(dataitems))])
这只是抛出和错误
OrderedDict([('tweet_id', '1234456'), ('tweet', "hello world""), ('labels', Counter({'item1': 2, 'item2': 1}))])],
dtype=object) is not JSON serializable
谢谢
pandas
提供功能把JSON数据成DataFrame
对象,它的基本工作原理等的表。可能是值得考虑这个,而不是使用numpy
:
In [1]: from pandas.io.json import json_normalize
...:
...: raw = [{"tweet_id": "1234456",
...: "tweet": "hello world",
...: "labels": {
...: "item1": 2,
...: "item2": 1
...: }},
...: {"tweet_id": "567890976",
...: "tweet": "testing",
...: "labels": {
...: "item1": 2,
...: "item2": 1,
...: "item3": 1,
...: "item4": 1
...: }
...: }]
...:
...: df = json_normalize(raw)
In [2]: df
Out[2]:
labels.item1 labels.item2 labels.item3 labels.item4 tweet \
0 2 1 NaN NaN hello world
1 2 1 1.0 1.0 testing
tweet_id
0 1234456
1 567890976
想通了,我无法在相同的数据帧都做到这一点的想法。我所做的到底是只提取tweet_id
s到一个数据帧 - >割裂开来,然后从取决于tweet_id
的分裂初始数据集相匹配的标签。