用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

最低0.47元/天 解锁文章
810

被折叠的 条评论
为什么被折叠?



