C.Radford
C.Radford

Reputation: 932

Kmeans getting same result but changing classes it starts with

I am running a kmeans clustering on an image I am hoping to classify. When I run the program I get the same result expect my colours aren'y consistent meaning that kmeans isn't repeating the exact same process. How can I maintain the classes to equal the same thing each time I execute the program?

Here are two examples. First image in set is the kmeans cluster result and second is classification map on image.

SET 1 enter image description here enter image description here

Set 2 enter image description here enter image description here

Code:

#Set a 6 KMeans clustering
    kmeans = KMeans(n_clusters = 4, n_jobs = -2)

    #Compute cluster centers and predict cluster indices
    X_clustered = kmeans.fit_predict(x_3d)

    # Display scatter of kmeans
    plt.scatter(X[:, 0], X[:, 1], c=X_clustered, s=5, cmap='viridis')
    plt.show()

    # Create a temp dataframe from our PCA projection data "x_3d"
    df = pd.DataFrame(x_3d)
    df['X_cluster'] = X_clustered

    #create an greyscale image and remap the color pixel based on the df file given
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    tempImage = img
    row,col = img.shape[:2]
    count = 0
    for i in range(row):
        for j in range(col):
            if X_clustered[count]==0:
                tempImage[i,j] = (255,255,255,1)
            elif X_clustered[count]==1:
                tempImage[i,j] = (0,255,0,1)
            elif X_clustered[count]==2:
                tempImage[i,j] = (0,0,255,1)
            elif X_clustered[count]==3:
                tempImage[i,j] = (125,125,0,1)
            elif X_clustered[count]==4:
                tempImage[i,j] = (0,125,125,1)
            elif X_clustered[count]==5:
                tempImage[i,j] = (125,0,125,1)
            elif X_clustered[count]==6:
                tempImage[i,j] = (255,255,0,1)
            elif X_clustered[count]==7:
                tempImage[i,j] = (255,0,255,1)
            elif X_clustered[count]==8:
                tempImage[i,j] = (0,255,255,1)
            elif X_clustered[count]==9:
                tempImage[i,j] = (125,125,125)  
            count+= 1

    return tempImage

Upvotes: 0

Views: 442

Answers (1)

Martino
Martino

Reputation: 778

The initialisation of the K-Means algorithm is not deterministic.

If you're using scikit-learn's KMeans, then you can either provide your own initialisation (init=...), or you can provide a random seed, so that the same random numbers are generated every time (random_state=42).

Upvotes: 4

Related Questions