I have been trying to program a math formula in python. The code is running but I have strong belief that the code is not reflecting the actual math formulas,but I am so lost I don't know where I could have gone wrong. Below is my code and I have attached the actual maths formula. Please check if the python code matches the math formula -if not, assist on where I am going wrong. I am honestly so lost.
## Inter cluster distance
inter = 0
for i in range(centr.shape[1]-1):
for j in range(i+1,centr.shape[1]):
for l in range(centr.shape[0]):
inter = inter + (centr[l][i]-centr[l][j])**2
print('Intercluster distance: '+ str(inter))
## Intra cluster cluster
intra = 0
for i in range(data.shape[0]):
for j in range(centr.shape[0]):[enter image description here][2]
intra = intra + (data[i][j] - centr[j][Cc[i]-1])**2
print('Intracluster distance: '+ str(intra))
Math equation intra-cluster distance Mth equation inter-cluster distance
Observe how the range
constructor works:
>>> list(range(3))
[0, 1, 2]
>>> list(range(1, 3))
[1, 2]
So the default start value is 0, and the stop value is never included. You thus need slightly different start and stop values.
Also, the indices for the matrix elements z
in your code are probably all off by 1 unit compared to the formula you give, because Python indexing works similarly to range
, starting at 0, whereas in the formula they apparently start at 1.
So e.g. for the inter-cluster distance, we should have this code:
## Inter cluster distance
inter = 0
for i in range(1, centr.shape[1]):
for j in range(i+1, centr.shape[1] + 1):
for l in range(1, 3 + 1):
inter = inter + (centr[l - 1][i - 1] - centr[l - 1][j - 1])**2
print('Intercluster distance: '+ str(inter))
Or you could make the indices i, j, l
all one unit smaller and index the z
values accordingly, which should yield the same result:
## Inter cluster distance
inter = 0
for i in range(centr.shape[1] - 1):
for j in range(i, centr.shape[1]):
for l in range(3):
inter = inter + (centr[l][i] - centr[l][j])**2
print('Intercluster distance: '+ str(inter))