-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK-means (from scratch).py
More file actions
63 lines (46 loc) · 1.44 KB
/
K-means (from scratch).py
File metadata and controls
63 lines (46 loc) · 1.44 KB
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
A1 = dict()
A2 = dict()
mat = list()
res = dict()
n1 = int(input("Total elements in A1: "))
print("\nEnter Elements value and membership value of set A1, seperated by spaces: \n")
for _ in range(n1):
n, k = input().split()
n = int(n)
k = float(k)
A1[n] = k
print(A1)
n2 = int(input("Total elements in A2: "))
print("\nEnter Elements value and membership value of set A2, seperated by spaces: \n")
for _ in range(n2):
n, k = input().split()
n = int(n)
k = float(k)
A2[n] = k
print(A2)
for k1 in A1:
p = list()
for k2 in A2:
x = (k1 + k2) % 3
p.append(x)
mat.append(p)
print(mat)
distinct = set(x for l in mat for x in l)
distinct = list(distinct)
print(distinct)
for i in range(len(distinct)):
exec("list" + str(distinct[i]) + " = list()")
print("created list %d" % (i + 1))
for k1 in A1:
for k2 in A2:
x = (k1 + k2) % 3
# print("list" + str(x) + ".append(" + A1[k] + ")")
exec("list" + str(x) + ".append(min([" + str(A1[k1]) + "," + str(A2[k2]) + "]))")
# exec("list" + str(x) + ".append(" + str(A2[k2]) + ")")
for i in range(len(distinct)):
exec("var" + str(distinct[i]) + " = max(list" + str(distinct[i]) + ")")
exec("print(var" + str(distinct[i]) + ")")
exec("res[" + str(distinct[i]) + "] = var" + str(distinct[i]))
print(res)
for key in res:
print("The element is " + str(key) + " and the membership value is " + str(res[key]))