How to calculate the distance between a random point in a dataset and a center for the kohonen algorithm?

93 Views Asked by At

I'm trying to calculate the distance between a random datapoint in the dataset and the center for the centroid. However, I get an error.

dist= math.sqrt((W[k][0]-dataset[0])**2 + (W[k][1]-dataset[1]**2))

TypeError: only size-1 arrays can be converted to Python scalars

My code:

import pandas as pd
import numpy as np
import random
import math
a1 = np.random.randn(250,2)
c1 = (0,0)
arr1 = a1 + (0,0)
a2 = np.random.randn(250,2)
arr2= a2 + [10,10]
c2 = (10,10)
a3 = np.random.randn(250,2)
arr3 = a3 + (0,10)
c3 = (0,10)
a4 = np.random.randn(250,2)
arr4 = a4 + (10,0)
c4 = (10,0)
alpha = 0.1
W = [[0,0],[0,10],[10,0],[10,10]]
dataset = [arr1,arr2,arr3,arr4]
for i in range(100):
    d_min = 999999
    for j in range(250):
        for k in range(4):
            dist= math.sqrt((W[k][0]-dataset[0])**2 + (W[k][1]-dataset[1]**2))
            if dist>d_min:
                d_min = dist
                k_min = k
        W[:,k_min] = W[:,k_min]*(1-alpha) + alpha*dataset[i]
    alpha = 0.5*alpha
0

There are 0 best solutions below