Skip to content
View in the app

A better way to browse. Learn more.

Opusmodus

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

kNN-algorithm

Featured Replies

I still have no idea what I could use this for in OPMO. In Max/MSP, it has interesting und usefull applications in the area of corpus-sampling. I didn’t write this code myself (Copilot), but it seems to fit.

GeeksforGeeks

K-Nearest Neighbor(KNN) Algorithm - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, co
;; kNN implementation in Common Lisp with weighted voting
;; -------------------------------------------------------
;; This program classifies a new data point based on the k nearest neighbors
;; from a given dataset, giving more weight to closer neighbors.

(defun euclidean-distance (vec1 vec2)
  "Compute the Euclidean distance between two numeric vectors."
  (unless (= (length vec1) (length vec2))
    (error "Vectors must have the same length."))
  (sqrt (reduce #'+
                (mapcar (lambda (a b) (expt (- a b) 2))
                        vec1 vec2))))

(defun knn-classify-weighted (dataset labels query k)
  "Classify QUERY using weighted k nearest neighbors from DATASET with LABELS.
   Closer neighbors contribute more to the vote."
  (when (or (null dataset) (null labels))
    (error "Dataset and labels cannot be empty."))
  (unless (= (length dataset) (length labels))
    (error "Dataset and labels must have the same length."))
  (when (or (<= k 0) (> k (length dataset)))
    (error "Invalid k value."))

  ;; Compute distances and pair with labels
  (let* ((distances (mapcar (lambda (point label)
                              (list (euclidean-distance point query) label))
                            dataset labels))
         ;; Sort by distance
         (sorted (sort distances #'< :key #'first))
         ;; Take k nearest
         (neighbors (subseq sorted 0 k))
         ;; Weighted label counts
         (label-weights (make-hash-table :test #'equal)))
    (dolist (n neighbors)
      (let ((dist (first n))
            (label (second n)))
        ;; Weight: inverse of distance (avoid division by zero)
        (incf (gethash label label-weights 0)
              (if (zerop dist) 1e6 (/ 1.0 dist)))))
    ;; Return label with highest total weight
    (car (car (sort (loop for key being the hash-keys of label-weights
                          collect (list key (gethash key label-weights)))
                    #'>
                    :key #'second)))))

;; Example usage
(let* ((dataset '((1.0 2.0) (2.0 3.0) (3.0 3.0) (6.0 5.0) (7.0 8.0)))
       (labels  '("A" "A" "B" "B" "B"))
       (query   '(2.5 3.0))
       (k 3))
  (format t "Predicted class for ~a: ~a~%"
          query
          (knn-classify-weighted dataset labels query k)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; GENERATING A 2D-RND_DATASET

(setf dataset_1
      (loop for x from 0.0 to 0.999 by 0.001 
            for y in (gen-noise 1000 :seed 6235)
            collect (list x y)))


;; GENERATING LABELS (a scale, 7 labels)

(setf labels_1 (rnd-sample 1000 (make-scale 'c4 7 :alt '(1 2)) :prob 0.5))
            

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; EXAMPLE

(let* ((dataset dataset_1)
       (labels  labels_1)
       (query   '(0.21 0.112)) ;; what i'm searching (2d)
       (k 5))
  (format t "Predicted class for ~a: ~a~%"
          query
          (knn-classify-weighted dataset labels query k)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  • AM changed the title to kNN-algorithm

Create an account or sign in to comment


Copyright © 2014-2026 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.