Skip to content

Bugs in "K-Means" BASIC code #24

@thecloudexpanse

Description

@thecloudexpanse

@mdcramer
I attempted to follow along with the "K-Means" program described in https://mdcramer.github.io/apple-2-blog/k-means/, once I had the previous "Refactored" program running successfully. There appears to be an omission in the published BASIC source and it is unable to successfully converge.

The "Draw Decision Boundary" code calls a non-existent subroutine at line 3060:
3060 GOSUB 3500

It's not clear what this subroutine should do or whether some previously presented code has been renumbered. If I just remove it, e.g.:
3060 REM GOSUB 3500 -- WHAT DOES THIS DO?

then the program will execute but it never converges, producing ACCURACY = 50% output for multiple iterations until it eventually hits a bad subscript error.

Also, I read that you are using ChatGPT to scan photos of the screen. Looks like it has trouble distinguishing 0 (zero) from O (upper-case O) on these lines:

- 2620   K0(J,0) = KM(J,0)
- 2630   K0(J,1) = KM(J,1)
+ 2620   KO(J,0) = KM(J,0)
+ 2630   KO(J,1) = KM(J,1)

UPDATE: After correcting the additional typos found on this line, the k-means program now successfully converges, though it is still reporting an ACCURACY = 50% so not really trusting the results yet.

- 2830   DI = DI + (KM%(I,0) - KO%(I,0)) ^ 2 + (KM%(I,1) - KO%(I,1)) ^ 2
+ 2830   DI = DI + (KM(I,0) - KO(I,0)) ^ 2 + (KM(I,1) - KO(I,1)) ^ 2

UPDATE 2: I think it's fixed now. Those were not the only references to a KM% array (instead of just KM). Once I changed all the references, it seems to be computing convergence correctly!

2690   KM(Y,0) = KM(Y,0) + DS%(I,0)
2700   KM(Y,1) = KM(Y,1) + DS%(I,1)
2710   KM(Y,2) = KM(Y,2) + 1

2740   KM(I,0) = KM(I,0) / KM(I,2)
2750   KM(I,1) = KM(I,1) / KM(I,2)

3030   IF KM(I - 1,1) - KM(I,1) <> 0 THEN M = -1 * (KM(I - 1,0) - KM(I,0)) / (KM(I - 1,1) - KM(I,1))
3040   P%(0,0) = (KM(I,0) - KM(I - 1,0)) / 2 + KM(I - 1,0)
3050   P%(0,1) = (KM(I,1) - KM(I - 1,1)) / 2 + KM(I - 1,1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions