2012-09-16 35 views
28

Tôi nhận được lỗi sau khi thực hiện lựa chọn tính năng đệ quy với cross-validation:Lỗi Loại: chỉ mảng số nguyên với một phần tử có thể được chuyển đổi sang một chỉ số

Traceback (most recent call last): 
    File "/Users/.../srl/main.py", line 32, in <module> 
    argident_sys.train_classifier() 
    File "/Users/.../srl/identification.py", line 194, in train_classifier 
    feat_selector.fit(train_argcands_feats,train_argcands_target) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/feature_selection/rfe.py", line 298, in fit 
    ranking_ = rfe.fit(X[train], y[train]).ranking_ 
TypeError: only integer arrays with one element can be converted to an index 

Mã mà tạo ra các lỗi như sau :

def train_classifier(self): 

    # Get the argument candidates 
    argcands = self.get_argcands(self.reader) 

    # Extract the necessary features from the argument candidates 
    train_argcands_feats = [] 
    train_argcands_target = [] 

    for argcand in argcands: 
     train_argcands_feats.append(self.extract_features(argcand)) 
     if argcand["info"]["label"] == "NULL": 
      train_argcands_target.append("NULL") 
     else: 
      train_argcands_target.append("ARG") 

    # Transform the features to the format required by the classifier 
    self.feat_vectorizer = DictVectorizer() 
    train_argcands_feats = self.feat_vectorizer.fit_transform(train_argcands_feats) 

    # Transform the target labels to the format required by the classifier 
    self.target_names = list(set(train_argcands_target)) 
    train_argcands_target = [self.target_names.index(target) for target in train_argcands_target] 

    ## Train the appropriate supervised model  

    # Recursive Feature Elimination 
    self.classifier = LogisticRegression() 
    feat_selector = RFECV(estimator=self.classifier, step=1, cv=StratifiedKFold(train_argcands_target, 10)) 

    feat_selector.fit(train_argcands_feats,train_argcands_target) 

    print feat_selector.n_features_ 
    print feat_selector.support_ 
    print feat_selector.ranking_ 
    print feat_selector.cv_scores_ 

    return 

Tôi biết tôi cũng nên thực hiện GridSearch cho thông số của bộ phân loại LogisticRegression, nhưng tôi không nghĩ đó là nguồn gốc của lỗi (hoặc là nó?).

Tôi nên đề cập rằng tôi đang thử nghiệm với khoảng 50 tính năng và hầu như tất cả chúng đều là phân loại (đó là lý do tại sao tôi sử dụng DictVectorizer để biến đổi chúng một cách thích hợp).

Bất kỳ trợ giúp hoặc hướng dẫn nào bạn có thể cho tôi đều được hoan nghênh. Cảm ơn!

EDIT

Dưới đây là một số ví dụ dữ liệu huấn luyện:

train_argcands_feats = [{'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'dado', 'head': u'dado', 'head_postag': u'N'}, {'head_lemma': u'postura', 'head': u'postura', 'head_postag': u'N'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'querer', 'head': u'quer', 'head_postag': u'V-FIN'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'participar', 'head': u'participando', 'head_postag': u'V-GER'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'recusar', 'head': u'recusando', 'head_postag': u'V-GER'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'participar', 'head': u'participando', 'head_postag': u'V-GER'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'governo', 'head': u'Governo', 'head_postag': u'N'}, {'head_lemma': u'de', 'head': u'de', 'head_postag': u'PRP'}, {'head_lemma': u'governo', 'head': u'Governo', 'head_postag': u'N'}, {'head_lemma': u'recusar', 'head': u'recusando', 'head_postag': u'V-GER'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'querer', 'head': u'quer', 'head_postag': u'V-FIN'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'muito', 'head': u'Muitas', 'head_postag': u'PRON-DET'}, {'head_lemma': u'prioridade', 'head': u'prioridades', 'head_postag': u'N'}, {'head_lemma': u'com', 'head': u'com', 'head_postag': u'PRP'}, {'head_lemma': u'prioridade', 'head': u'prioridades', 'head_postag': u'N'}] 

train_argcands_target = ['NULL', 'ARG', 'ARG', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'ARG', 'ARG', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'ARG', 'NULL', 'NULL'] 
+0

Theo dấu vết ngăn xếp, sự cố nằm trong cuộc gọi 'feat_selector.fit (train_argcands_feats, train_argcands_target)' của bạn. 'RFECV' là lớp bạn tạo hay là thư viện? Có thể đăng mã 'RFECV.fit()' của bạn không? – acattle

+0

@acattle Đó là một thư viện có kiến ​​thức học hỏi: http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFECV.html – feralvam

+0

@acattle bạn đang thấy ở đâu? – XORcist

Trả lời

32

Cuối cùng tôi đã giải quyết vấn đề. Hai điều phải được thực hiện:

  1. train_argcands_target là một danh sách và nó có phải là một mảng NumPy. Tôi ngạc nhiên khi nó hoạt động tốt trước khi tôi chỉ sử dụng bộ ước lượng trực tiếp.
  2. Vì một lý do nào đó (tôi không biết tại sao), nó không hoạt động nếu tôi sử dụng ma trận thưa thớt được tạo bởi DictVectorizer. Tôi đã phải, "bằng tay", biến đổi mỗi từ điển tính năng thành một mảng tính năng với chỉ số nguyên đại diện cho mỗi giá trị tính năng. Quá trình chuyển đổi tương tự như quy trình tôi trình bày trong mã cho các giá trị đích.

Nhờ tất cả những người đã cố giúp đỡ!

9

Nếu ai vẫn còn quan tâm,

tôi đã sử dụng CountVectorizer trên một cái gì đó rất giống nhau và nó đã cho tôi những lỗi tương tự. Tôi nhận ra rằng vectơ cho tôi một ma trận thưa thớt COO về cơ bản là một danh sách toạ độ. Các phần tử trong ma trận COO không thể được truy cập thông qua các chỉ mục hàng. Tốt nhất là chuyển đổi nó thành một ma trận CSR (Nén hàng thưa thớt) để lập chỉ mục hàng khôn ngoan. Việc chuyển đổi có thể được thực hiện dễ dàng coo_matrix.tocsr(). Không có thay đổi khác là cần thiết, điều này làm việc cho tôi.

+1

Tôi đã có cùng một vấn đề và giải pháp này làm việc cho tôi. Nó dễ hơn là lấy ra câu trả lời được chấp nhận ở đây. Ngoài ra, lý do được cung cấp (COO không thể tiếp cận được qua các chỉ mục hàng) có ý nghĩa hơn nhiều. – Dexter

+0

Hoàn toàn đồng ý. COO là vấn đề. –

Các vấn đề liên quan