text_html_output_1.png
from fcmeans import FCM

from sklearn.datasets import load_digits

from sklearn.preprocessing import minmax_scale

import numpy as np # linear algebra

digits = load_digits()

K = 10 # number of clusters

X = digits['data']
feature_names = digits['feature_names']
X = minmax_scale(X,feature_range=(0,1))
npixels = int(np.sqrt(X.shape[1]))

y = digits['target']
target_names = digits['target_names']

images = digits['images']
for i in range(1,101):
    ax = plt.subplot(10,10,i)
    ax.imshow(images[i],cmap='Greys')
    ax.axes.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)

fcm = FCM(n_clusters=K, m=1.1, max_iter=100, error=1e-09, random_state=911)
fcm.fit(X)
centers = fcm.centers
labels = fcm.predict(X)

for i in range(K):
    ax = plt.subplot(1,10,i+1)
    ax.imshow(np.reshape(fcm.centers[i],[npixels,npixels]),cmap='Greys')
    ax.axes.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)

i1 = np.where(y==9) # label = 3 -> digit = 9
i2 = np.where(labels==8) # label = 8 -> digit = 3
S = list(set(i2[0]).intersection(set(i1[0])))

for i in range(len(S)):
    ax = plt.subplot(1,len(S),i+1)
    ax.imshow(np.reshape(X[S[i]],[npixels,npixels]),cmap='Greys')
    ax.axes.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)
    ax.set(title=str(i))
    
for i in range(3,6):
    plt.figure()

    ax = plt.subplot(2,3,1)
    ax.imshow(np.reshape(X[S[i]],[npixels,npixels]),cmap='Greys')
    ax.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)
    
    ax = plt.subplot(2,3,3)
    ax.plot(np.log(fcm.soft_predict(X[S])[i]));
    ax.grid(visible=True)
    ax.set_xticks(list(range(10)))    
    ax.set(xlabel='Label',ylabel='Membership (log-scale)')

fcm.soft_predict(X[[0]])