假设我有一个包含两个键的字典,spam和ham,用于显示如下所示的spam和ham文本或电子邮件:
data = {
'spam': [
['hi', "what's", 'going', 'on', 'sexy', 'thing'],
['1-800', 'call', 'girls', 'if', "you're", 'lonely'],
['sexy', 'girls', 'for', 'youuuuuu']],
'ham': [['hey', 'hey', 'I', 'got', 'your', 'message,', "I'll", 'be', 'home', 'soon!!!'],
['Madden', 'MUT', 'time', 'boys']]}
我想把字典分成训练集和测试集(从80/20训练开始测试)。我希望分割是不考虑键的,所以只需将80%的训练集消息和20%的测试集消息子集。在这个小例子中,我们总共有5条消息(3条是垃圾邮件,2条是火腿)。我已经四处寻找解决方案,但还没有找到任何解决这种情况的方法
使用适当命名的^{} :
你可以,也可能应该使用更强大的东西,比如熊猫:
输出:
您可以将dict转换为元组列表,然后进行拆分
每个元素都是一对
(sentence, label)
相关问题 更多 >
编程相关推荐