Recent Posts

Pages: [1] 2 3 ... 10
1
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by Lee Mac on Today at 08:47:45 AM »
Adapting the code to yield multiple values representing the mode is relatively straightforward -
Code - Auto/Visual Lisp: [Select]
  1. (defun multimode ( lst fuz / cnt dif itm len rtn )
  2.     (while lst
  3.         (setq len (length lst)
  4.               itm (car lst)
  5.               lst (vl-remove-if '(lambda ( x ) (equal itm x fuz)) (cdr lst))
  6.               dif (- len (length lst))
  7.         )
  8.         (cond
  9.             (   (= cnt dif)
  10.                 (setq rtn (cons itm rtn))
  11.             )
  12.             (   (< cnt dif)
  13.                 (setq cnt dif rtn (list itm))
  14.             )
  15.         )
  16.     )
  17.     (reverse rtn)
  18. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (multimode '(1 2 3 3 4 5) 0.1)
  2. (3)
  3. _$ (multimode '(1 2 3 3 5 4 5) 0.1)
  4. (3 5)
2
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by kdub_nz on May 31, 2024, 11:33:53 PM »
Quote
The mode is the most common number that appears in your set of data. To find the mode count how often each number appears and the number that appears the most times is the mode.
https://www.ncl.ac.uk/webtemplate/ask-assets/external/maths-resources/statistics/descriptive-statistics/mean-median-and-mode.html#:~:text=Mode-,Definition,most%20times%20is%20the%20mode.

Quote
Mode: The most frequent number—that is, the number that occurs the highest number of times.
https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review

Seems 'mode' is not an Cardinal value, just the number that appears most . . nor an analysis of the other numbers.

. . . so my expectation would be to return a single number, or a list if there are multiple modes

Having two modes is called "bimodal".

Having more than two modes is called "multimodal".

I s'pose the handling of a list with an equal amount of each number would depend on the use case.


//--
edge cases can be a killer.

3
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by ymg on May 31, 2024, 10:56:54 PM »
As far as I know you can have more than one mode.  Also if every item as the same count mode should be undefined.

Simply doing a vl-sort on the countitems routine of Lee give you the modes at a glance as wwell as the frequency of each item.

Code: [Select]
;; Count Items  -  Lee Mac                                    ;
;; Returns a list of dotted pairs detailing the number of     ;
;; occurrences of each item in a supplied list.               ;

(defun CountItems ( l / x c )
   
    (if (setq x (car l))
        (progn
            (setq c (length l)
                  l (vl-remove x (cdr l))  
            )
            (cons (cons x (- c (length l))) (CountItems l))
        )
       
    )
   
)

(defun mode (l)
   (setq l (vl-sort (countitems l) '(lambda (a b) (> (cdr a) (cdr b))))) 
)



Sample call:
Code: [Select]
_$ (mode '(1 2 3 3 3 3 4 5 6 6 7 8 8 8 8))
((3 . 4) (8 . 4) (6 . 2) (1 . 1) (2 . 1) (4 . 1) (5 . 1) (7 . 1))

4
.NET / Re: Previews or Thumbnails
« Last post by retsameht on May 31, 2024, 07:53:32 PM »
I think I must be missing something here, here is the code I have:

Code - C#: [Select]
  1.      activeDoc.SendStringToExecute("_.qsave\n", false, false, false);
  2.  

The above line doesn't execute until after your code returns control to AutoCAD. Unless it's being called from the application context, I would use the Editor's Command() method.
5
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by hmspe on May 31, 2024, 10:11:16 AM »
Thanks, Lee. 
6
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by Lee Mac on May 31, 2024, 07:05:33 AM »
Since you're already shortening the list using vl-remove-if with each iteration, it's more efficient to iterate until the list is exhausted rather than using a fixed iteration over the entire original list, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun mode ( lst fuz / cnt dif itm len rtn )
  2.     (while lst
  3.         (setq len (length lst)
  4.               itm (car lst)
  5.               lst (vl-remove-if '(lambda ( x ) (equal itm x fuz)) (cdr lst))
  6.               dif (- len (length lst))
  7.         )
  8.         (if (< cnt dif)
  9.             (setq cnt dif rtn itm)
  10.         )
  11.     )
  12.     rtn
  13. )
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
  2.  
  3.     (MODE L 0.01)......1782 / 2.49 <fastest>
  4.     (@MODE L 0.01).....4437 / 1.00 <slowest>
7
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by hmspe on May 31, 2024, 12:08:35 AM »
Sorry, no time to look at code but what happens in the following situation?

11, 11, 33, 33, 54

Should be: (11, 33)
My most recent post will return 11.  My understanding is that the mode would be undefined for your data set because there is not a single number with the highest count in the data set. 

I'm working with distances between adjacent points in an x,y array that come from an ssget operation.  My data set will always have only one most common value.
8
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by JohnK on May 30, 2024, 10:59:28 PM »
Sorry, no time to look at code but what happens in the following situation?

11, 11, 33, 33, 54

Should be: (11, 33)
9
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by hmspe on May 30, 2024, 10:31:25 PM »
I found an old function by John Uhden in the Autocad forums and modified it slightly.  I changed a couple of single character variables to meaningful names and I return a value instead of a list containing all the instances of the most common value. 

Code - Auto/Visual Lisp: [Select]
  1. (defun @mode (lst fuzz / index count item items mode)
  2.   (setq index 0
  3.         count 0
  4.   )
  5.   (repeat (length lst)
  6.     (setq item (nth index lst)
  7.           items (vl-remove-if-not '(lambda (x)(equal x item fuzz)) lst)
  8.           index (1+ index)
  9.     )
  10.     (if (> (length items) count)
  11.       (setq count (length items) mode items)
  12.     )
  13.   )
  14.   (car mode)
  15. )
  16.  

Test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tx ()
  2.   (setq l (list 1.0 5.6 9.3.5 6.6 3 4 8 7 6 2 1 3 5 4 56 8 45 123 545 1 235 15 35 135 1 5 6 7 8 9.3 9.3 9.3 9.3 9.3 9.3 9.3 9.3 9.3 5 1 2 2))
  3.   (print (@mode l  0.01))
  4.   (princ)
  5. )
  6.  

Returns:
9.3

Thanks for the suggestions.
10
AutoLISP (Vanilla / Visual) / Re: most common value in a list (mode)
« Last post by rayakmal on May 30, 2024, 09:41:43 PM »
Welcome back, YMG.

Just use count items by Lee Mac

Pages: [1] 2 3 ... 10