fasttext的使用,预料格式,调用方法

摘要:
数据格式:分词后的句子+__标签_+标签fasttext_模型。pyfromfasttextimportFastTextimportnumpyasnpdefget_data_path(by_word=True,train=True):ifby_word:return“./classification/data_by_word_train.txt”iftrainelse“./classifie/da

数据格式:分词后的句子+ __label__+标签

fasttext的使用,预料格式,调用方法第1张

fasttext_model.py

from fasttext import FastText
import numpy as np

def get_data_path(by_word=True,train=True):
    if by_word:
        return "./classify/data_by_word_train.txt" if train else "./classify/data_by_word_test.txt"
    else:
        return "./classify/data_train.txt" if train else "./classify/data_test.txt"

def prepar_model():
    data_path = get_data_path(by_word=True,train=True)
    model = FastText.train_supervised(data_path,dim=100,epoch=20,wordNgrams=2)
    model.save_model("./fasttext_model/classify_by_word_100_20_2.model")

def ceshi_model():
    model = FastText.load_model("./fasttext_model/classify_by_word_100_20_2.model")
    test_data_path = get_data_path(by_word=True, train=False)

    sentences = []
    labels = []
    for line in open(test_data_path,encoding="utf-8").readlines():
        line = line.strip()
        temp_ret = line.split("	")
        if len(temp_ret)==2:
            sentences.append(temp_ret[0])
            labels.append(temp_ret[1])

    ret = model.predict(sentences)[0]
    ret = [i[0] for i in ret]
    acc = np.mean([1 if labels[i] == ret[i] else 0 for i in range(len(labels))])
    print(acc)

if __name__ == '__main__':
    prepar_model()
    ceshi_model()

  

免责声明:文章转载自《fasttext的使用,预料格式,调用方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于json对象的深拷贝Python 持久化管理之 Pickle/ZODB下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

cocosCreator中Protobuf的简单使用

CocosCreatorwebStorm或VSCode安装node.js下载protobufjs6.8.6步骤:...

微信支付服务商模式支付与普通微信支付的配置区别

chapter=7_7&index=5注:与普通微信支付相比,源代码是上述7/8之间的区别,其他可以看作是服务提供商自己的微信支付配置;...

Linux 安装.src.rpm源码包的方法

接下来是rpm安装过程。...

Linux cat查看文件,查找关键字(grep),统计(wc -l)

######cat搜索关键字的语法:cat file | grep keyword | wc lcat/proc/meminfo | grepSwap | wc-l#######Linux系统中wc命令的功能是统计指定文件中的字节、单词和行数,并显示和输出统计结果。如果没有给出文件名,则从标准输入中读取。wc还提供指定文件的总统计数。此标志不能与-c标志一起...

FTPClient

(iReplyCode==331||iReplyCode==230)){CloseSocketConnect();thrownewIOException(strReply.Substring(4));}if(iReplyCode!=230){SendCommand("PASS"+strRemotePass);if(!=null){SendCommand("Q...

选包

安装系统后,将不会安装一些基本工具。此时,您可以根据yum的要求安装它们。你也可以使用任何你想要的时尚。...