SciKit 站点提供了这个 k-means 演示,我想尽可能多地使用它来聚集我自己的一些文档,因为我对机器学习和 SciKit 都是新手。问题在于以适合他们演示的形式获取我的文档。
这是 SciKit 示例中的“问题区域”:
dataset = fetch_20newsgroups(subset='all', categories=categories, shuffle=True, random_state=42)
labels = dataset.target
true_k = np.unique(labels).shape[0]
可以看出,在示例中,作者使用/“获取”名为“20newsgroups”的数据集,该调用(根据此页面;参见 7.7 的第二段)“返回原始文本文件列表可以提供给文本特征提取器。” 我不依赖“文本文件”列表——如下面的代码所示——但我可以以任何必要的形式放置我的“文档”。
如何使用 SciKit 示例而不必将我的“文档”放在文本文件中?还是仅从文本文件而不是文档所在的数据库中对文档进行聚类是标准做法?从演示/文档中根本不清楚示例中的哪些内容是完全多余的,使用它是因为它使作者的生活更轻松,哪些不是。或者至少我不清楚。
if cursor.rowcount > 0: #don't bother doing anything if we don't get anything from the database
data = cursor.fetchall()
for row in data:
temp_string = row[0]+" "+row[1]+" "+row[3]+" "+row[4] # currently skipping the event_url: row[2]
page = BeautifulSoup((''.join(temp_string)))
pagetwo = str(page)
clean_text = nltk.clean_html(pagetwo)
tokens = nltk.word_tokenize(clean_text)
fin_doc = "" + "\n"
for word in tokens:
fin_word = stemmer.stem(word).lower()
if fin_word not in stopwords and len(fin_word) > 2:
fin_doc += fin_word + " "
documents.append(fin_doc)