問句和疑問句的區別,問句識別:基于Xgboost的中文疑問句判斷模型
問句和疑問句的區別,問句識別:基于Xgboost的中文疑問句判斷模型
一、前言
關于Xgboost:
xgboost是大規模并行boosted tree的工具,它是目前最快最好的開源boosted
tree工具包,比常見的工具包快10倍以上。在數據科學方面,有大量kaggle選手選用它進行數據挖掘比賽,其中包括兩個以上kaggle比賽的奪冠方案。在工業界規模方面,xgboost的分布式版本有廣泛的可移植性,支持在YARN,
MPI, Sungrid Engine等各個平臺上面運行,并且保留了單機并行版本的各種優化,使得它可以很好地解決于工業界規模的問題。
網上有不少介紹的文章,這里就此略過,直奔主題,寫一篇煉丹文。
二、實戰
1、語料
(1)語料格式
content,label
在么,1
你好,0
...
左邊為文本,右邊為標簽,從這里也可以看出來,疑問句判別問題可以簡單的作為一個二分類問題來解決。
(2)語料預處理
tokenizer = jieba.Tokenizer()corpus['tokens'] = corpus.content.apply(lambda x: list(tokenizer.cut(x)))
從本地讀取語料后,進行簡單的分詞處理
(3)特征工程
vectorizer = TfidfVectorizer(smooth_idf=True,analyzer=analyzer,ngram_range=(1, 1),min_df=1, norm='l1')sparse_vector = vectorizer.fit_transform(train.tokens.apply(lambda x: ' '.join(x)).tolist())
問句和疑問句的區別,這里對分詞后的語料進行TFIDF特征抽取,構造訓練矩陣。
2、模型
(1)模型參數
模型參數使用k折交叉驗證進行對比,確定
for i, param in enumerate(param_grid):cv_result = xgb.cv(param, self.train_matrix,num_boost_round=self.num_boost_round, # max iter roundnfold=self.nfold,stratified=self.stratified,metrics=self.metrics, # metrics focus onearly_stopping_rounds=self.early_stopping_rounds) # stop when metrics not get better
最終得到表現最好的一組參數及相應的迭代次數等
(2)模型訓練
_, best_param, best_iter_round = self.model_param_select()self.model = xgb.train(dtrain=self.train_matrix, params=best_param, num_boost_round=best_iter_round)
與交叉驗證尋找最佳參數不用,訓練模型時使用全部的數據進行訓練
(3)模型結果
Param select 0, auc: 0.9793438, iter_round: 207, params: {'objective': u'binary:logistic', 'subsample': 0.5, 'eta': 0.1, 'max_depth': 4, 'silent': 1}, now best auc: 0.9793438Param select 1, auc: 0.9799142, iter_round: 350, params: {'objective': u'binary:logistic', 'subsample': 0.7, 'eta': 0.1, 'max_depth': 4, 'silent': 1}, now best auc: 0.9799142Param select 2, auc: 0.9802402, iter_round: 280, params: {'objective': u'binary:logistic', 'subsample': 1.0, 'eta': 0.1, 'max_depth': 4, 'silent': 1}, now best auc: 0.9802402...Param select 24, auc: 0.97926, iter_round: 694, params: {'objective': u'binary:logistic', 'subsample': 0.5, 'eta': 0.02, 'max_depth': 6, 'silent': 1}, now best auc: 0.980495Param select 25, auc: 0.9803058, iter_round: 824, params: {'objective': u'binary:logistic', 'subsample': 0.7, 'eta': 0.02, 'max_depth': 6, 'silent': 1}, now best auc: 0.980495Param select 26, auc: 0.980129, iter_round: 880, params: {'objective': u'binary:logistic', 'subsample': 1.0, 'eta': 0.02, 'max_depth': 6, 'silent': 1}, now best auc: 0.980495
最終可以看到,模型的auc在0.980495得到最佳的參數,保存模型。
3、實例
from interrogative.api import *train()
tag = recognize(u'今天 來 點 兔子 嗎')
output = '是疑問句' if tag else '不是疑問句'
print(output)
三、其他
1、Xgboost參數解釋:https://www.cnblogs.com/mfryf/p/6293814.html
2、具體源碼可以在我的github上找到:https://github.com/lpty/nlp_base