用fasttext 训练

下载fasttext

官网: https://github.com/facebookresearch/fastText

编译 和安装

wget https://github.com/facebookresearch/fastText/archiv/v0.9.1.zip
unzip v0.9.1.zip
cd fastText-0.9.1
 make
 pip install .

使用fasttext

格式:

__label__Sociology , 年轻 男子 疑因 感情 受挫 旅馆 内 跳楼 身亡 新快报
__label__Technology , 18 倍 光变 27mm 广角 尼康 P80 套装 2800 元 作

喂给 fasttext 的文档必须是这样的格式: 前缀+标签 + 逗号 + 分词

__label__  这个是前缀
Sociology: 这个是标签
年轻 男子 疑因 感情   :这是中文分词 中间用空格隔开

变换数据

1,要将数据集 分成 训练集 和 测试集 第一步 将数据转换成 csv

方便后面的切割操作

2,将CSV数据 切割成 train.txt 和 test.txt

import fasttext

import re
from types import MethodType, FunctionType

import jieba

from collections import defaultdict

import  pandas  as pd

import  random
import  os

class TransformData(object):

    #将文档转换成 CSV
    def to_csv(self, inPath, outPath, index=False):
        dd = {
   
   }
        handler = open(inPath)


        for line in handler:
            label, content = line.split(',', 1)
            key =label.strip('__label__').strip()
            if not  dd.get(key,False):
                dd[key] =[]
            dd[key].append(content.strip())
        handler.close()

        df = pd.DataFrame()
        for key in dd.keys():
            col = pd.Series(dd[key], name=key)
            df = pd.concat([df, col], axis=1)
        return df.to_csv(outPath, index=index, encoding='utf-8')
    #切割数据集 成 train.txt  test.txt
    def  SplitTrainTest(s
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值