使用特征哈希进行聚类

2024-09-28 03:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须对一些json格式的文档进行集群。我想修改特性哈希来减少维度。首先,我的意见是:

doc_a = { "category": "election, law, politics, civil, government",
          "expertise": "political science, civics, republican"
        }

doc_b = { "category": "Computers, optimization",
          "expertise": "computer science, graphs, optimization"
        }
doc_c = { "category": "Election, voting",
          "expertise": "political science, republican"
        }
doc_d = { "category": "Engineering, Software, computers",
          "expertise": "computers, programming, optimization"
        }
doc_e = { "category": "International trade, politics",
          "expertise": "civics, political activist"
        }

如何创建相似性,然后使用散列来创建每个文档的相似性?读了http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html我有点迷路了。 我不确定是否必须使用“dict”或将数据转换为具有一些整数,然后将“pair”用于featureHasher的“input_type”。我应该如何解释featureHasher的输出?例如,示例http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html输出一个numpy数组。在

^{pr2}$

我认为行是文档,列值是。。?比方说,如果我想聚类或查找这些向量之间的相似性(使用余弦或Jaccard),不确定是否必须进行项目比较?在

预期输出:doc_a、doc_cc和doc_e应该在一个集群中,其余的在另一个集群中。在

谢谢!在


Tags: 文档httpdoc集群scikit相似性sciencepolitical
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:24

如果您使用HashingVectorizer而不是FeatureHasher来解决这个问题,您会使事情变得更简单。HashingVectorizer负责将输入数据标记化,并可以接受字符串列表。在

这个问题的主要挑战是您实际上有两种文本特征,category和{}。这种情况下的诀窍是为这两个特性安装一个哈希向量器,然后组合输出:

from sklearn.feature_extraction.text import HashingVectorizer
from scipy.sparse import hstack
from sklearn.cluster import KMeans

docs = [doc_a,doc_b, doc_c, doc_d, doc_e]

# vectorize both fields separately
category_vectorizer = HashingVectorizer()
Xc = category_vectorizer.fit_transform([doc["category"] for doc in docs])

expertise_vectorizer = HashingVectorizer()
Xe = expertise_vectorizer.fit_transform([doc["expertise"] for doc in docs])

# combine the features into a single data set
X = hstack((Xc,Xe))
print("X: %d x %d" % X.shape)
print("Xc: %d x %d" % Xc.shape)
print("Xe: %d x %d" % Xe.shape)

# fit a cluster model
km = KMeans(n_clusters=2)

# predict the cluster
for k,v in zip(["a","b","c","d", "e"], km.fit_predict(X)):
    print("%s is in cluster %d" % (k,v))

相关问题 更多 >

    热门问题