簡體   English   中英

sklearn管道如何使用不平衡庫?

[英]How to use the imbalanced library with sklearn pipeline?

我正在嘗試解決文本分類問題。 我想使用MultinomialNB創建基線模型

我的數據在少數類別中是高度不平衡的,因此決定將不平衡庫與sklearn管道一起使用,並參考本教程

按照文檔中的建議在管道中引入了兩個階段之后,該模型失敗並給出了錯誤。

from imblearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from imblearn.under_sampling import (EditedNearestNeighbours,
                                     RepeatedEditedNearestNeighbours)
# Create the samplers
enn = EditedNearestNeighbours()
renn = RepeatedEditedNearestNeighbours()

pipe = make_pipeline_imb([('vect', CountVectorizer(max_features=100000,\
                                         ngram_range= (1, 2),tokenizer=tokenize_and_stem)),\
                         ('tfidf', TfidfTransformer(use_idf= True)),\
                          ('enn', EditedNearestNeighbours()),\
                          ('renn', RepeatedEditedNearestNeighbours()),\
                          ('clf-gnb',  MultinomialNB()),])

錯誤:

TypeError: Last step of Pipeline should implement fit. '[('vect', CountVectorizer(analyzer='word', binary=False, decode_error='strict',

有人可以幫忙嗎? 我也願意使用(Boosting / SMOTE)實現的不同方式嗎?

似乎來自“ mblearn”的管道不支持像sklearn中的命名一樣。 imblearn文檔中

* steps:估算器列表。

您應該將代碼修改為:

pipe = make_pipeline_imb( CountVectorizer(max_features=100000,\
                                         ngram_range= (1, 2),tokenizer=tokenize_and_stem),\
                         TfidfTransformer(use_idf= True),\
                         EditedNearestNeighbours(),\
                         RepeatedEditedNearestNeighbours(),\
                         MultinomialNB())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM