【问题标题】:Apply KNN from small supervised dataset to large unsupervised dataset in Python将 KNN 从小型监督数据集中应用到 Python 中的大型无监督数据集
【发布时间】:2019-01-31 11:02:47
【问题描述】:

我已经在一个包含大约 200 个 Python 样本的小型监督数据集上训练和测试了一个 KNN 模型。我想将这些结果应用于包含数千个样本的更大的无监督数据集。

我的问题是:有没有办法使用小型监督数据集拟合 KNN 模型,然后更改大型无监督数据集的 K 值?我不想通过使用较小数据集中的低 K 值来过度拟合模型,但不确定如何拟合模型,然后在 Python 中更改 K 值。

这可能使用 KNN 吗?有没有其他方法可以将 KNN 应用于更大的无监督数据集?

【问题讨论】:

  • 欢迎来到stackoverflow!请阅读stackoverflow.com/help/mcve。为您的问题提供一些数据。帮助社区为您提供帮助

标签: python scikit-learn knn


【解决方案1】:

我建议在较大的数据集上实际拟合 KNN 模型几次,每次使用不同的 k 值。对于这些模型中的每一个,您都可以计算Silhouette Score

比较各种轮廓得分,并为您的最终值k(聚类数)选择您用于最高得分模型的值。

举个例子,这是我去年为自己做的一些代码:

from sklearn import mixture
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt


## A list of the different numbers of clusters (the 'n_components' parameter) with 
## which we will run GMM.
number_of_clusters = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

## Graph plotting method
def makePlot(number_of_clusters, silhouette_scores):
    # Plot the each value of 'number of clusters' vs. the silhouette score at that value
    fig, ax = plt.subplots(figsize=(16, 6))
    ax.set_xlabel('GMM - number of clusters')
    ax.set_ylabel('Silhouette Score (higher is better)')
    ax.plot(number_of_clusters, silhouette_scores)

    # Ticks and grid
    xticks = np.arange(min(number_of_clusters), max(number_of_clusters)+1, 1.0)
    ax.set_xticks(xticks, minor=False)
    ax.set_xticks(xticks, minor=True)
    ax.xaxis.grid(True, which='both')
    yticks = np.arange(round(min(silhouette_scores), 2), max(silhouette_scores), .02)
    ax.set_yticks(yticks, minor=False)
    ax.set_yticks(yticks, minor=True)
    ax.yaxis.grid(True, which='both')

## Graph the mean silhouette score of each cluster amount.
## Print out the number of clusters that results in the highest
## silhouette score for GMM.
def findBestClusterer(number_of_clusters):
    silhouette_scores = []
    for i in number_of_clusters:
        clusterer = mixture.GMM(n_components=i) # Use the model of your choice here
        clusterer.fit(<your data set>) # enter your data set's variable name here
        preds = clusterer.predict(<your data set>)
        score = silhouette_score(<your data set>, preds)
        silhouette_scores.append(score)

    ## Print a table of all the silhouette scores
    print("")
    print("| Number of clusters | Silhouette score |")
    print("| ------------------ | ---------------- |")
    for i in range(len(number_of_clusters)):
        ## Ensure printed table is properly formatted, taking into account
        ## amount of digits (either one or two) in the value for number of clusters.
        if number_of_clusters[i] <= 9:
            print("| {number}                  | {score:.4f}           |".format(number=number_of_clusters[i], 
                                                                        score=round(silhouette_scores[i], 4)))
        else:
            print("| {number}                 | {score:.4f}           |".format(number=number_of_clusters[i], 
                                                                        score=round(silhouette_scores[i], 4)))


    ## Graph the plot of silhoutte scores for each amount of clusters
    makePlot(number_of_clusters, silhouette_scores)

    ## Find and print out the cluster amount that gives the highest 
    ## silhouette score.
    best_silhouette_score = max(silhouette_scores)
    index_of_best_score = silhouette_scores.index(best_silhouette_score)
    ideal_number_of_clusters = number_of_clusters[index_of_best_score]
    print("")
    print("Having {} clusters gives the highest silhouette score of {}.".format(ideal_number_of_clusters,
                                                                                round(best_silhouette_score, 4)))

findBestClusterer(number_of_clusters) 

请注意,在我的示例中,我使用了 GMM 模型而不是 KNN,但您应该能够稍微修改 findBestClusterer() 方法以使用您想要的任何聚类算法。在此方法中,您还将指定您的数据集。

【讨论】:

    【解决方案2】:

    在机器学习中有两大类学习者,即渴望学习者(决策树、神经网络、支持向量机...)和惰性学习者,例如 KNN。事实上,KNN 根本不做任何学习。它只存储您拥有的“标记”数据,然后使用它来执行推理,以便计算新样本(未标记)与其存储的数据中的所有样本(标记数据)的相似程度。然后基于新样本的K 最近实例(K 最近邻居因此得名)的多数投票,它将推断它的类/值。

    现在回答您的问题,“训练”KNNK 本身无关,因此在执行推理时请随意使用任何K 为您提供最佳结果的方法。

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2021-10-21
      • 2020-05-29
      • 2021-09-28
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2017-09-04
      • 2020-03-06
      相关资源
      最近更新 更多