Search code examples
scikit-learncluster-analysisnameerror

NameError: name 'pairwise_distances_argmin' is not defined


Upon running this code:

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

from sklearn.datasets import make_blobs
X, y_true = make_blobs(n_samples = 300, centers= 4, cluster_std =0.60,random_state = 0)
plt.scatter(X[:,0], X[:,1], s=50)

labels = pairwise_distances_argmin(X, centres)

I am getting a NameError

"name 'pairwise_distances_argmin' is not defined"

What may be the possible reasons for it.

Please note that I am relatively new to this field, elaborated answers are most welcome.

Thanks in advance.

Have searched Stack overflow and unsuccessfully try solutions suggested on these pages:

  1. Weird results of sklearn.metrics.pairwise_distances_argmin_min when computing euclidean distance

  2. NameError: name 'sklearn' is not defined


Solution

  • You wish to use the pairwise_distances_argmin function.

    You're going to have to import it:

    from sklearn.metrics import pairwise_distances_argmin