#!/usr/bin/env python
# coding: utf-8

# In[3]:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().system('pip install scikit-fuzzy')
import skfuzzy as fuzz


# In[4]:


from mpl_toolkits.mplot3d  import Axes3D
from sklearn.datasets import make_moons
from sklearn.datasets import make_blobs


# In[14]:


X, y_true=make_blobs(n_samples=397, centers=3,cluster_std=0.60, random_state=97)
D=pd.DataFrame(X)
D.columns=['X', 'Y']


# In[15]:


plt.figure(figsize=(5,5))
plt.plot(D['X'], D['Y'], 'o', color='b')
plt.xlabel("Value of X")
plt.ylabel("Value of Y")
plt.title('Plot for X vs Y')
plt.show()


# In[16]:


np.random.seed(104729)
FPC=[]
K=[]
for k in range(2,10):
    cntr, u, u0,d,jm,p,fpc=fuzz.cluster.cmeans(D.T, k, m=2, error=0.0005, maxiter=1000, init=None)
    FPC.append(fpc)
    K.append(k)
    
K


# In[7]:


FPC


# In[17]:


k=pd.DataFrame(FPC).idxmax()+2
k
k[0]


# In[18]:


plt.figure(figsize=(6,5))
plt.plot(range(2,10), np.round(FPC,3), ls='-',
        linewidth=2, color="pink", marker='o',markerfacecolor='blue',markersize=7)
plt.axvline(x=k[0], color='b', ls='--', linewidth=1.5)
plt.title("Optimal number of cluster \n for Fuzzy C Means")
plt.xlabel("Number of clusters for fuzzy C nmeans")
plt.ylabel('Fuzzy partition coefficient (FPC)')
plt.show()


# In[13]:


np.random.seed(199999)
cntr, u, u0,d,jm,p,fpc=fuzz.cluster.cmeans(
      D.T,k[0], m=2, error=0.0005, maxiter=1000, init=None)


# In[28]:


print("Cluster centers :\n", cntr)
print("\n Within cluster sum of squares", jm)
print("Final partition matrix:\n", np.round(u,3))
print("Number of iterations", p)
print("The fuzzy partitions coefficient (FPC)", fpc)


# In[26]:


cluster_membership=np.argmax(u,axis=0)
cluster_membership
print("cluster membership:", cluster_membership)


# In[33]:


colors=['m','g','y', 'b', 'k','r','c']
plt.figure(figsize=(5,5))
for i in range(len(D)):
    plt.plot(D.iloc[i,0], D.iloc[i,1],'o',color=colors[cluster_membership[i]])
plt.xlabel("Value of X")
plt.ylabel("Value of Y")
plt. title("Plot for X vs Y")
plt.show()


# In[ ]:




