-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (138 loc) · 6.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pandas as pd # reading all required header files
import numpy as np
import random
import operator
import math
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data)
#Number of data
n = len(df)
#number of clusters
k = 3
#dimension of cluster
d = 4
# m parameter
m = 2
#number of iterations
MAX_ITERS = 12
plt.figure(0,figsize=(5,5)) #scatter plot of sepal length vs sepal width
plt.scatter(list(df.iloc[:,0]), list(df.iloc[:,1]), marker='o')
plt.axis('equal')
plt.xlabel('Sepal Length', fontsize=16)
plt.ylabel('Sepal Width', fontsize=16)
plt.title('Sepal Plot', fontsize=25,color='b')
plt.grid()
plt.show()
plt.figure(1,figsize=(5,5)) #scatter plot of sepal length vs sepal width
plt.scatter(list(df.iloc[:,2]), list(df.iloc[:,3]), marker='o')
plt.axis('equal')
plt.xlabel('petal Length', fontsize=16)
plt.ylabel('petal Width', fontsize=16)
plt.title('Petal Plot', fontsize=25,color='b')
plt.grid()
plt.show()
def initializeMembershipWeights():
"""
membership_mat = []
for i in range(n):
wts = []
sum=0;
for j in range(k):
weight = np.random.random_integers(1,10)
wts.append(weight)
sum = sum + weight
weights = [w/sum for w in wts]
membership_mat.append(weights)
print(membership_mat)
"""
weight = np.random.dirichlet(np.ones(k),n)
weight_arr = np.array(weight)
return weight_arr
def computeCentroids(weight_arr):
C = []
for i in range(k):
weight_sum = np.power(weight_arr[:,i],m).sum()
Cj = []
for x in range(d):
numerator = ( df.iloc[:,x].values * np.power(weight_arr[:,i],m)).sum()
c_val = numerator/weight_sum;
Cj.append(c_val)
C.append(Cj)
return C
def updateWeights(weight_arr,C):
denom = np.zeros(n)
for i in range(k):
dist = (df.iloc[:,:].values - C[i])**2
dist = np.sum(dist, axis=1)
dist = np.sqrt(dist)
denom = denom + np.power(1/dist,1/(m-1))
for i in range(k):
dist = (df.iloc[:,:].values - C[i])**2
dist = np.sum(dist, axis=1)
dist = np.sqrt(dist)
weight_arr[:,i] = np.divide(np.power(1/dist,1/(m-1)),denom)
return weight_arr
def plotData(z,C):
plt.subplot(4,3,z+1) #scatter plot of sepal length vs sepal width
plt.scatter(list(df.iloc[:,2]), list(df.iloc[:,3]), marker='o')
for center in C:
plt.scatter(center[2],center[3], marker='o',color='r')
plt.axis('equal')
plt.xlabel('Sepal Length', fontsize=16)
plt.ylabel('Sepal Width', fontsize=16)
plt.grid()
def FuzzyMeansAlgorithm():
weight_arr = initializeMembershipWeights()
plt.figure(figsize=(50,50))
for z in range(MAX_ITERS):
C = computeCentroids(weight_arr)
updateWeights(weight_arr,C)
plotData(z,C)
plt.show()
return (weight_arr,C)
final_weights,Centers = FuzzyMeansAlgorithm()
df_sepal = df.iloc[:,0:2]
df_petal = df.iloc[:,2:5]
plt.figure(0,figsize=(5,5)) #scatter plot of sepal length vs sepal width
plt.scatter(list(df_sepal.iloc[:,0]), list(df_sepal.iloc[:,1]), marker='o')
plt.axis('equal')
plt.xlabel('Sepal Length', fontsize=16)
plt.ylabel('Sepal Width', fontsize=16)
plt.title('Sepal Plot', fontsize=25,color='b')
plt.grid()
for center in Centers:
plt.scatter(center[0],center[1], marker='o',color='r')
plt.show()
plt.figure(1,figsize=(5,5)) #scatter plot of sepal length vs sepal width
plt.scatter(list(df_petal.iloc[:,0]), list(df_petal.iloc[:,1]), marker='o')
plt.axis('equal')
plt.xlabel('petal Length', fontsize=16)
plt.ylabel('petal Width', fontsize=16)
plt.title('Petal Plot', fontsize=25,color='b')
plt.grid()
for center in Centers:
plt.scatter(center[2],center[3], marker='o',color='r')
plt.show()
X = np.zeros((n,1))
plt.figure(0,figsize=(8,8)) #scatter plot of sepal length vs sepal width
plt.axis('equal')
plt.xlabel('Sepal Length', fontsize=16)
plt.ylabel('Sepal Width', fontsize=16)
plt.title('Sepal Plot', fontsize=25,color='b')
plt.grid()
for center in Centers:
plt.scatter(center[0],center[1], marker='D',color='r')
clr = 'b'
for i in range(n):
cNumber = np.where(final_weights[i] == np.amax(final_weights[i]))
if cNumber[0][0]==0:
clr = 'y'
elif cNumber[0][0]==1:
clr = 'g'
elif cNumber[0][0]==2:
clr = 'm'
plt.scatter(list(df_sepal.iloc[i:i+1,0]), list(df_sepal.iloc[i:i+1,1]), alpha=0.25,s=100,color=clr)
plt.show()