数据部分及预处理

该博客介绍了数据预处理的步骤,包括编写脚本复制文件、划分测试集、训练集和验证集,以及利用7个函数进行预处理。重点提到了`data/cnews_loader.py`文件,其中包含读取文件、构建词汇表、转换为ID表示、读取类别、还原文字、固定长度处理和批次数据生成等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写脚本文件将每个分类拷贝6500个文件

MAXCOUNT=6500



for category in $( ls THUCNews); do

  echo item: $category



  dir=THUCNews/$category

  newdir=data/thucnews/$category

  if [ -d $newdir ]; then

    rm -rf $newdir

    mkdir $newdir

  fi



  COUNTER=1

  for i in $(ls $dir); do

    cp $dir/$i $newdir

    if [ $COUNTER -ge $MAXCOUNT ]

    then

      echo finished

      break

    fi

    let COUNTER=COUNTER+1

  done



done




2.把数据分成测试集,训练集,验证集

import os



def _read_file(filename):

    """读取一个文件并转换为一行"""

    with open(filename, 'r', encoding='utf-8') as f:

        return f.read().replace('\n', '').replace('\t', '').replace('\u3000', '')



def save_file(dirname):

    """

    将多个文件整合并存到3个文件中

    dirname: 原数据目录

    文件内容格式:  类别\t内容

    """

    f_train = open('data/cnews/cnews.train.txt', 'w', encoding='utf-8')

    f_test = open('data/cnews/cnews.test.txt', 'w', encoding='utf-8')

    f_val = open('data/cnews/cnews.val.txt', 'w', encoding='utf-8')

    for category in os.listdir(dirname):   # 分类目录

        cat_dir = os.path.join(dirname, category)

        if not os.path.isdir(cat_dir
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值