3

NLPタスクを実行するために、scikit-learnパッケージに実装されているさまざまな分類子を試しています。分類を実行するために使用するコードは次のとおりです

def train_classifier(self, argcands):
        # 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))
            train_argcands_target.append(argcand["info"]["label"]) 

        # 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
        self.classifier = LinearSVC()
        #self.classifier = SVC(kernel="poly", degree=2)

        self.classifier.fit(train_argcands_feats,train_argcands_target)

        return

def execute(self, argcands_test):
        # Extract features
        test_argcands_feats = [self.extract_features(argcand) for argcand in argcands_test]

        # Transform the features to the format required by the classifier
        test_argcands_feats = self.feat_vectorizer.transform(test_argcands_feats)

        # Classify the candidate arguments 
        test_argcands_targets = self.classifier.predict(test_argcands_feats)

        # Get the correct label names
        test_argcands_labels = [self.target_names[int(label_index)] for label_index in test_argcands_targets]

        return zip(argcands_test, test_argcands_labels)

コードからわかるように、私はサポートベクターマシン分類器の2つの実装をテストしています。LinearSVCと多項式カーネルを使用したSVCです。さて、私の「問題」について。LinearSVCを使用すると、問題なく分類できます。テストインスタンスはいくつかのラベルでタグ付けされています。ただし、多項式SVCを使用する場合、すべてのテストインスタンスは同じラベルでタグ付けされます。考えられる理由の1つは、単純に、多項式SVCは私のタスクに使用する適切な分類子ではないということですが、それは問題ありません。多項式SVCを適切に使用していることを確認したいだけです。

あなたが私に与えることができるすべての助け/アドバイスをありがとう。

更新 回答に示されている推奨事項に従って、分類子をトレーニングするコードを変更して、次のことを実行します。

# Train the appropriate supervised model
parameters = [{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['poly'], 'degree': [2]}]
self.classifier = GridSearchCV(SVC(C=1), parameters, score_func = f1_score)

今、私は次のメッセージを受け取ります:

ValueError: The least populated class in y has only 1 members, which is too few. The minimum number of labels for any class cannot be less than k=3.

これは、トレーニングデータ内のクラスのインスタンスの不均一な分布と関係がありますよね?または、プロシージャを誤って呼び出していますか?

4

1 に答える 1

4

どちらの場合も、グリッド検索を使用して正則化パラメーターCの値を調整する必要があります。それ以外の場合は、結果を比較することはできません。一方のCの値が適切であると、もう一方のモデルの結果が不安定になる可能性があるためです。

多項式カーネルの場合、次数の最適値(たとえば、2または3以上)をグリッド検索することもできます。その場合、Cと次数の両方を同時にグリッド検索する必要があります。

編集

これは、トレーニングデータ内のクラスのインスタンスの不均一な分布と関係がありますよね?または、プロシージャを誤って呼び出していますか?

StratifiedKFold相互検証を実行できるように、クラスごとに少なくとも3つのサンプルがあることを確認してください(これは、分類にk == 3使用されるデフォルトのCVだと思います)。GridSearchCV少ない場合は、モデルが有用なものを予測できると期待しないでください。クラスごとに少なくとも100個のサンプルをお勧めします(10未満の機能とクラス間の決定境界に多くの規則性があるトイプロブレムに取り組む場合を除いて、ある程度恣意的な経験則として)。

ところで、質問/バグレポートには常に完全なトレースバックを貼り付けてください。そうしないと、正しい原因を診断するために必要な情報がない可能性があります。

于 2012-08-28T16:25:57.757 に答える