sk-learn: ข้อผิดพลาดของ fit() รับอาร์กิวเมนต์ตำแหน่ง 2 รายการ แต่ได้รับ 3 รายการใน FeatureUnion

ฉันใช้ sk-learn ใน python เพื่อให้พอดีกับโมเดลและแปลง input_data ผ่านโมเดล

ฉันใช้ FeatureUnion เพื่อรวม CountVectorizer และ TfidfEmbeddingVectorizer

คุณสามารถใช้เฉพาะ CountVectorizer หรือ TfidfEmbeddingVectorizer เท่านั้นก็ได้ แต่หากฉันรวมคุณสมบัติทั้งสองเข้าด้วยกันด้วย Feature Union จะมีข้อผิดพลาดดังนี้:

TypeError: fit() takes 2 positional arguments but 3 were given

คลาส TfidfEmbeddingVectorizer เป็นเช่นนี้:

class TfidfEmbeddingVectorizer(object):
   ...
    def fit(self, X):
            tfidf = TfidfVectorizer(analyzer=lambda x: x)
            tfidf.fit(X)
            # if a word was never seen - it must be at least as infrequent
            # as any of the known words - so the default idf is the max of 
            # known idf's
            max_idf = max(tfidf.idf_)
            self.word2weight = defaultdict(
                lambda: max_idf,
                [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])

            return self

และฉันใช้ FeatureUnion เช่นนี้:

model = gensim.models.Word2Vec(speech.train_data, size = 100)
w2v = dict(zip(model.wv.index2word, model.wv.syn0))

count = CountVectorizer(tokenizer=lambda doc: doc, lowercase=False)
w2v_tfidf = TfidfEmbeddingVectorizer(w2v)
feature_union = FeatureUnion([('ngram', count),
                             ('tfidf', w2v_tfidf)])
feature_union.fit(speech.train_data)

ฉันได้เห็นวิธีแก้ปัญหาที่การลดระดับเวอร์ชัน sk-learn เป็น 0.18.0 ทำให้เป็นเรื่องปกติ แต่ฉันไม่สามารถดาวน์เกรด sk-learn ด้วยข้อผิดพลาดนี้:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://andinghub.visualstudio.com/visual-cpp-build-tools

มีวิธีแก้ไขปัญหาอื่นสำหรับการใช้ฟังก์ชัน fit ของ FeatureUnion หรือไม่


person Gi Yeon Shin    schedule 08.04.2018    source แหล่งที่มา


คำตอบ (1)


เมธอด fit() ของ FeatureUnion ใช้ X และ y เป็นอินพุตตาม เอกสารประกอบ:

พอดี(X, y=ไม่มี)

Fit all transformers using X.

แม้ว่าค่าเริ่มต้นจะเป็น None แต่ยังคงส่งผ่านไปยังหม้อแปลงภายใน มีอยู่ด้วยเหตุผลด้านความเข้ากันได้เมื่อใช้ในไปป์ไลน์

ตอนนี้กำลังพูดถึงวิธีหม้อแปลงภายใน fit()

พอดี (raw_documents, y=ไม่มี)

Learn vocabulary and idf from training set.

อย่างที่คุณเห็น มันมี y ด้วยเหตุผลเดียวกัน แม้ว่าจะไม่ได้ใช้ที่ไหนเลยก็ตาม

  • TfidfEmbeddingVectorizer fit() ที่คุณกำหนดเองไม่มีพารามิเตอร์ y เพิ่มเติม

แต่การรวมคุณลักษณะจะพยายามผลัก y (โดยมีค่า None) เข้าไป และทำให้เกิดข้อผิดพลาด เพียงเปลี่ยน fit เป็น:

def fit(self, X, y=None):
    ....
    ....
person Vivek Kumar    schedule 09.04.2018