簡體   English   中英

如何使用 Python 或 R 對大型文本語料庫(例如職位列表)進行聚類?

[英]How to cluster a large text corpus (e.g. list of job titles) using Python or R?

我有一個文本語料庫 - 從網絡中提取的工作圖塊列表。 該列表非常干凈,並存儲為一列 CSV 文件,其中標題按行列出。

我嘗試過使用 TF-IDF 和 Affinity Propagation 的方法,但這會遇到內存問題。 我嘗試使用word2vec然后應用聚類算法來做到這一點,但它沒有顯示出不錯的結果。 對大約 75,000 個職位的數據集進行聚類的最有效方法是什么?

您可以使用特征化字級的嵌入,如標題gensim.models.word2vec然后用sklearn.cluster.DBSCAN 如果沒有看到數據集,很難給出更具體的建議。

備選方案之一可以是主題建模,例如潛在狄利克雷分配(LDA)模型。

最小的R示例如下所示:

library(topicmodels)
library(tidytext)
library(data.table)
library(tm)

# Reading Craigslist job titles
jobs <- fread('https://raw.githubusercontent.com/h2oai/app-ask-craig/master/workflow/data/craigslistJobTitles.csv')
jobs[, doc_id := 1:.N]

# Building a text corpus
dtm <- DocumentTermMatrix(Corpus(DataframeSource(jobs[, .(doc_id, text = jobtitle)])),
                          control = list(removePunctuation = TRUE,
                                         removeNumbers = TRUE,
                                         stopwords = TRUE,
                                         stemming = TRUE,
                                         wordLengths = c(1, Inf)))

# Let's set number of topics to be equal to number of categories and fit LDA model
n_topics <- length(unique(jobs[, category]))
lda <- LDA(dtm, k = n_topics, method = 'Gibbs', control = list(seed = 1234, iter = 1e4))

# Kind of confusion matrix to inspect relevance
docs <- setDT(tidy(lda, matrix = 'gamma'))[, document := as.numeric(document)]
docs <- docs[, .(topic = paste0('topic_', .SD[gamma == max(gamma)]$topic)), by = .(doc_id = document)]
dcast(merge(jobs, docs)[, .N, by = .(category, topic)], category ~ topic, value.var = 'N')

關於 Craigslist 數據集的好消息是它有每個職位的標簽(類別),因此您可以構建類似這樣的混淆矩陣:

          category topic_1 topic_2 topic_3 topic_4 topic_5 topic_6
1:      accounting     357     113    1091     194     248     241
2:  administrative     595     216    1550     260     372     526
3: customerservice    1142     458     331     329     320     567
4:       education     296     263     251     280    1638     578
5:    foodbeverage     325     369     287    1578     209     431
6:           labor     546    1098     276     324     332     853

當然,LDA 是無監督的,估計的主題不應該與原始類別匹配,但是我們觀察到了例如labor類別和topic_2之間的語義交集。

首先,您需要使用 tfidf 或 word2vec 等對文本進行矢量化。請參閱下面的 tfidf 實現:我正在跳過預處理部分,因為它會因問題陳述而異。

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import DBSCAN

df = pd.read_csv('text.csv')
text = df.text.values
tfidf = TfidfVectorizer(stop_words='english')
vec_fit = tfidf.fit(text)
features = vec_fit.transform(text)
# now comes the clustering part, you can use KMeans, DBSCAN at your will
model = DBSCAN().fit(features)  # this might take ages as per size of the text and does not require to provide no. of clusters!!!
unseen_features = vec_fit.transform(unseen_text)
y_pred = model.predict(unseen_features)

sklean 文檔中有用於聚類的評估技術: https ://scikit-learn.org/stable/modules/clustering.html#clustering-performance-evaluation

暫無
暫無

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

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