<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: k mean clustering algorithm in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9989159#M65652</link>
    <description>&lt;P&gt;Thank you, Currently I'm working with only 2D points.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for sharing the C# link, I'll try that code.&lt;/P&gt;</description>
    <pubDate>Sat, 09 Jan 2021 06:33:30 GMT</pubDate>
    <dc:creator>Satish_Rajdev</dc:creator>
    <dc:date>2021-01-09T06:33:30Z</dc:date>
    <item>
      <title>k mean clustering algorithm</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9986583#M65649</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Happy New Year!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for lisp function for the k mean clustering, was wondering if anybody have that already who can share it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;k mean&amp;nbsp;clustering algorithm:&lt;/P&gt;&lt;P&gt;&lt;A href="https://en.wikipedia.org/wiki/K-means_clustering" target="_blank"&gt;https://en.wikipedia.org/wiki/K-means_clustering&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 07:33:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9986583#M65649</guid>
      <dc:creator>Satish_Rajdev</dc:creator>
      <dc:date>2021-01-08T07:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: k mean clustering algorithm</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9988332#M65650</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1808573"&gt;@Satish_Rajdev&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Doesn't seem terribly complicated. I'd be interested in trying to come up with a solution over this weekend. I will update with what I come up with. Are you working with 2D points or 3D points?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For those interested,&lt;/P&gt;&lt;P&gt;This Article about how to accomplish it in C# has some good pseudocode and graphics describing the process:&lt;/P&gt;&lt;P&gt;&lt;A href="https://visualstudiomagazine.com/articles/2013/12/01/k-means-data-clustering-using-c.aspx" target="_blank"&gt;https://visualstudiomagazine.com/articles/2013/12/01/k-means-data-clustering-using-c.aspx&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;~DD&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 19:25:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9988332#M65650</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2021-01-08T19:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: k mean clustering algorithm</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9988665#M65651</link>
      <description>&lt;P&gt;Here is my quick attempt on the topic. It has one known bug. The code doesnt make sure that all clusters have at least 1 entity, so in some runs (with more clusters) it's possible to eat up all the entities of a cluster and error out. I post it as is (thats what makes it a quick attempt though). There are optimizations that can be implemented, depending on the size of the data set and the iterations you want to run.&lt;BR /&gt;&lt;BR /&gt;I've prepared a function "AddSampleData", which adds points at random. I'm using "PDMODE" 33 to better see what's going on.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;(defun c:test ( / numOfClusters maxIterations ss ctr ent pt cluster data means)

(if (setq ss (ssget '((0 . "POINT"))))
(progn

(setq numOfClusters 3)
(setq maxIterations 10)

(setq ctr 0)
(repeat (sslength ss)
(setq ent (ssname ss ctr)
      pt (cdr (assoc 10 (entget ent)))
      cluster (LM:randrange 1 numOfClusters)
)
(setq data (cons (list pt cluster ent) data))
(ColorPoint ent cluster)
(setq ctr (1+ ctr))
);repeat

(repeat maxIterations
(CalcMeans)
(setq data (CalcPoints))
);repeat
;;;(mapcar '(lambda (pt) (vl-cmdf "_circle" pt 10)) (mapcar 'car means));show means as circles
));if ss
(princ)
);defun

(defun CalcPoints ()

(mapcar '(lambda (x / pt cluster ent distances newCluster)
(setq pt (car x) cluster (cadr x) ent (caddr x))
(setq distances (mapcar '(lambda (mean) (list (distance pt (car mean)) (cadr mean))) means))
(setq newCluster (cadr (assoc (apply 'min (mapcar 'car distances)) distances)))

(if (/= cluster newCluster)
(progn
(ColorPoint ent newCluster)
(redraw ent);visualize changes
(list pt newCluster ent)
)
x
)
) data)
);defun


(defun CalcMeans ( / cluster)
(setq cluster 1 means nil)
(repeat numOfClusters
(setq means (cons (list (Average (mapcar 'car (vl-remove-if-not '(lambda (x) (equal (cadr x) cluster)) data))) cluster) means))
(setq cluster (1+ cluster))
)
);defun


(defun Average (data)
(mapcar '/ (apply 'mapcar (cons '+ data)) (list (length data) (length data) (length data)))
);defun


(defun ColorPoint (point color)
(setq ed (entget point) oldColor (assoc 62 ed))
(entmod (subst (cons 62 color) oldColor ed))
);defun


(defun AddSampleData ( / minX minY maxX maxY count pt)

(setq minX 0 minY 0 maxX 10000 maxY 10000 count 8000)
(repeat count
(setq pt (list (LM:randrange minX maxX) (LM:randrange minY maxY) 0))
(entmakex (list (cons 0 "POINT") (cons 10 pt) (cons 62 0)))
)
(princ)
);defun


;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
    (setq m   4294967296.0
          a   1664525.0
          c   1013904223.0
          $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
    )
    (/ $xn m)
);defun


;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
);defun&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 22:01:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9988665#M65651</guid>
      <dc:creator>doaiena</dc:creator>
      <dc:date>2021-01-08T22:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: k mean clustering algorithm</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9989159#M65652</link>
      <description>&lt;P&gt;Thank you, Currently I'm working with only 2D points.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for sharing the C# link, I'll try that code.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2021 06:33:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9989159#M65652</guid>
      <dc:creator>Satish_Rajdev</dc:creator>
      <dc:date>2021-01-09T06:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: k mean clustering algorithm</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9989187#M65653</link>
      <description>&lt;P&gt;Thank you for putting this effort. This is really awesome for the start, I can see there is a bug as you stated, I'll give more try on this code to make it better.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2021 07:10:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/k-mean-clustering-algorithm/m-p/9989187#M65653</guid>
      <dc:creator>Satish_Rajdev</dc:creator>
      <dc:date>2021-01-09T07:10:40Z</dc:date>
    </item>
  </channel>
</rss>

