1

多項単純ベイズを使用してテキスト分類モデルを作成しようとしています。私のデータには 10 種類のカテゴリがあります。モデルのトレーニング中に、これらのカテゴリを整数形式で表しました。

topics = ["gis","security","photo","mathematica","unix","wordpress","scifi","electronics","android","apple"]
topic2label = {topics[i]:i for i in range(len(topics))}

トレーニング データ形式:

{"topic":"electronics","question":"What is the effective differencial effective of this circuit","excerpt":"I'm trying to work out, in general terms, the effective capacitance of this circuit .  \n\nWhat is the effective capacitance of this circuit and will the ...\r\n        "}
{"topic":"electronics","question":"Heat sensor with fan cooling","excerpt":"Can I know which component senses heat or acts as heat sensor in the following circuit?\nIn the given diagram, it is said that the 4148 diode acts as the sensor. But basically it is a zener diode and ...\r\n        "}

これは私のコードスニペットがどのように見えるかです:

# ---------------------------------------- Training -------------------------------------
import sklearn
with open('training.json') as f:
    next(f)
        for line in f:
            data = json.loads(line)
            topic.append(data["topic"])
            que = data["question"]
            question.append(data["question"])
            excer = data["excerpt"]
            excerpt.append(data["excerpt"])
            combo.append(que +" "+ excer)

unique_topics = list(set(topic))
numeric_topics = [name.replace('gis', '1').replace('security', '2').replace('photo', '3').replace('mathematica', '4').replace('unix', '5').replace('wordpress', '6').replace('scifi', '7').replace('electronics', '8').replace('android', '9').replace('apple', '10') for name in new_topic]
x1 = np.array(question)
x2 = np.array(excerpt)
x3 = np.array(combo)
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5, ngram_range=(1,2),stop_words="english") 
X = vectorizer.fit_transform(x3)
Y = np.array(new_topic)
clf = MultinomialNB(alpha=0.1).fit(X, Y)

# ----------------------------   Prediction -----------------------------------------

docs_new = []

input = int(raw_input())
for i in xrange(input):
    input_data = raw_input()
    data = json.loads(input_data)
    que = data["question"]
    excer = data["excerpt"]
    docs_new.append(que +" "+ excer)

X_new_counts = vectorizer.transform(docs_new)
predicted = clf.predict(X_new_counts)
predicted =  list(predicted)
for i in predicted:
    print i

今、奇妙な動作を分析しましたが、カテゴリの整数表現を使用している間、モデルの精度は 82% で、文字列表現を使用している場合、精度は 90% に急上昇しました。

私の質問は、2 番目の状況でモデルが異なる (より良い) 動作をするのはなぜですか?

PS私はsklearnライブラリを使用しています。

4

0 に答える 0