-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscretization.py
More file actions
78 lines (69 loc) · 1.92 KB
/
discretization.py
File metadata and controls
78 lines (69 loc) · 1.92 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import math;
import statistics;
def euc(x,y,a,b):
return math.sqrt(((x-a)*(x-a))+((y-b)*(y-b)));
def kmeans():
n = int(input("Enter number of data points "));
k = int(input("Enter number of clusters to be formed "));
x = [0];
y = [0];
for i in range(1, n + 1):
x.append(float(input("Enter x" + str(i))));
y.append(float(input("Enter y" + str(i))));
del x[0];
del y[0];
clu = [1];
cnx = [x[0]];
cny = [y[0]];
for i in range(1, k):
clu.append(i + 1);
cnx.append(x[i]);
cny.append(y[i]);
for i in range(k, n):
mindist = 10000;
clu.append(0);
for j in range(0, k):
t = euc(x[i], y[i], cnx[j], cny[j]);
if (t < mindist):
mindist = t;
clu[i] = clu[j];
cnx[clu[i] - 1] = (cnx[clu[i] - 1] + x[i]) / 2;
cny[clu[i] - 1] = (cny[clu[i] - 1] + y[i]) / 2;
print("Points in the order as entered in input have been assigned following cluster numbers");
print(clu);
def covariance(x, y, n):
xm = int(round(sum(x)/len(x)));
ym = int(round(sum(y)/len(y)));
mul = [0];
for i in range(0, n):
dx = x[int(i)]-xm;
dy = y[int(i)]-ym;
mul.append(dx*dy);
cov = (sum(mul))/(n-1);
return cov;
def correlation():
n = input("Enter n ");
n = int(n);
x = [0];
y = [0];
for i in range(1, n + 1):
x.append(float((input("Enter x" + str(i)))));
y.append(float((input("Enter y" + str(i)))));
del x[0];
del y[0];
cov = covariance(x, y, n);
sx = statistics.stdev(x);
sy = statistics.stdev(y);
cor = cov / (sx * sy);
print(cor);
if (cor > 0):
print("Positively related.");
else:
print("Negatively related.");
ch = int(input("Enter choice 1-K means 2-Correlation"));
if(ch==1):
kmeans();
elif(ch==2):
correlation();
else:
print("Invalid choice. Terminated.");