I was having a look at the wiki description with a view to trying to do my own version and then comparing with Emir U's code.  But I was stuck wondering about the lack of definition of cost,  as well as what the greedy choice of k points is aiming to optimise at the start. The
algorithm is rather sparsely described there.

Anyway,  I thought it _might_ be worth considering using complex coordinates,
eg
   cdata =: j./"1 data

Euclidean distance is then eg
   | x1j1 - x0j0

Forgetting that magnitude was simply | ,  I discovered there's a mistake in the NuVoc:
this appears near the bottom of
https://code.jsoftware.com/wiki/Vocabulary/jdot#dyadic  :

Related primitives
Real/Imag (+. y), Signum (Unit Circle) (* y), Length/Angle (*. y), Magnitude (* y), .....

Clearly,  it should read " .... Magnitude  (| y), ..."

Faut de mieux,  I came up with this for build,  not knowing what greediness was trying
to achieve:
   build =: (3 $: ]) : (]{~ (?#))    NB. choose x of y at random,  x = 3 by default...

Cheers,

Mike

On 13/02/2021 13:04, Clifford Reiter wrote:
I will take a stab at the euclidean distance matrix verb. I note that your
edist gives the square of the distance.

+/&.:*: 3 4 NB. euclidean length

5

ed=:+/&.:*:@:-"1 NB. euclidean distance

0 0 ed 3 4

5

1 2 ed 4 6

5

ed/~ 1 2,:4 6

0 5

5 0

edist 1 2,:4 6

0 25

25 0

edm=:ed/~ NB. euclidean dist matrix verb

./|,D-*: edm data
8.52651e_14

The same up to roundoff

On Sat, Feb 13, 2021 at 4:54 AM Emir U <[email protected]> wrote:

Hi guys, I'm very new to J (about 6-7 hours experience), I'm coding a
bunch of useful algos for clustering, optimisation and stats to give J a
proper go: this is my first. I (think) I've coded the k-mediods PAM algo as
described here: https://en.wikipedia.org/wiki/K-medoids

I'm trying to tune my intuition as to what this code should look like when
written properly. i.e. in canonical form. I'd be grateful for your
feedback. I've enclosed the code below which is basically the kM_step and
kM functions: the rest is testing code. If you run it it'll generate some
data, cluster it and plot it for you.


NB. UPDATE MEDIODS FROM DATA.
kM_step=: 4 : 0
d=. (x { "1 y)
s=. ([@> (i. <./) each (;/ d )) { x
idx=: I. & (= & s)
d=. { "1 ({ & y)
w=. (i. <./) & (+/%#) & d
; ((w { [) & idx) each x
)

NB. ITERATE UPDATE STEP TO CONVERGENCE.
choose=: 4 : '({~ x ? #) ,y'
kM=: 4 : '(kM_step & y) ^:_ x choose (i. #y)'

NB. CALCULATE PAIRWISE EUCLIDEAN DISTANCE MATRIX.
edist=: 3 : 0
x=. +/ |: y^2
a=. (,.x) +/ .+ (,:x)
b=. y +/ .* (|: y)
a - (2*b)
)

NB. TESTING: GENERATE DATA, FIT & PLOT.
load 'plot'

data=: ?(50 2$0)
data=: data,3+(3*?(50 2$0))
data=: data,5+(4*?(50 2$0))
D=: edist data
M=: (3 kM D) { data

pd 'reset'
pd 'type dot'
pd 'pensize 3.1'
pd 'color gray'
pd ;/ |: data
pd 'show'
pd 'color red'
pd 'pensize 4'
pd ;/ |: M

M

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm


--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to