Question: I need to compute labels from a clustering centers that was generated by another clusterer **in a different dataset without the original clusterer object**.

I know i could hard code it in python like so

```py
def compute_labels(centers,datapoints):
    ans=[]
    for point in datapoints:
        ans.append(
            min(
                ((i,np.linalg.norm(point-center)) for i,center in enumerate(centers)),
                key=lambda t:t[1]
            )[0]
        )
    return ans

```

however, it would be slow for my application, i need a lower level implementation of it, so i was wondering if would be possible just using scikit-learn or numpy.

what i tried:

```py
from sklearn.cluster import KMeans
import numpy as np

np.random.seed(42)

datapoints1=np.random.rand(200,38)
datapoints2=np.random.rand(200,38)

kmeans1=KMeans(
    init="k-means++",
    random_state=42,
    n_init=100
 )
kmeans1=kmeans1.fit(datapoints1)

kmeans2=KMeans(
    init=kmeans1.cluster_centers_,
    max_iter=1,
    n_init=1
)
kmeans2.predict(datapoints2)
print((kmeans1.cluster_centers_==kmeans2.cluster_centers_).all())

```

but it raises `sklearn.exceptions.NotFittedError` exception in `kmeans2.predict(datapoints2)`, i tried setting `max_iter=0` in kmeans kwargs, but it raises an exception as well.

Answer: 
sklearn is checking a function called `check_is_fitted` which is looking at attributes of the model. In your case, as you never called `fit`, some attributes doesn't exist which triggers the error. You can fake it by creating them manually such as:

```
from sklearn.cluster import KMeans
import numpy as np

np.random.seed(42)

datapoints1=np.random.rand(200,38)
datapoints2=np.random.rand(200,38)

kmeans1=KMeans(
    init="k-means++",
    random_state=42,
    n_init=100
 )
kmeans1=kmeans1.fit(datapoints1)

kmeans2=KMeans(
    init=kmeans1.cluster_centers_,
    max_iter=1,
    n_init=1
)

kmeans2.cluster_centers_ = kmeans1.cluster_centers_                # you have it
kmeans2.labels_ = kmeans1.labels_                                  # to test if required, no difference found
print([v for v in vars(kmeans2)
       if v.endswith("_") and not v.startswith("__")])             # if this list is empty, the model if not fitted, you can compare it to kmeans1

pred = kmeans2.predict(datapoints2)
print(pred)                                                        # [3 7 1 ... 2]
print((kmeans1.cluster_centers_== kmeans2.cluster_centers_).all()) # True

```
Just an addition to [Nicolas M. answer](https://stackoverflow.com/a/60622765/5133524)

Generalized Function (with a dummy static variable):

```py
def compute_labels(centers,datapoints):
    compute_labels.dummy.cluster_centers_=centers
    return compute_labels.dummy.predict(datapoints)
compute_labels.dummy=KMeans()

```