我想了解我是否有一组 JSON 格式的字典数据,例如以下示例:
data = {'a':'120120121',
'b':'12301101',
'c':'120120121',
'd':'12301101',
'e':'120120121',
'f':'12301101',
'g':'120120121',
'h':'12301101',
'i':'120120121',
'j':'12301101'}
是否可以使用 Python 将字典随机拆分为 70:30?
输出应该是这样的:
training_data = {'a':'120120121',
'b':'12301101',
'c':'120120121',
'e':'120120121',
'g':'120120121',
'i':'120120121',
'j':'12301101'}
test_data = {'d':'12301101',
'f':'12301101',
'h':'12301101'}
最佳答案
最简单的方法就是使用 sklearn.model_selection.train_test_split
在这里,和
如果这是您想要的结构,请返回字典:
from sklearn.model_selection import train_test_split
s = pd.Series(data)
training_data , test_data = [i.to_dict() for i in train_test_split(s, train_size=0.7)]
print(training_data)
# {'b': '12301101', 'j': '12301101', 'a': '120120121', 'f': '12301101',
# 'e': '120120121', 'c': '120120121', 'h': '12301101'}
print(test_data)
# {'i': '120120121', 'd': '12301101', 'g': '120120121'}
关于python - 字典数据可以随机分成测试集和训练集吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60926460/