Author Topic: most common value in a list (mode)  (Read 318 times)

0 Members and 2 Guests are viewing this topic.

hmspe

  • Bull Frog
  • Posts: 367
most common value in a list (mode)
« on: May 30, 2024, 03:25:26 PM »
I'm looking for a lisp function to find the mode of a list.  The list is numeric and represents distances between inserts in a drawing.  I'm having no luck finding anything useful.  Any help would be appreciated.  Online search is no help at all.
"Science is the belief in the ignorance of experts." - Richard Feynman

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: most common value in a list (mode)
« Reply #1 on: May 30, 2024, 03:39:02 PM »
What is mode of a list?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Re: most common value in a list (mode)
« Reply #2 on: May 30, 2024, 03:41:23 PM »
Not quite sure I understand either but it sounds like you'd want to build a "Run Length Encoding" or something of that nature (something to count the instances of each entry) to find the highest number, next highest, lowest, etc.
https://www.theswamp.org/index.php?topic=57274.0#top
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

hmspe

  • Bull Frog
  • Posts: 367
Re: most common value in a list (mode)
« Reply #3 on: May 30, 2024, 04:17:54 PM »
The mode is the most common value in a list.  I don't recall ever hearing the term in any of my math classes, but my grandchildren know it well.

Thanks for the link. 
"Science is the belief in the ignorance of experts." - Richard Feynman

ymg

  • Mosquito
  • Posts: 5
Re: most common value in a list (mode)
« Reply #4 on: May 30, 2024, 09:30:34 PM »
From https://www.investopedia.com/terms/m/mode.asp#:~:text=The%20mode%20in%20statistics%20refers,the%20numbers%20in%20the%20set.

The Bottom Line:
In statistics, the mode is the number that occurs most often. A data set can have one or more modes. The mode is different from the mean, which is the average of the numbers in a set; it's also different from the median, which is the midpoint of a set. Finding the mode in a set of numbers can tell you which data points occur most commonly, which can be useful information when analyzing statistics.


Just use count items by Lee Mac

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 LM:CountItems ( l / c x )
    (if (setq x (car l))
        (progn
            (setq c (length l)
                  l (vl-remove x (cdr l))
            )
            (cons (cons x (- c (length l))) (LM:CountItems l))
        )
    )
)

Examples:

Code: [Select]
(LM:CountItems '("A" "B" "B" "B" "C" "C" "D" "E" "E" "E" "E"))
=>  (("A" . 1) ("B" . 3) ("C" . 2) ("D" . 1) ("E" . 4))


(LM:CountItems '(1 2 3 3 3 4 5 6 6 7 8 8 8 8))
=>  ((1 . 1) (2 . 1) (3 . 3) (4 . 1) (5 . 1) (6 . 2) (7 . 1) (8 . 4))
« Last Edit: May 30, 2024, 09:56:14 PM by ymg »

rayakmal

  • Newt
  • Posts: 59
Re: most common value in a list (mode)
« Reply #5 on: May 30, 2024, 09:41:43 PM »
Welcome back, YMG.

Just use count items by Lee Mac


hmspe

  • Bull Frog
  • Posts: 367
Re: most common value in a list (mode)
« Reply #6 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.
"Science is the belief in the ignorance of experts." - Richard Feynman

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Re: most common value in a list (mode)
« Reply #7 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)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

hmspe

  • Bull Frog
  • Posts: 367
Re: most common value in a list (mode)
« Reply #8 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.
"Science is the belief in the ignorance of experts." - Richard Feynman

Lee Mac

  • Seagull
  • Posts: 12927
  • London, England
Re: most common value in a list (mode)
« Reply #9 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>

hmspe

  • Bull Frog
  • Posts: 367
Re: most common value in a list (mode)
« Reply #10 on: May 31, 2024, 10:11:16 AM »
Thanks, Lee. 
"Science is the belief in the ignorance of experts." - Richard Feynman

ymg

  • Mosquito
  • Posts: 5
Re: most common value in a list (mode)
« Reply #11 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))


kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2152
  • class keyThumper<T>:ILazy<T>
Re: most common value in a list (mode)
« Reply #12 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.

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.