簡體   English   中英

SkLearn model 用於文本分類

[英]SkLearn model for text classification

我有一個分類器多類,使用LinearSVC庫提供的 LinearSVC model 進行訓練。 這個 model 提供了一個decision_function函數方法,我將它與 numpy 庫函數一起使用以正確解釋結果集。

但是,我不明白為什么這種方法總是試圖將總概率(在我的例子中為 1)分配到每個可能的類之間。

我期望我的分類器有不同的行為。

我的意思是,例如,假設我有這樣一小段文字:

"There are a lot of types of virus and bacterias that cause disease."

但是我的分類器是用三種類型的文本訓練的,比如說“數學”、“歷史”和“技術”。

因此,我認為當我嘗試對其進行分類時,這三個主題中的每一個的概率都非常接近於零(因此遠不等於 1)。

是否有更合適的方法或 model 來獲得我剛才描述的結果?

我是否使用了錯誤的方式decision_function

有時,您的文本可能與用於訓練分類器的任何主題無關,反之亦然,對於多個主題,概率可能約為 1。

我想我需要對這些問題(文本分類、非二進制分類等)有所了解

非常感謝您的幫助!

您的問題有多個部分,我將盡我所能回答。

  1. 我不明白為什么這種方法總是試圖分配概率的總數?

That is the nature of most of the ML models out there, a given example has to be put into some class, and every model has some mechanism to compute the probability that a given data point belongs to a class and whichever class has the highest probability您將預測相應的 class。

為了解決您的問題,即示例的存在不屬於任何類,您始終可以在訓練 model 時創建一個名為others的偽類,即使您的數據點不對應於您的任何實際課程,例如mathshistorytechnology ,根據您的示例,它將被分箱到other class。

  1. 解決您的數據點可能屬於多個類的問題。

這就是多標簽分類的典型用途。

希望這可以幫助!

您正在尋找的是多標簽分類 model。 參考這里了解理解多標簽分類和支持多標簽分類任務的模型列表。

演示多標簽分類的簡單示例:

from sklearn.datasets import fetch_20newsgroups

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.preprocessing import OneHotEncoder
categories = ['sci.electronics', 'sci.space', 'talk.religion.misc',]
newsgroups_train = fetch_20newsgroups(subset='all',
                                      remove=('headers', 'footers', 'quotes'),
                                      categories=categories)

from sklearn.multioutput import MultiOutputClassifier
from sklearn.pipeline import make_pipeline

X, y = newsgroups_train.data, OneHotEncoder(sparse=False)\
    .fit_transform([[newsgroups_train.target_names[i]]
                      for i in newsgroups_train.target])

model = make_pipeline(TfidfVectorizer(stop_words='english'),
                      MultiOutputClassifier(LinearSVC()))

model.fit(X, y)

print(newsgroups_train.target_names)
# ['sci.electronics', 'sci.space', 'talk.religion.misc']


print(model.predict(['religion followers of jesus']))
# [[0. 0. 1.]]


print(model.predict(['Upper Atmosphere Satellite Research ']))
# [[0. 1. 0.]]


print(model.predict(['There are a lot of types of virus and bacterias that cause disease.']))
# [[0. 0. 0.]]

處理此問題的常用方法是嘗試將文本樣本投射到某種向量空間中,並測量該向量空間與同一向量空間中代表分類的某些原型位置之間的“距離”。

這個分類器的 model 很方便,因為如果您將文本樣本折疊成詞匯頻率的向量,它幾乎可以簡單地表示為向量 - 其中維度由您選擇跟蹤的詞匯特征的數量定義。

通過對更廣泛的文本語料庫進行聚類分析,您可以嘗試確定通常出現在聚類中的中心點,並且可以根據它們所在的向量位置來描述它們。

最后,在定義了一些聚類中心后,您可以簡單地用畢達哥拉斯方法找到您選擇的樣本最接近這些主題聚類中的哪一個——但您也可以輕松獲得樣本與所有主題聚類之間的相對距離。其他集群中心也是如此 - 所以它的概率較小,更多的是空間度量。

暫無
暫無

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

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