# K-Means [Fx-5800P]

fx-5800P

No replies to this topic

### #1 Tritonio

Tritonio

Newbie

• Members
• 16 posts
• Gender:Male

• Calculators:
FX-5800P, FX-991ES+, FX-991EX, HP Prime

Posted 12 November 2014 - 02:44 AM

This will run the K means algorithm with the given number of clusters (K). It will output the centers of the clusters and will also use the Freq column to assign a cluster number to each row of the stats table. The initial values of the Freq column are NOT used so you have to actually type in multiple times any point with multiplicity instead of using the Freq column.

(It's a bit big so if you encounter any errors, please point them out, maybe I've mistyped something.)

```FreqOff
n→N
FreqOn
0→L
"K="?K
0→S
0→M
For 1→I To T
RanInt#(1,K)→List Freq[I]
Next
Do
S→L
M+1→M
0→DimZ
3K+3→DimZ
For 1→I To T
List Freq[I]→J
Z[3J]+List X[I]→Z[3J]
Z[3J+1]+List Y[I]→Z[3J+1]
Z[3J+2]+1→Z[3J+2]
Next
For 1→I To K
If Z[3I+2]=0:Then
List X[RanInt#(1,T)]→Z[3I]
List X[RanInt#(1,T)]→Z[3I+1]
Else
Z[3I]÷Z[3I+2]→Z[3I]
Z[3I+1]÷Z[3I+2]→Z[3I+1]
IfEnd
Next
0→S
For 1→I To T
1×10^99→B
For 1→J To K
√((List X[I]-Z[3J])²+(List Y[I]-Z[3J+1])²)→D
If D<B:Then
D→B
J→W
IfEnd
Next
W→List Freq[I]
S+B→S
Next
Cls
Locate 1,1,"S="
Locate 3,1,S÷T
Locate 1,2,"ITERATION "
Locate 11,2,M
LpWhile S≠L
Locate 1,3,"CONVERGED."◣
For 1→I To K
Cls
Locate 1,1,"K="
Locate 3,1,I
Locate 1,2,"X="
Locate 3,2,Z[3I]
Locate 1,3,"Y="
Locate 3,3,Z[3I+1]◣
Next
Cls
"DONE."```

### Also tagged with one or more of these keywords: fx-5800P

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users