繁体   English   中英

使用 Python 基于其模式对 URL 进行聚类

[英]Cluster URLs based on their pattern using Python

我是聚类技术的新手,我非常重视您可以为我的问题提供的任何输入。 基本上,我想根据结构模式对 URL 进行聚类。 例如

  • cluster1 - 简单的 URL https://domain/path/file
  • cluster2 - 缩短的 URL
  • cluster3 - 重定向 URL
  • ....
  • 簇 k - 新的 URL 模式

给定一个 URL 数据集,我想了解存在多少个不同的 URL 模式簇,然后直观地查看差异。

我在现有方法中看到的是明智的集群域(将同一网站的 URL 集群在一起)。 这不是我所期待的。 当我尝试基于 nlp(基于单词)的相似性聚类时,这种情况正在发生,因为同一网站的 URL 往往具有相同的单词,差异很小。

相反,我想关注 URL 结构并识别 URL 模式。 删除所有特殊字符并为每个 URL 创建一个词袋会使 URL 结构无效。 任何人都可以帮助我确定合适的聚类技术以及矢量化技术来识别不同的 URL 模式集群。

提前致谢

下面是如何对文本进行聚类的示例。

import numpy as np
from sklearn.cluster import AffinityPropagation
import distance
    
words = "kitten belly squooshy merley best eating google feedback face extension impressed map feedback google eating face extension climbing key".split(" ") #Replace this line
words = np.asarray(words) #So that indexing with a list will work
lev_similarity = -1*np.array([[distance.levenshtein(w1,w2) for w1 in words] for w2 in words])

affprop = AffinityPropagation(affinity="precomputed", damping=0.5)
affprop.fit(lev_similarity)
for cluster_id in np.unique(affprop.labels_):
    exemplar = words[affprop.cluster_centers_indices_[cluster_id]]
    cluster = np.unique(words[np.nonzero(affprop.labels_==cluster_id)])
    cluster_str = ", ".join(cluster)
    print(" - *%s:* %s" % (exemplar, cluster_str))

结果:

 - *eating:* climbing, eating
 - *google:* google, squooshy
 - *feedback:* feedback
 - *face:* face, map
 - *impressed:* impressed
 - *extension:* extension
 - *key:* belly, best, key, kitten, merley

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM