1蚣抗、數(shù)據(jù)說明
corpus為一個list,每一項代表一個文本癌瘾,每個文本為分詞后的數(shù)據(jù)觅丰。
2、導(dǎo)入包
from sklearn.feature_extraction.text importTfidfVectorizer
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans
import jieba
import matplotlib.pyplot as plt
3妨退、使用TF-IDF特征進行聚類
tfidf_model = TfidfVectorizer().fit(corpus)
tfidf_matrix = tfidf_model.transform(corpus)
num_clusters = 10
km_cluster = KMeans(n_clusters=num_clusters, max_iter=300, n_init=40,
init='k-means++',n_jobs=-1)
result = km_cluster.fit_predict(tfidf_matrix)
tfidf_weight = tfidf_matrix.toarray()
4妇萄、可視化
# 使用T-SNE算法,對權(quán)重進行降維咬荷,準(zhǔn)確度比PCA算法高冠句,但是耗時長
tsne = TSNE(n_components=2)
decomposition_data = tsne.fit_transform(tfidf_weight)
x = []
y = []
for i in decomposition_data:
x.append(i[0])
y.append(i[1])
fig = plt.figure(figsize=(10, 10))
ax = plt.axes()
plt.scatter(x, y, c=km_cluster.labels_, marker="x")
plt.xticks(())
plt.yticks(())
plt.show()