NLP文本情感分析:测试集、验证集、训练集的划分(具体实现步骤)

一、前言:

最近在做中文语料三分类模型,在调参这边碰到了问题,一直存在过拟合。为了找到过拟合原因,将训练状况可视化,所以对数据集重新进行了划分。

二、如何实现

1.使用sklearn划分训练集合测试集

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(combined, y, test_size=0.2)

其中combined,y是我的全部数据集,comnined是中文分词合集,y是分类集。通过y来记录combined的分类。(这一部分与要说的无关,只要知道combined合y是我要分的语料合集就可以了)。
参数test_size=0.2表示划分训练集占0.8,测试集占0.2.

2.使用模型训练里的 validation_split参数对训练集和验证集进行划分

 我用的keras框架里的Sequential模型。
 
 训练代码如下:
    history = model.fit(x_train, y_train, batch_size=batch_size ,epochs=n_epoch,validation_split=0.2)
     
    #绘图
    pyplot.plot(history.history['loss'])
    pyplot.plot(history.history['val_loss'])
    pyplot.title('model train loss vs val_loss')
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['loss', 'val_loss'], loc='upper right')
    pyplot.show()

validation_split表示划分验证集的比重为0.2,训练集为0.8.

使用matplotlib绘图需要导包:

import matplotlib
from matplotlib import pyplot  

NLP文本情感分析:测试集、验证集、训练集的划分(具体实现步骤)_第1张图片
出现的图是这样,我的图看起来有问题!!!
调参去了…

你可能感兴趣的:(NLP)