I used sklearn for implementing k-means method. the k-means class has a method, called "predict". to predict new samples according to trained sample.
from sklearn.datasets import make_blobs
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score
'''
make sample
'''
X, y=make_blobs(n_samples=100, n_features=2, centers=3)
'''
kmeans
'''
kmeans_obj=KMeans(n_clusters=3)
#train
kmeans_obj.fit(X)
#labels:
labels=kmeans_obj.predict(X)
'''
output
'''
plt.scatter(X[:,0], X[:,1], c=labels)
plt.show()
'''
generate new samples and predict them
'''
while True:
'''
perdict kmeans?!?!?!?
'''
new_X, new_y=make_blobs(n_samples=50, n_features=2, centers=4)
perdict_new_sample_lables=kmeans_obj.predict(new_X)
plt.scatter(X[:,0], X[:,1], c=labels)
plt.scatter(new_X[:,0], new_X[:,1], c=perdict_new_sample_lables, marker="x")
plt.show()
circle shape in the pictures are trained dataset. and cross shape in the picture are new element that predicted.
the problem here isnot with deterministic, nondeterministic of the result. in nondeterministic algorithm output change in every run. but the result here is completely wrong!! in picture 2 violet cross must be Green