Sklearn中pairwise_distances_argmin

Sklearn中pairwise_distances_argmin()方法

作用:遍历序列,求序列中距离的最小值,并返回其下标。

常用参数介绍:
pairwise_distances_argmin(X,y, axis=,metric=)
X,y:输入的序列
axis:取值0或1
metric:距离类型,通常使用euclidean(欧几里得距离)
返回值介绍:
返回值返回的是X或y序列中的下标。根据axis取值的不同返回的不同序列中的下标。

首先针对axis=1的情况,axis=1是计算X中与y距离最近,并返回对应y的下标。下面是例子。

from sklearn.metrics import pairwise_distances_argmin
X = [(0,0),(2,2)] #2个数据
y = [(1,0),(3,3),(2,2)] #3个数据
pairwise_distances_argmin(X,y,axis=1,metric='euclidean')
》》》
结果:array([0, 2], dtype=int64)

我们发现返回值只有两个,与X的数据个数一致。我们简单剖析一下。X中的一个数据(0,0)与y序列的三个数据计算距离,发现(0,0)与(1,0)的距离最近。(1,0)在y的下标是0。接着是X中的二个数据(2,2)与y序列的三个数据计算距离,发现(2,2)与(2,2)的距离最近。(2,2)在y的下标是2。于是就返回2。所以最后的结果是array([0, 2], dtype=int64)。

针对axis=0的情况,axis=0是计算y中与X距离最近,并返回对应X的下标。下面是例子。

from sklearn.metrics import pairwise_distances_argmin
X = [(0,0),(2,2)] #2个数据
y = [(1,0),(3,3),(2,2)] #3个数据
pairwise_distances_argmin(X,y,axis=0,metric='euclidean')
》》》
结果:array([0, 1, 1], dtype=int64)

我们发现返回值只有三个,与y的数据个数一致。与axis=1同理,只不过次是计算y中的数据与X数据的距离。y中的一个数据(1,0)与,x序列的两个个数据计算距离,发现(1,0)与(0,0)的距离最近。(0,0)在x的下标是0,返回0。第二个数据(3,3)与x序列计算距离,发现(3,3)与(2,2)的距离最近。(2,2)在x的下标是1,返回1。第三个数据(2,2),与x序列计算距离,发现(2,2)与(2,2)的距离最近。(2,2)在x的下标是1,返回1。所以最后的结果是array([0, 1, 1], dtype=int64)

import time import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import MiniBatchKMeans, KMeans from sklearn.metrics.pairwise import pairwise_distances_argmin from sklearn.datasets import make_blobs # Generate sample data np.random.seed(0) batch_size = 45 centers = [[1, 1], [-1, -1], [1, -1]] n_clusters = len(centers) X, labels_true = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7) # Compute clustering with Means k_means = KMeans(init='k-means++', n_clusters=3, n_init=10) t0 = time.time() k_means.fit(X) t_batch = time.time() - t0 # Compute clustering with MiniBatchKMeans mbk = MiniBatchKMeans(init='k-means++', n_clusters=3, batch_size=batch_size, n_init=10, max_no_improvement=10, verbose=0) t0 = time.time() mbk.fit(X) t_mini_batch = time.time() - t0 # Plot result fig = plt.figure(figsize=(8, 3)) fig.subplots_adjust(left=0.02, right=0.98, bottom=0.05, top=0.9) colors = ['#4EACC5', '#FF9C34', '#4E9A06'] # We want to have the same colors for the same cluster from the # MiniBatchKMeans and the KMeans algorithm. Let's pair the cluster centers per # closest one. k_means_cluster_centers = k_means.cluster_centers_ order = pairwise_distances_argmin(k_means.cluster_centers_, mbk.cluster_centers_) mbk_means_cluster_centers = mbk.cluster_centers_[order] k_means_labels = pairwise_distances_argmin(X, k_means_cluster_centers) mbk_means_labels = pairwise_distances_argmin(X, mbk_means_cluster_centers) # KMeans for k, col in zip(range(n_clusters), colors): my_members = k_means_labels == k cluster_center = k_means_cluster_centers[k] plt.plot(X[my_members, 0], X[my_members, 1], 'w', markerfacecolor=col, marker='.') plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=6) plt.title('KMeans') plt.xticks(()) plt.yticks(()) plt.show() 这段代码每一句在干什么
06-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值